function [x, rate] = fixedpoint( f, x, a, tol, maxIter, verbose = false )
  % ----------------------------------------------------------------------
  % Jonathan Senning <jonathan.senning@gordon.edu>
  % Gordon College
  %
  % Usage: x = fixedpoint(@f, x, a, tol, maxIter[, verbose])
  %        or
  %        [x,rate] = fixedpoint(@f, x, a, tol, maxIter[, verbose])
  %
  % This function performs a fixed point iteration to solve f(x) = 0.
  % It constructs a function g(x) = x + a * f(x) so that p = g(p) when
  % f(p) = 0.  The value of "a" is choosen to speed convergence -- it
  % should make g'(p) = 0 where p is the solution of f(p) = 0.
  %
  % This function requires that the function f(x) be already defined.
  % ----------------------------------------------------------------------

  if ( nargin ~= 5 && nargin ~= 6 )
    fprintf( '\aUsage: ' );
    fprintf( '[x,rate] = fixedpoint(@f, x, a, tol, maxIter[, verbose])\n' );
    return
  end

  % These extra points are needed to start off the error calculation which
  % compares estimates of solutions just computed with previous estimates.

  x1 = x + 8.0 * tol;
  x0 = x + 4.0 * tol;

  k = 0;

  if verbose
    fprintf( '     k  Estimate of root  Est. diff.\n' );
    fprintf( '    -- ------------------ ----------\n' );
  end

  while ( k <= maxIter && abs( x - x0 ) >= tol )
    x2 = x1;
    x1 = x0;
    x0 = x;
    x = x + a * f( x );
    if verbose
      fprintf( '    %2d %18.11e %10.4e\n', k, x, abs( x - x0 ) );
    end
    k = k + 1;
  end

  if ( k > maxIter )
    fprintf( 'Error: exceeded %d iterations\n', maxIter );
  end

  rate = log( abs((x - x0) / (x0 - x1)) ) / log( abs((x0 - x1) / (x1 - x2)) );

end
