加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

matlab中用牛顿法求方程的近似解

(2010-08-11 22:28:51)
标签:

教育

分类: 学习
程序主要部分摘自百度知道:
function rtn=newton1(fx,dfx,x0,tol,N)
% Newton Method
% The first parameter fx is a external function with respect to viable x.
% The second parameter dfx is the first order diffential function of fx.
% x0 is initial iteration point.即解附近的一个值
% tol is the tolerance of the loop.
% N is the maximum number of iterations.
x=x0;f0=eval_r(fx);df0=eval_r(dfx);
n=0;
 disp('   [   n        xn          xn+1           fn+1  ]');
while n<=N
    x1=x0-f0/df0;
    x=x1;f1=eval_r(fx);
    X=[n,x0,x1,f1];
    disp(X);
    if abs(x0-x1)<tol
        rtn=x1;
        fprintf('The procedure was successful.')
        return
    else  
        n=n+1;
        x0=x1;f0=f1;
    end
end
if n==N+1
    rtn=x1;
    fprintf('the method failed after N iterations.\n '),
 end


    调用格式如:result=newton1(y,dydx,19.5,0.000001,10)

    用牛顿法时,往往包括两步:第一步用某种全局方法(比如画图的方法)得到近似解;第二步用牛顿法得到满足精度要求的精确解。
    比如,需要求出函数 y=(0.65-0.01*x)*(200*exp(0.025*x))-0.45*x = - (9*x)/20 - 2*exp(x/40)*(x - 65) 在x>0时的最大值,首先画图得到其最大值可能在 19.5 附近。由于其一阶导数为 f= - 2*exp(x/40) - (exp(x/40)*(x - 65))/20 - 9/20 ,令 f=0 不易得到解析解.所以用牛顿法 result=newton1(f,dfdx,19.5,0.000001,10)可以得到较精确的解。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有