Stuck at coding modified Newton-Rapson function
조회 수: 3 (최근 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에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!