Stuck at coding modified Newton-Rapson function
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
seoung gi Sin
le 10 Avr 2022
Commenté : seoung gi Sin
le 10 Avr 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 commentaires
Réponse acceptée
Torsten
le 10 Avr 2022
Modifié(e) : Torsten
le 10 Avr 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Assumptions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!