function xnew=mnhf_fpi(Fun,xold,tol,Nmax) %MNHF_FPI Fixed point algorithm for computing the solution to x=g(x). % % Fun - name of the external function, g(x). % xold - initial estimate of the root % tol - tolerance % Nmax - maximum number of iterations % % Usage mnhf_fpi(@poly1,1,1e-8,1000); % Check inputs. if nargin < 4, Nmax = 1000; end if nargin < 3, tol = 1e-8; end if (length(xold) ~= 1) error('Please provide one initial guess') end delta = 1; % Can select any value with |delta| > tol. count = 1; % Initialize counter. while abs(delta)>tol xnew = feval(Fun,xold); % Compute new root estimate. fprintf('Iteration=%2.0f\t, Current root estimate=%12.8f\n',count,xnew) delta = xnew-xold; count = count+1; % Increment counter. xold = xnew; if count > Nmax error('Maximum number of iterations exceeded.\n\n') end end