function fixedPoint(f,x0);
% Draws a fixed point diagram of function f starting in x0
%
% fixedPoint(f,x0)
%  
% Examples are:  
%  
% matlab> fixedPoint('0.25+0.65*x',0.1)
%  
% matlab> fixedPoint('0.5*(1-cos(x*pi))', 0.6)
%
% For a demonstration just use
%  clf; 
%  subplot(1,2,1); fixedPoint('0.25+0.6*x', 0.1); 
%  subplot(1,2,2); fixedPoint('0.5*(1-cos(x*pi))', 0.6);  
  
  hold on;
  axis([0 1 0 1]); xlabel('x'); ylabel('T(x)'); 
  title(['x=T(x)=' f]);
  plot(0:0.01:1,0:0.01:1,'r--');
  x = 0:0.01:1; 
  plot(x,eval(f),'b-');
  
  x=x0;
  for t=1:10
    xn = eval(f);
    
    arrow([x x], [x xn], 0.001+0.15*sqrt(abs(xn-x)));
    arrow([x xn], [xn xn], 0.001+0.15*sqrt(abs(xn-x)));    
    x = xn;
  end;

function [L_h,F_h]=arrow(Start,Stop,ALen,Base,SLen,Width,Color,BLineStyle);
%---------------------------------------------------------
% arrow            plot a nice arrow
%                plot an arrow with the following properties:
%
%                A-D: SLen                    ^ A
%                A-C: ALen                   / \       
%                A  : Stop point            /   \
%                B  : Start point          /     \
%                E-F: Base                / ..D.. \
%                                       E/..  |C ..\F
%                                             |
%                                             |B
% Input  : - Start points. [X Y]
%          - Stop pouints. [X Y]
%          - Arrow length. (default is 0.05 of total length).
%          - Base length.  (default is 1/2 of arrow length).
%          - Short length. (default is like Arrow length).
%          - line width. (default is 0.5).
%          - color. (default is 'k').
%          - boundary line style. (default is 'none').
% Output : - Line handle.
%          - Arrow (filled) handle.
% By : Eran O. Ofek         October 1999
%---------------------------------------------------------

Length = sqrt((Stop(1)-Start(1)).^2+(Stop(2)-Start(2)).^2);

if (nargin==8),
   % no default.
elseif (nargin==7),
   BLineStyle = 'none';
elseif (nargin==6),
   Color      = 'g';
   BLineStyle = 'none';
elseif (nargin==5),
   Width      = 1.0;
   Color      = 'g';
   BLineStyle = 'none';
elseif (nargin==4),
   SLen       = ALen;
   Width      = 1.0;
   Color      = 'g';
   BLineStyle = 'none';
elseif (nargin==3),
   Base       = 0.5.*ALen;
   SLen       = ALen;
   Width      = 1.0;
   Color      = 'g';
   BLineStyle = 'none';
elseif (nargin==2),
   ALen       = 0.05.*Length;
   Base       = 0.5.*ALen;
   SLen       = ALen;
   Width      = 1.0;
   Color      = 'g';
   BLineStyle = 'none';
else
   error('illigal number of input arguments');
end


L_h=plot([Start(1),Stop(1)],[Start(2),Stop(2)],Color);
set(L_h,'LineWidth',Width);
hold on;
Theta  = atan2(Stop(2)-Start(2),Stop(1)-Start(1));


A(1) = Stop(1);
A(2) = Stop(2);
D(1) = A(1) - SLen.*cos(Theta);
D(2) = A(2) - SLen.*sin(Theta);

Phi  = atan(0.5.*Base./ALen);
BC   = sqrt(ALen.^2 + (0.5.*Base).^2);


E(1) = A(1) - BC.*cos(Theta  + Phi);
E(2) = A(2) - BC.*sin(Theta  + Phi);

F(1) = A(1) - BC.*cos(Theta  - Phi);
F(2) = A(2) - BC.*sin(Theta  - Phi);

F_h=fill([A(1);E(1);D(1);F(1)],[A(2);E(2);D(2);F(2)],Color);
set(F_h,'LineStyle',BLineStyle);
