求根的方法有很多,前面介绍了用交叉法(Bracketing Method)求函数的根,本文介绍几种用开型法(Open Methods)求根的方法。但着重介绍牛顿-拉普森(Newton-Raphson)法。在Bracketing Methods中,一般需要两个初始的猜想值,用于迭代的起始。但是在Open Methods中,只需要一个起始值或者两个但是不需要让它们分布在精确值对的两侧。但是Open Methods方法的缺点是,在有些情况下他不能得到收敛的值,如下图:
1、牛顿-拉普森(Newton-Raphson)方法
牛顿-拉普森迭代算法的推导,如图:
斜率slope能通过如下方程得到:
其中,xi为开始的假设值。则下一个x值的求取形式如下:
这就是牛顿-拉普森迭代公式。
举个例子:
,初始猜想值x = 0.5,则求根迭代公式为。通过excel,可以得到求根迭代过程结果如下:
虽然第一次预测得到很不好的结果,但是最后还是得到了正确的值。只是迭代过程变得很慢。牛顿-拉普森对存在多个根和下图几种情况应用的时候,收敛性很不好。
下面贴上代码:
function [root , ea, iter] = newtraph(func, dfunc, xr, es, maxit, varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin < 3
error('At least 3 input argument required');
end
if nargin < 4 | isempty(es)
es = 0.0001;
end
if nargin < 5 | isempty(maxit)
maxit = 50;
end
iter = 0;
while(1)
xrold = xr;
xr = xr - func(xr)/dfunc(xr);
iter = iter + 1;
if xr ~= 0
ea = abs((xr - xrold)/xr) * 100;
end
if ea <= es | iter >= maxit
break;
end
end
root = xr;
除此之外,还有几种重要的Open Methods求根方法:
2、Secant Method 和 Modified Secant Method
3、Brent's Method(结合了Inverse Quadratic Interpolation 和 Secant Method)
4、多项式逼近
(具体介绍详见参考书籍第六章)
参考书籍: Applied Numerical Methods with Matlab for Engineers and Scientists_3ed