newton(计算方法)
鲁鸿
2023-12-01
% newton.m
%x=newton(inline('x^3-2*x-5'),inline('3*x^2-2'),0.5,100,1e-6)
%function [y,z]=newton(fv,df,x0,n,tol)
function [y]=newton(fv,df,x0,tol)
x(1)=x0; b=1; k=1;
while or(k==1,abs(b)>tol*abs(x(k)))
x(k+1)=x(k)-feval(fv,x(k))/feval(df,x(k));
b=x(k+1)-x(k);
k=k+1;
%if(k>n)
% error('Error: Reached maximum iteration times');
%end
end
y=x(k-1);
%if nargout>1
% z=k-1;
end
// newton_run.m /
clear all
%x=newton(inline('x^3-2*x-5'),inline('3*x^2-2'),0.5,100,1e-6)
%[xx,k]=newton(inline('x^3-2*x-5'),inline('3*x^2-2'),0.5,100,1e-6)
x=newton(inline('x^3-2*x-5'),inline('3*x^2-2'),0.5,1e-6)