%  -----------------------------------------------------------
%                Three-Dimensional Linear Example
%  -----------------------------------------------------------
function example_1

close all;
clear all;
clc;

%  ------- Parameters  -------
tmin        = 0.0;   
tmax        = 100.0;
h           = 0.1;
trans       = 10.0;
tspan_trans = [tmin:h:trans];
tspan       = [tmin:h:tmax];

%  ---------------  Integrate ODE  ----------------
V0 = [20,-10,-10]';
[t_trans,v_trans] = ode45(@rhs_ode,tspan_trans,V0);

V0    = v_trans(end,:)'; 
[t,v] = ode45(@rhs_ode,tspan,V0);


%  ---------------  Plot Results  --------------
xmin = -2.0;  
xmax = 2.0;

%  ------  Time Series  -------
figure(1);
subplot(3,1,1);
plot(t,v(:,1),'r','LineWidth',2);
ylabel('{x_1}');
axis([tmin tmax xmin xmax]);

subplot(3,1,2);
plot(t,v(:,2),'g','LineWidth',2);
ylabel('{x_2}');
axis([tmin tmax xmin xmax]);

subplot(3,1,3);
plot(t,v(:,3),'b','LineWidth',2);
ylabel('{x_3}');
axis([tmin tmax xmin xmax]);


figure(2);
plot(t,v(:,1),t,v(:,2),t,v(:,3));
legend('x_1','x_2','x_3'); 



%  ------  Phase-Space  -------
figure(3);
plot3(v(:,1),v(:,2),v(:,3));
xlabel('x1'); ylabel('x2'); zlabel('x3');
grid on;


function f = rhs_ode(t,v)

  % --------  Matrix A  ---------
  A = [-0.25 3.0 0; -3 -0.25 0; 0 0 -0.2];
  x = v;
  
  %  --------  Write RHS of ODEs  --------
  f = A*x;