% ========================================================================
% Nonlinear Dynamical Systems
% date : January 23,2006, Monday
% IES: http://www.cavs.msstate.edu/hse/ies/projects/nsf_nonlinear
% Created by: Saurabh Prasad
% ========================================================================
%   Generating a time-series (scalar and vector) from the Rossler system
%    x = -(y+z) ;
%    y = (x + a*y);
%    z = (b + (x-c)*z);
%    Required Subroutines:
%    |----> rx.m, ry.m, rz.m 



clear all;

dt= 0.01;            % inegrate step
NN = 500000;         % Number of samples
T = 1;

x = .5*ones(NN+1,1); % initial conditions
y = .5*ones(NN+1,1); % initial conditions
z = .5*ones(NN+1,1); % initial conditions

a = 0.15;
b = 0.2;
c = 10;


% The following for loop uses the Runge-Kutta Numerical Integration method
% to obtain the data stream.
for n = 1:NN,
    xx = x(n);
    yy = y(n);
    zz = z(n);

    xk1 = dt*rx(xx,yy,zz,a,b,c);
    yk1 = dt*ry(xx,yy,zz,a,b,c);
    zk1 = dt*rz(xx,yy,zz,a,b,c);

    xk2 = dt*rx(xx+xk1/2,yy+yk1/2,zz+zk1/2,a,b,c);
    yk2 = dt*ry(xx+xk1/2,yy+yk1/2,zz+zk1/2,a,b,c);
    zk2 = dt*rz(xx+xk1/2,yy+yk1/2,zz+zk1/2,a,b,c);

    xk3 = dt*rx(xx+xk2/2,yy+yk2/2,zz+zk2/2,a,b,c);
    yk3 = dt*ry(xx+xk2/2,yy+yk2/2,zz+zk2/2,a,b,c);
    zk3 = dt*rz(xx+xk2/2,yy+yk2/2,zz+zk2/2,a,b,c);

    xk4 = dt*rx(xx+xk3,yy+yk3,zz+zk3,a,b,c);
    yk4 = dt*ry(xx+xk3,yy+yk3,zz+zk3,a,b,c);
    zk4 = dt*rz(xx+xk3,yy+yk3,zz+zk3,a,b,c);

    nn = n+1;
    x(nn) = xx + xk1/6 + xk2/3 +xk3/3 +xk4/6;
    y(nn) = yy + yk1/6 + yk2/3 +yk3/3 +yk4/6;
    z(nn) = zz + zk1/6 + zk2/3 +zk3/3 +zk4/6;
end


plot(x,y,'--');

x_new = zeros(NN/10,1);
y_new = zeros(NN/10,1);
z_new = zeros(NN/10,1);

for t = 1 : NN/10-1
    x_new(t) = x((10*t+1));
    y_new(t) = y((10*t+1));
    z_new(t) = z((10*t+1));
end;

start=10001;

% saving time series data for x variable
%

rossler_x = x_new(start:end);
rossler_full = [x_new(start:end) y_new(start:end) z_new(start:end)];
save rossler_x.mat rossler_x;
save rossler_full.mat rossler_full;
