ルーツを求める非線形方程式のMatlab数値分析ニュートンの方法
%牛顿法求非线性方程的根:
% 输入:fun--非线性函数;dfun--非线性函数导数;x0--初始值;tol--精度;
% 输出:x--非线性方程数值根
function [x,iter]=newton(fun,dfun,x0,tol)
format long
iter=1;
x=x0;
while iter<500
x=x-feval(fun,x)/feval(dfun,x);
if abs(feval(fun,x))<tol
break;
end
iter=iter+1;
end
ニュートンの関数ファイル
function y=fun3(x)
y=x*cos(x)+2;%
ニュートンの派生関数ファイル
function y=dfun3(x)
y=cos(x)-x*sin(x);
呼び出しプログラム
x=newton(@fun3,@dfun3,2,1e-3)