2021-01-07 Matlab numerical analysis nonlinear equation root Newton method

Matlab numerical analysis of nonlinear equations seeking roots Newton's method


%牛顿法求非线性方程的根:
%   输入: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 file for newton

 

function y=fun3(x)y=x*cos(x)+2;%

 

Newton's derived function file

​​​​​​​

function y=dfun3(x)y=cos(x)-x*sin(x);

Calling program

x=newton(@fun3,@dfun3,2,1e-3)

 

Guess you like

Origin blog.csdn.net/qingfengxd1/article/details/112321832