It is required to use a program to finish this question Only
Solution
The code is implemented in MATLAB
It includes 4 files
f.m........... implements the given function sin(x)-x
df.m............implements its derivative
mysecant.m...........implements the secant method code
mynewton.m ............implemnts the newton method
Copy the code into 4 different files and save them in the matlab home folder
%%%Code function f(x)
function y= f(x)
y=sin(x)-x;
end
%code function df.m
function y= df(x)
y=cos(x)-1;
end
%%mysecant.m
p0=pi/4; %initial conditions
p1=3*pi/4; %initial conditions
Pn=[]; %solution sequence initially empty
while 1
p2=p0-(f(p0)*(p1-p0))/(f(p1)-f(p0)); %f(x) is the given function implemnted in a separate file f.m
if abs(p2-p1) < 10^-7 %check accuracy
break
end
p0=p1;
p1=p2;
Pn=[Pn,p2]; %solution sequence
end
plot(Pn); %plot solution sequence
disp(Pn\')
%%mynewton.m
p0=pi/4; %initial conditions
Pn=[]; %solution sequence initially empty
while 1
p1=p0-(f(p0)/df(p0)); %f(x) is the given function implemnted in a separate file f.m
%df(x) is the derivative function implemented
%in a separate file df.m
if abs(p1-p0) < 10^-7 %check accuracy
break
end
p0=p1;
Pn=[Pn,p1]; %solution sequence
end
plot(Pn); %plot solution sequence
disp(Pn\')
