function [x,niter]=aitken(phi,x0,tol,nmax,varargin) %AITKEN Aitken's method. % [ALPHA,NITER]=AITKEN(PHI,X0) computes an % approximation of a fixed point ALPHA of function PHI % starting from the initial datum X0 using Aitken's % extrapolation method. The method stops after 100 % iterations or after the absolute value of the % difference between two consecutive iterates is % smaller than 1.e-04. PHI can be defined either % as an inline function or an anonymous function or % by an M-file. % [ALPHA,NITER]=AITKEN(PHI,X0,TOL,NMAX) allows to % define the tolerance on the stopping criterion and % the maximum number of iterations. if nargin == 2 tol = 1.e-04; nmax = 100; elseif nargin == 3 nmax = 100; end x = x0; diff = tol + 1; niter = 0; while niter < nmax & diff >= tol gx = feval(phi,x,varargin{:}); ggx = feval(phi,gx,varargin{:}); xnew = (x*ggx-gx^2)/(ggx-2*gx+x); diff = abs(x-xnew); x = xnew; niter = niter + 1; end if (niter==nmax & diff>tol) fprintf(['Fails to converge within maximum ',... 'number of iterations\n']); end return