function [ X, iters ] = fix_p( g, x0, prec, maxit )
% function [ X, iters ] = fix_p( g, x0, prec, maxit )
%   Solution of the eqution x=g(x) using fixed point iteration method
%
%   Input:
%       g - inline function
%      x0 - initial iteration
%       p - precision, optional, default value 10^-6
%   maxit - maximal number of iterations, optional, default value 50
%
%   Output:
%       X - approximate solution
%   iters - iterations

if nargin < 2
    error('Too few input arguments!');
end

% precision
if (nargin < 3)
    prec = 0.000001;
end

% maximal number of iterations
if (nargin < 4)
    maxit = 100;
end

X = g(x0);
iters=[x0 X];
n_it=1;		% number of iterations

while (abs(x0 - X) > prec ) & (abs((x0 - X)/X) > prec)	% estimate of absoute and relative error
     x0 = X;
      X = g(x0);
   n_it = n_it + 1;
  iters = [iters, X];

   if n_it > maxit
       warning('The maximum number of iterations has been exceeded!');
       break;
   end

end


end		% end of function

