问题描述
function [ sol,flag ] = newtonx(sx,relerr,maxit,func)
% [SOL,FLAG]=NEWTONX(SX,RELERR,MAXIT,FUNC)
%
% Solves f(x)=0 using Newton’s method
%
% Input: scalar sx - starting point of the iteration
% small positive scalar relerr - desired relative error
% positive integer maxit - maximum no. of iterations permitted
% func: name of the program that defines f(x);
%
% Note: when running newtonx on the problem defined by func,you
% must enter @func (func with symbol @ attached) in the input line.
% The calling sequence for the program func must have the format
% [yval,yder]=func(x)
% where x is the point used to compute yval=f(x) and yder=f’(x).
%
% Output: scalar sol - solution found
% scalar flag - flag=0 indicates solution successfully found
% flag=1 indicates derivative too small; halt
% flag=2 indicates too many iterations; halt
k=0 ;
while (k<maxit)
k=k+1 ;
[yval,yder]=func(sx) ; % evaluate f(x) and f’(x)
if (abs(yder)<abs(yval)*sqrt(eps))
sol=[]; flag=1 ; return % derivative too small
end
sx = sx - yval / yder ; % take the Newton step
if (relerr == 0)
sol=sx ; flag=0 ; return % succesful termination
end
end
sol=[]; flag=2 ; % failure: too many steps
end
我需要有关合适的终止标准的帮助,目前,我有(relerr == 0),但我确定这是不正确的。任何帮助或提示都很棒,我不确定该怎么办。
解决方法
| sx-sx_ {上一页} | = | yval / yder |
因此,相对误差公式为| yval /(sx * yder)|或在matlab术语中为abs(yval /(sx * yder))
abs(yval /(sx * yder))