Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. (25 points) For 0, consider the equation Implement the secant method in MATLA

ID: 3281591 • Letter: 1

Question

1. (25 points) For 0, consider the equation Implement the secant method in MATLAB (or in any programming language) and find the root of the above equation. Use o0.5, r Tkl 10-10 as a convergence criterion. In addition, use your function from HW 2 employing Newton's method and repeat the calculation with same initial guess ro and convergence criterion as before. Attach your code for the secant method and provide MATLAB outputs for both cases. Which method converges faster? Briefly explain. , x1 = 0.6 and 12 k+1

Explanation / Answer

Matlab code

%%%% secant method

syms x
f=x+log(x);
tol=10^(-10);

y(1)=0.5;
y(2)=0.6;

fprintf('iteration roots error ');
for n=2:50
f1=subs(f,y(n));
f2=subs(f,y(n-1));
y(n+1)=y(n)-f1*(y(n)-y(n-1))/(f1-f2);
err=abs(y(n+1)-y(n));
fprintf(' %d %1.10f %1.10f ',(n-1),y(n+1),err);
if ( err < tol)
break;
end
end
fprintf('Roots of equation %f ', y(end) );

OUTPUT:

iteration roots error  
1 0.5684138975 0.0315861025   
2 0.5671202823 0.0012936152   
3 0.5671433068 0.0000230245   
4 0.5671432904 0.0000000164   
5 0.5671432904 0.0000000000   
Roots of equation 0.567143

%%%% Newton method %%%

clc;
clear all;
close all;
format long;
syms x
f=x+log(x);
tol=10^(-10);
% % % N-R Method;
s(1)=0.5;
fprintf('iteration roots error ');
for n=1:2000
l1=subs(f,s(n));
l2=subs(diff(f),s(n));
s(n+1)=s(n)-l1/l2;
e=abs(s(n+1)-s(n));
fprintf(' %d %1.10f %1.10f ',n,s(n+1),e);
if (e < tol)
break;
end
end
fprintf('Roots of equation = %f ',s(end));

OUTPUT:

iteration roots error  
1 0.5643823935 0.0643823935   
2 0.5671389877 0.0027565942   
3 0.5671432904 0.0000043027   
4 0.5671432904 0.0000000000   
Roots of equation = 0.567143

Newton method converge faster as it take only 4 iteration to converge as compared to 5 iteration taken by secant method