牛頓迭代法的Matlab
阿新 • • 發佈:2020-12-28
function [k,X]=Newton(y,x0)
format long
syms x
m=zeros(1,100);
for i=1:100
s=x0-subs(y,x,x0)/subs(diff(y),x,x0);
if abs(s-x0)>1e-8
x0=s;
m(i)=x0;
else
m(i)=s;
break
end
end
k=i;
X=m(1,1:i);
結果顯示:
> [k,X]=Newton(x-log(x)-2,3)
k =
4
X =
3.147918433002165 3.146193440797909 3.146193220620586 3.146193220620582