(It is required to use a program to finish this question. Only programs written
ID: 3405446 • Letter: #
Question
(It is required to use a program to finish this question. Only programs written in C/C++/Mat lab/Octave is acceptable. Hand in the code via CCLE (please put all of them in one file). Print your tabulated results and graphs. Please also print the code out and hand in the hard copy of the code.) Solve the following equation by the following methods: sin(x) - x = 0 on [0, pi/2] Write a complete program to implement the secant method to solve the equation with an accuracy of at least 10^-7 (using |p_n - p_n-1|) with initial values p_0 and p_1 as pi/4 and 3 pi/8 respectively. Plot and print the graph of the sequence {p_n} generated by the secant method. Write a complete programme to implement the Newton's Method to solve the equation with an accuracy of at least 10^-7 (using |p_n - p_n-1|) with initial value p_0 as pi/4. Plot and print the graph of the sequence {p_n} generated by Newton's method.Explanation / Answer
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')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.