Apply Newton’s method to?(?)=?−2sin(?)=0. Compare the number of iterations required for convergence to that of the fixed-point iteration method with formulation ?=?(?)=2sin(?). Solve for x0 = pi/4, piand -piwith14-digit accuracy [i.e., tol = 10-(14+1)= 10-15]. Compare your solutions to those obtained using Matlab’s fsolve and fzero.
Newtons Method : Matlab code
f = @(x) x-2*Sin(x);
fp = @(x) 1-2Cos(x);
x0 = pi/4;
N = 10;
tol = 1E-14;
x(1) = x0; % Set initial guess
n = 2;
nfinal = N + 1;
while (n <= N + 1)
fe = f(x(n - 1));
fpe = fp(x(n - 1));
x(n) = x(n - 1) - fe/fpe;
if (abs(fe) <= tol)
nfinal = n;
break;
end
n = n + 1;
end
plot(0:nfinal - 1,x(1:nfinal),'o-')
title('Solution:')
xlabel('Iterations')
ylabel('X')
Fixed Point Iteration: Matlab Code
function [ x ] = fixedpoint(g,I,y,tol,m)
% input: g, I, y, tol, max
% g - function
% I - interval
% y - starting point
% tol - tolerance (error)
% m - maximal number of iterations
% x - approximate solution
a=I(1);b=I(2);
if(y<a | y>b)
error('The starting iteration does not lie in I.')
end
x=y;
gx=g(y);
while(abs(x-gx)>tol & m>0)
if(gx<a | gx>b)
error('The point g(x) does not lie in I.')
end
y=x;
x=g(y);
m=m-1;
end
Get Answers For Free
Most questions answered within 1 hours.