2021-01-07 matlab数值分析 非线性方程求根 牛顿法

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

newton的函数文件

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

newton的导函数文件

​​​​​​​

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

调用程序

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

猜你喜欢

转载自blog.csdn.net/qingfengxd1/article/details/112321832
今日推荐