Answer To: Zero found Residual Actual iterations used5. Apply the secant method to compute the negative zero...
Kshitij answered on Oct 11 2021
MATLAB/Code_5.m
%% Problem No.5
% clear screen
clc
clear all
maxiter = 100; % max number of iteration before get the answer
f = input('Enter Function in terms of x: ');
xn_2 = input('Ener Lower Limit: ');
xn_1 = input('Ener Upper Limit: ');
maxerr = input('Enter Maximum Error: ');
xn = (xn_2*f(xn_1) - xn_1*f(xn_2))/(f(xn_1) - f(xn_2));
disp('xn-2 f(xn-2) xn-1 f(xn-1) xn f(xn)');
disp(num2str([xn_2 f(xn_2) xn_1 f(xn_1) xn f(xn)],'%20.7f'));
flag = 1;
while abs(f(xn)) > maxerr
xn_2 = xn_1;
xn_1 = xn;
xn = (xn_2*f(xn_1) - xn_1*f(xn_2))/(f(xn_1) - f(xn_2));
disp(num2str([xn_2 f(xn_2) xn_1 f(xn_1) xn f(xn)],'%20.7f'));
flag = flag + 1;
if(flag == maxiter)
break;
end
end
if flag < maxiter
disp(['Root is x = ' num2str(xn)]);
disp(['Iteration Used = ' num2str(flag)]);
else
disp('Root does not exist');
end
MATLAB/P6/Code_6.m
%% Problem 6.
disp('Problem 6.')
% Clear screen
clc
clear all
%% Part (b)
disp('Part (b)')
% Input Parameter
a1 = input('a1 =');
a2 = input('a2 =');
a3 = input('a3 =');
x0 = zeros(1,2);
x0(1) = input('X0(1) =');
x0(2) = input('X0(2) =');
%% Part (a)
disp('Part (a & c)')
% system of nonlinear equations in vector form
fun = @(x)[sin(a1*x(1)*x(2))-2*x(2)-x(1);a2*(exp(2*x(1))-exp(1))+a3*(4*x(2)^2-2*x(1))]
tol=0;
nmax=3;
[x,res,niter] = newtonsys(@fun_newtonsys, @fun_newtonsys_J, x0, tol,nmax,a1,a2,a3)
clear all
%% Part(d)
disp('Part (d)')
x0=[-0.4;0.06];
nmax = 100;
tol=1E-8;
a1=12.5;
a2=0.91;
a3=2.7;
[x,res,niter] = newtonsys(@fun_newtonsys, @fun_newtonsys_J, x0, tol,nmax,a1,a2,a3)
clear all
%% Part(e)
disp('Part (e)')
x0=[-0.46;0.09];
nmax = 200;
tol=1E-8;
a1=6.28;
a2=0.8;
a3=2.75;
[x,res,niter] = newtonsys(@fun_newtonsys, @fun_newtonsys_J, x0, tol,nmax,a1,a2,a3)
MATLAB/P6/fun_newtonsys.m
function f = fun_newtonsys(x, a1, a2,a3)
n = length(x); f = zeros(n, 1);
f(1) = sin(a1*x(1)*x(2))-2*x(2)-x(1);
f(2) = a2*(exp(2*x(1))-exp(1))+a3*(4*x(2)^2-2*x(1));
MATLAB/P6/fun_newtonsys_J.m
function J = fun_newtonsys_J(x, a1, a2,a3)
n = length(x); J = zeros(n, n);
J(1,1) = a1*x(2)*cos(a1*x(1)*x(2))-1; J(1,2) = -2;
J(2,1) = 2*a2*exp(2*x(1)) - 2*a3; J(2,2) = 8*a3*x(2);
MATLAB/P6/newtonsys.m
function [x,res,niter] = newtonsys(Ffun, Jfun, x0, tol,...
nmax, varargin)
niter = 0; err = tol + 1; x = x0;
while err >= tol && niter < nmax
J = Jfun(x,varargin{:}); F = Ffun(x,varargin{:});
delta = - J\F; x = x + delta;
err = norm(delta); niter = niter + 1;
end
res = norm(Ffun(x,varargin{:}));
if (niter==nmax && err> tol)
fprintf(['Fails to converge within maximum',...
'number of iterations.\n',...
'The iterate returned has relative ',...
'residual %e\n'],res);
else
fprintf(['The method converged at iteration ',...
'%i with residual %e\n'],niter,res);
end
return
MATLAB/P6/Untitled.m
x0=[-0.36;0.06];
nmax = 3;
tol=0;
a1=12.5;
a2=0.91;
a3=2.7;
[x,res,niter] = newtonsys(@fun_newtonsys, @fun_newtonsys_J, x0, tol,nmax,a1,a2,a3)
MATLAB/P7/approxJ_fd.m
function J = approxJ_fd(fun, x0, x1, varargin)
h = x0 - x1; % this is a naive way for h, can be improved
n = length(x0); J = zeros(n, n);
F = fun(x1, varargin{:});
for j = 1:n % form the approximate Jacobian
e = zeros(n,1); e(j) = 1; % form the j-th unit vector
J(:, j) = (fun(x1 + h(j)*e, varargin{:}) - F)/h(j);
end
MATLAB/P7/approxJ_fdh.m
function J = approxJ_fdh(fun, x0, h, varargin)
n = length(x0); J = zeros(n, n);
F = fun(x0, varargin{:});
for j = 1:n % form the approximate Jacobian
e = zeros(n,1); e(j) = 1; % form the j-th unit vector
J(:, j) = (fun(x0 + h*e, varargin{:}) - F)/h;
end
MATLAB/P7/Code_7.m
%% Problem No. 7
disp('Problem No. 7')
% clear screen
clc
clear all
%% Part (a)
disp('Part (a)')
a1 = 12.58;
a2 = 0.9;
a3 = 2.73;
fun = @(x)[sin(a1*x(1)*x(2))-2*x(2)-x(1);a2*(exp(2*x(1))-exp(1))+a3*(4*x(2)^2-2*x(1))];
x0 = [-0.3;0.05];
h = 0.01;
niter = 0;
J = approxJ_fdh(fun, x0, h)
delta = -J\fun(x0)
x = x0 + delta
x0 = x;
err = norm(delta)
niter = niter + 1
%% Part(b & c)
disp('Part (b & c)')
tol = 10^(-8); nmax = 100;
[x,res,niter] = newtonsys_approxJ_fdh(fun, x0, h, tol,nmax)
MATLAB/P7/newtonsys_approxJ_fd.m
function [x,res,niter] = newtonsys_approxJ_fd(Ffun, x0, x1, tol,...
nmax, varargin)
niter = 1; err = tol + 1; x = x1;
while err >= tol && niter < nmax
J = approxJ_fd(Ffun, x0, x, varargin{:});
F = Ffun(x, varargin{:});
delta = - J\F; x0 = x; x = x + delta;
err = norm(delta); niter = niter + 1;
end
res = norm(Ffun(x, varargin{:}));
if (niter==nmax && err> tol)
fprintf([' Fails to converge within maximum ',...
'number of iterations.\n',...
'The iterate returned has relative ',...
'residual %e\n'], res);
err
else
fprintf(['The method converged at iteration ',...
'%i with residual %e\n'],niter, res);
end
MATLAB/P7/newtonsys_approxJ_fdh.m
function [x,res,niter] = newtonsys_approxJ_fdh(Ffun, x0, h, tol,...
nmax, varargin)
niter = 1; err = tol + 1;x1=x0-h; x = x1;
while err >= tol && niter < nmax
J = approxJ_fdh(Ffun, x0, h, varargin{:});
F = Ffun(x, varargin{:});
delta = - J\F; x0 = x; x = x + delta;
err = norm(delta); niter = niter + 1;
end
res = norm(Ffun(x, varargin{:}));
if (niter==nmax && err> tol)
fprintf([' Fails to converge within maximum ',...
'number of iterations.\n',...
'The iterate returned has relative ',...
'residual %e\n'], res);
err
else
fprintf(['The method converged at iteration ',...
'%i with residual %e\n'],niter, res);
end
MATLAB/P8/approxJ_compl.m
function J = approxJ_compl(fun,x0,EPS,varargin)
n = length(x0); J = zeros(n, n); %EPS = 2*eps;
for j = 1:n % form the approximate Jacobian
e = zeros(n,1); e(j) = 1;
J(:, j) = (1./EPS).*imag(fun(x0 + EPS.*sqrt(-1).*e));
end
MATLAB/P8/Code_8.m
%% Problem No.8
disp('Problem No.8')
% Clear screen
clc
clear all
%% Part(b)
disp('Part(b)')
x0=[-0.3;0.05];
EPS=.1;
a1=12.576;
a2=0.918;
a3=2.72;
fun = @(x)[sin(a1*x(1)*x(2))-2*x(2)-x(1);a2*(exp(2*x(1))-exp(1))+a3*(4*x(2)^2-2*x(1))];
J = approxJ_compl(fun,x0,EPS)
niter=0;
delta = -J\fun(x0)
x = x0 + delta
x0 = x;
err = norm(delta)
niter = niter + 1
%% Part(d)
disp('Part(d)')
EPS = 10^(-5); tol = 10^(-8); nmax = 100;
[x,res,niter] = newtonsys_approxJ_compl(fun, x0, EPS, tol, nmax)
MATLAB/P8/newtonsys_approxJ_compl.m
function [x,res,niter] = newtonsys_approxJ_compl(Ffun, x0, EPS, tol,...
nmax, varargin)
h=2*EPS;
niter = 1; err = tol + 1;x1=x0-h; x = x1;
while err >= tol && niter < nmax
J = approxJ_compl(Ffun,x0,EPS,varargin{:});
F = Ffun(x, varargin{:});
delta = - J\F; x0 = x; x = x + delta;
err = norm(delta); niter = niter + 1;
end
res = norm(Ffun(x, varargin{:}));
if (niter==nmax && err> tol)
fprintf([' Fails to converge within maximum ',...
'number of iterations.\n',...
'The iterate returned has relative ',...
'residual %e\n'], res);
err
else
fprintf(['The method converged at iteration ',...
'%i with residual %e\n'],niter, res);
end
MATLAB/P9/approxJ_compl.m
function J = approxJ_compl(fun,x0,EPS,varargin)
n = length(x0); J = zeros(n, n); %EPS = 2*eps;
for j = 1:n % form the approximate Jacobian
e = zeros(n,1); e(j) = 1;
J(:, j) = (1./EPS).*imag(fun(x0 + EPS.*sqrt(-1).*e));
end
MATLAB/P9/broyden.m
function [x, res, niter, err, B]=broyden(Ffun, B0, x0, tol, ...
nmax, varargin)
niter = 0; err = tol + 1;
B = B0; x = x0;F = Ffun(x,varargin{:});
while err >= tol && niter < nmax
F = Ffun(x,varargin{:});
delta = -B\F; x = x + delta;
%B = B + (F*delta')/(delta'*delta);
err = norm(delta); niter = niter + 1;
end
res = norm(Ffun(x,varargin{:}));
if (niter==nmax && err> tol)
fprintf(['Fails to converge within maximum',...
'number of iterations.\n',...
'The iterate returned has relative ',...
'residual %e\n'],res);
else
fprintf(['The method converged at iteration ',...
'%i with residual %e\n'],niter,res);
end
return
MATLAB/P9/Code_9.m
%% Problem No.9
disp('Problem No.9')
% Clear Screen
clc
clear all
%% Part(b)
disp('Part(b)')
n=2;
x0=[-0.3;0.05];
a1=12.6;
a2=0.91;
a3=2.71;
fun = @(x)[sin(a1*x(1)*x(2))-2*x(2)-x(1);a2*(exp(2*x(1))-exp(1))+a3*(4*x(2)^2-2*x(1))];
B0=eye(n,n);
tol = 1e-8;
nmax = 3;
[x, res, niter, err, B]=broyden(fun, B0, x0, tol,nmax)
%% Part (c)
disp('Part (c)')
EPS=10^-7;
B0 = approxJ_compl(fun,x0,EPS)
tol = 1e-8;
nmax = 100;
[x, res, niter, err, B]=broyden(fun, B0, x0, tol,nmax)
MATLAB/Report.docx
Q 5.
Code
clc;
clear;
maxiter = 100; % max number of iteration before get the answer
f = input('Enter Function in terms of x: ');
xn_2 = input('Ener Lower Limit: ');
xn_1 = input('Ener Upper Limit: ');
maxerr = input('Enter Maximum Error: ');
xn = (xn_2*f(xn_1) - xn_1*f(xn_2))/(f(xn_1) - f(xn_2));
disp('xn-2 f(xn-2) xn-1 f(xn-1) xn f(xn)');
disp(num2str([xn_2 f(xn_2) xn_1 f(xn_1) xn f(xn)],'%20.7f'));
flag = 1;
while abs(f(xn)) > maxerr
xn_2 = xn_1;
xn_1 = xn;
xn = (xn_2*f(xn_1) - xn_1*f(xn_2))/(f(xn_1) - f(xn_2));
disp(num2str([xn_2 f(xn_2) xn_1 f(xn_1) xn f(xn)],'%20.7f'));
flag = flag + 1;
if(flag == maxiter)
break;
end
end
if flag < maxiter
disp(['Root is x = ' num2str(xn)]);
disp(['Iteration Used = ' num2str(flag)]);
else
disp('Root does not exist');
end
Output
Enter Function in terms of x: @(x) cosh(x)+cos(x)-3
Ener Lower Limit: 0
Ener Upper Limit: 2
Enter Maximum Error: 1E-12
xn-2 f(xn-2) xn-1 f(xn-1) xn f(xn)
0.0000000 -1.0000000 2.0000000 0.3460489 1.4858302 -0.5926628
2.0000000 0.3460489 1.4858302 -0.5926628 1.8104552 -0.0989666
1.4858302 -0.5926628 1.8104552 -0.0989666 1.8755297 0.0387352
1.8104552 ...