I am writing a MATLAB program that has to dfo the following. Use (a) the Newtoni
ID: 2997675 • Letter: I
Question
I am writing a MATLAB program that has to dfo the following.
Use (a) the Newtonian-Raphson method and (b) the modified secant method (=0.05) to determine the root of f(x) using an intial guess of x = 0.5825 and ?s = 0.01%. Explain your results.
This is what I have so far, but it doesn't work. Any help would be appreciated. Thanks
The function file is as follows:
function [x,y,n]=newtrapf(fn,gn,xl,tol,max)
% Function to find the root of a function fn using the false-position method
%Inputs are:-
% fn gn = funtion and derivative names in quotes, respectively
% xl = guesses which bracket the root
% tol = tolerance level
% max = maximum number of iterations allowed
%Outputs are:-
% x => desired root
% y => error in calculating root
% n => number of iterations
n=0;
error=1;
errora=1000;x=1000;
while errora > tol
fl=feval(fn,xl);
gl=feval(gn,xl);
xr=xl-fl/gl;
fr=feval(fn,xr);
xold=x;
xl=xr;y=fr;
x=xr;
errora=abs((x-xold)/(x+eps)*100);
error=abs(y);
n=n+1;
if n >= max
disp('Warning! Maximun number of iterations reached but not converged yet!')
break
end
end
The command lines are as follows:
syms x
fn=@(x)x^5-16.05*x^4+88.75*x^3-192.0375*x^2+116.35*x-31.6875;
gn=@(x)5*x^4-(321*x^3)/5+(1065*x^2)/4-(15363*x)/40+2327/20;
[x,y,n]newtrapf(fn,gn,0.5825,0.0001,3)
Explanation / Answer
% one end command was missing.
function [x,y,n]=newtrapf(fn,gn,xl,tol,max)
% Function to find the root of a function fn using the false-position method
%Inputs are:-
% fn gn = funtion and derivative names in quotes, respectively
% xl = guesses which bracket the root
% tol = tolerance level
% max = maximum number of iterations allowed
%Outputs are:-
% x => desired root
% y => error in calculating root
% n => number of iterations
n=0;
error=1;
errora=1000;x=1000;
while errora > tol
fl=feval(fn,xl);
gl=feval(gn,xl);
xr=xl-fl/gl;
fr=feval(fn,xr);
xold=x;
xl=xr;y=fr;
x=xr;
errora=abs((x-xold)/(x+eps)*100);
error=abs(y);
n=n+1;
if n >= max
disp('Warning! Maximun number of iterations reached but not converged yet!')
break
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.