我有这个牛顿法的MATLAB代码,我试图为它编写一个修改版本来创建Halley的方法。代码是
function root = newton(fname,fdname, fd2name, x,xtol,ftol,n_max,display)
% Newton's Method.
%
% input: fname is a string that names the function f(x).
% fdname is a tring that names the derivative f'(x).
% x is the initial point
% xtol and ftol are termination tolerances
% n_max is the maximum number of iteration
% display = 1 if step-by-step display is desired,
% = 0 otherwise
% output: root is the computed root of f(x)=0
%
n = 0;
fx = feval(fname,x);
if display,
disp(' n x f(x)')
disp('------------------------------------------------------')
disp(sprintf('%4d %23.15e %23.15e', n, x, fx))
end
if abs(fx) <= ftol
root = x;
return
end
for n = 1:n_max
fdx = feval(fdname,x);
d = fx/fdx;
x = x - d;
fx = feval(fname,x);
if display,
disp(sprintf('%4d %23.15e %23.15e', n, x, fx)), end
if abs(d) <= xtol | abs(fx) <= ftol
root = x;
return
end
end
root = x;
我知道我必须更改for循环中的代码。
所以我改变了:
fd2x = feval(fd2name, x)
d = 2*fx*fdx/(2*fdx.^2 - fxfd2x)
这有意义吗?我还需要改变什么?