Stuck at coding modified Newton-Rapson function
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
seoung gi Sin
am 10 Apr. 2022
Kommentiert: seoung gi Sin
am 10 Apr. 2022
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 Kommentare
Akzeptierte Antwort
Torsten
am 10 Apr. 2022
Bearbeitet: Torsten
am 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Assumptions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!