Stuck at coding modified Newton-Rapson function
15 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Torsten
2022 年 4 月 10 日
編集済み: Torsten
2022 年 4 月 10 日
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Assumptions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!