function Rossler
% main program
global hndl1 hndl2 hndl3

t=0; tmax=200*pi; h=0.1; x=[0.0;3.0; 0.0]; L=20;
X=x*ones(1,L);

GI(x);

while t<tmax
   [t, x]=RK(t,x,h);
   X=[x X(:,1: L-1)];
   set(hndl1,'xdata',X(1,1),'ydata',X(2,1),'zdata',X(3, 1));
   set(hndl2,'xdata',X(1,1:5),'ydata',X(2,1:5),'zdata',X(3, 1:5));
   set(hndl3,'xdata',X(1,L-1:L),'ydata',X(2,L-1:L),'zdata',X(3, L-1:L));
   drawnow;
end
%end of main

% Rossler equation
function dx=fn(t,x)
dx=[-(x(2)+x(3)); x(1)+0.35*x(2); 0.4*x(1)+x(1)*x(3)-4.5*x(3)];

% Runge Kutta method
function [t, x]=RK(t,x,h)
   f1=fn(t,x);
   f2=fn(t+h/2,x+h*f1/2);
   f3=fn(t+h/2,x+h*f2/2);
   f4=fn(t+h,x+h*f3);
   t=t+h;
   x=x+h*(f1+2*(f2+f3)+f4)/6;
   
% Graphics initialization
function GI(x)
global hndl1 hndl2 hndl3
  clf;
  hndl1=line('color','r','linestyle','.','markersize',20,...
     'erase','xor','xdata',x(1),'ydata',x(2),'zdata',x(3));
  hndl2=line('color','r','linestyle','-','linewidth',1,...
     'erase','none','xdata',[],'ydata',[],'zdata',[]);
  hndl3=line('color','b','linestyle','-',...
     'erase','none','xdata',[],'ydata',[],'zdata',[]);
  axis([-6 10 -8 8 0 8]);
  box on;
  title('Phase Portrait: Rossler equation');
  xlabel('x(t)');
  ylabel('y(t)');
  zlabel('z(t)');
  grid on;
  axis square;
  figure(1);

