matlab中用牛顿法求方程的近似解
(2010-08-11 22:28:51)
标签:
教育 |
分类: 学习 |
程序主要部分摘自百度知道:
disp(' [
n
xn
xn+1
fn+1
]');
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
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)可以得到较精确的解。
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;
while n<=N
end
if n==N+1
后一篇:为人处事还是有很多地方要学