In: Advanced Math
Use the Newton’s method to find the root for ex+1 = 2 + x, over [−2, 2]. Can you find a way to numerically determine whether the convergence is roughly quadratic using error produced at each iteration? Include your answers as Matlab code comments
clc;
clear all;
format short
f=@(x)exp(x+1) - 2 - x; %function
f1=@(x)exp(x+1)-1; %derivative of function
x(1)=1; %initial value
tol=1e-7; % tolernece
erorr=0.1;
n=1;
Max_iter=18; % Maximum iteration
while(erorr>tol&n<Max_iter)
x(n+1)=x(n)-(2*f(x(n))/f1(x(n))); %newton method
%x=x1;
erorr(n)=abs((x(n)-x(n+1)));
n=n+1;
end
disp('_____________________________________________________________')
disp('n x(n)) f(x(n)) error ')
disp('____________________________________________________________')
for i=1:n-1
fprintf('%d\t%15f \t %20f \t %20f \n',i
,x(i),f(x(i)),erorr(i))
end
for i=2:n-2
r(i-1)=log(erorr(i+1)/erorr(i))/log(erorr(i)/erorr(i-1));
end
disp('Rate of convergence')
r'
disp('Root of function is')
y=x(end)
disp('Number of iteration to converge the root')
n-1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_____________________________________________________________
n x(n)) f(x(n)) error
____________________________________________________________
1 1.000000 4.389056
1.373929
2 -0.373929 0.244177
0.561166
3 -0.935095 0.002153
0.064203
4 -0.999298 0.000000
0.000702
5 -1.000000 0.000000
0.000000
Rate of convergence
ans =
2.4212
2.0830
2.0003
Root of function is
y =
-1.0000
Number of iteration to converge the root
ans =
5
>>