Stuck at coding modified Newton-Rapson function
15 views (last 30 days)
Show older comments
I made the function of Modified Newton-Rapson method .
But when I put function as 3*x^2-10*x+7, xi=0, error=0.01, and the result comes NaN.
What is wrong in this code. Please help.
function root=mnr(func, xi,error)
i=1;
xrold=0;
xr=xi
a=[];
fprintf('iteration root error\n')
while(1)
xrold=xr;
mul=subs(func,xr)/subs(diff(func),xr);
xr=xr-subs(mul,xr)/subs(diff(mul),xr);
a(i)=xr;
er=(xr-xrold)/xr;
fprintf(' %d %e %f\n', i, double(xr),double(er))
i=i+1;
if abs(er)<=error
break
end
end
0 Comments
Accepted Answer
Torsten
on 10 Apr 2022
Edited: Torsten
on 10 Apr 2022
Why do you call it "Modified Newton-Raphson" ?
I do not quite understand what the two lines
mul=subs(func,xr)/subs(diff(func),xr);
xr=xr-subs(mul,xr)/subs(diff(mul),xr);
are supposed to do.
syms x
func = 3*x^2-10*x+7;
xi = 0;
error = 1e-6;
root = mnr(func, xi,error)
function root=mnr(func, xi,error)
syms x
df= diff(func,x);
i=1;
xrold=0;
xr=xi
a=[];
fprintf('iteration root error\n')
while(1)
xrold=xr
mul=double(subs(func,xr))/double(subs(df,xr));
xr=xr-mul;
%a(i)=xr;
er=(xr-xrold)/xr;
%fprintf(' %d %e %f\n', i, double(xr),double(er))
%i=i+1;
if abs(er)<=error
break
end
end
root = xr;
end
More Answers (0)
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!