Can anyone help me write a MATLAB CODE which can run both secant method and modi
ID: 3110207 • Letter: C
Question
Can anyone help me write a MATLAB CODE which can run both secant method and modified secant method in one program. The program would prompt user to input any function f(x) and diplay each iteration data including the true error in form of table until the true error is below 1% if possible. You could refer to the example below to get the same result.
EXAMPLE 6.6 The Secant Method Problem Statement. Use the secant method to estimate the root of fox) e-x-x.Start with initial estimates of x 0 and xo 1.0 Solution. Recall that the true root is 0.56714329. First iteration X-1 fix-1) 1.00000 0.63212 -0.6321200 1) 0.61270 8.0% l (-0.63212) Second iteration: foxo) 0.63212 Xo xi 0.61270 fox 0.07081 ONote that both estimates are now on the same side of the root.) -0.07081 0.61270) X2 0.61270 0.56384 0.58% 0.63212 (-0.07081) Third iteration: 0.61270 f(x1 0.07081 0.56384 fx2) 0.00518 0.00518(0.61270 -0.56384) 0.56384 0.56717 0.0048% -0.07081 (-0.00518)Explanation / Answer
clc;
clear all;
f=@(x)exp(-x)-x;
x(1)=0;
x(2)=1;
n=1;
tol=1e-6;
iteration=0;
i=2;
eps = 1e0;
eps1 = 1e0;
del=0.01;
x1(i)=1;
%modified secont method
while (abs(eps1) > tol)
x1(i+1)=x1(i)-f(x1(i))*((x1(i)+x1(i)*del)-x1(i))/(f((x1(i)+x1(i)*del))-f(x1(i)));
eps1((i)) = x1(i+1)-0.56714329;
i = i+1;
x1(i);
end
disp('______________________________________________________________')
disp( 'itertion x_value error')
disp('______________________________________________________________')
for i=1:4
fprintf('%f %f %f ',i,x1(i),abs(eps1(i)))
end
fprintf(' Secont method ')
i=2;
%secont method
while (abs(eps) > tol)
x(i+1)=x(i)-f(x(i))*(x(i)-x(i-1))/(f(x(i))-f(x(i-1)));
eps (i)= x(i+1)-0.56714329;
i = i+1;
x(i);
end
disp('______________________________________________________________')
disp( 'itertion x_value error')
disp('______________________________________________________________')
for i=1:5
fprintf('%f %f %f ',i,x(i),abs(eps(i)))
end
x; %roots of three functions
______________________________________________________________
itertion x_value error
______________________________________________________________
1.000000 0.000000 1.000000
2.000000 1.000000 0.029881
3.000000 0.537263 0.000134
4.000000 0.567010 0.000000
Secont method
______________________________________________________________
itertion x_value error
______________________________________________________________
1.000000 0.000000 1.000000
2.000000 1.000000 0.045557
3.000000 0.612700 0.003305
4.000000 0.563838 0.000027
5.000000 0.567170 0.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.