function funciter()
% Find the stable point of f(x) = x using functional iteration

  xn = 2.8;       % This is the initial condition
  maxit = 75;     % Allow a maximum of maxit iterations
  tol = 1.0e-8;   % Stop when |x_{n+1} - x_n| < tol
%
% Use two vectors to store the approximations of the stable point and the errors
%
  rv = zeros(1, maxit);
  ev = zeros(1, maxit);
%
  err = 1.0;  % Initial error
  iter = 0;   % Iteration counter
%
  while iter < maxit && err > tol
    xnew = f(xn);
    err = abs(xnew - xn);
    iter = iter + 1;
%
    rv(iter) = xnew;
    ev(iter) = err;
%
    xn = xnew;
  end
%
% Check for convergence
%
  if iter == maxit
    fprintf(1, 'Functional iteration failed to converge in %d steps\n', maxit);
  else
    fprintf(1, 'Functional iteration converged in %d steps\n', iter);	
  end
%
% Plot results
%
  if iter < maxit
    semilogy(1:iter, ev(1:iter), 'b-o');
    axis([0 iter+1 1.0e-16 1]);
    xlabel('number of iterations');
    ylabel('estimated error');
%
    figure();
    plot(1:iter, rv(1:iter), 'k-*');
    xlabel('number of iterations');
    ylabel('approximate stable point');
  end
%
end

function out = f(x)
   out = (-x^3 + 21*x + 5) / 19.0;
end
