% this codes makes the first several of the phase portraits for the
% exercises

function make_de_plots
global A
set(0,                           ...
   'defaultaxesfontsize', 20,   ...
   'defaultaxeslinewidth', 1.2, ...
   'defaultlinelinewidth', 2.0, ...
   'defaultpatchlinewidth', 0.7); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% exercise a: saddle point
A = [0,1;1,0]
eig(A)
 
% create some phase portraits
tstep = 0.01; %time step size
t_end = 5; % length of time interval
tspan = [0:tstep:t_end];

 
s = [-1.13;1.1]; % initial data for integration
    
[T,S] = ode23s(@deRHS,tspan,s); 
 
figure(1)
plot(S(:,1),S(:,2),-S(:,1),-S(:,2),S(:,2),S(:,1),-S(:,2),-S(:,1),'linewidth',2)
 
hold on
 
xlabel('u','fontsize',20)
ylabel('v','fontsize',20)

plot([0, 1],[0,1],[0,1],[0,-1],[0,-1],[0,1],[0,-1],[0,-1],'linewidth',2)
plot([-5,5],[0,0],'--',[0,0],[-5,5],'--',0,0,'*','linewidth',2) % nullclines
axis([-1 1 -1 1])
annotation('textarrow',[0.3 .3],[0.56 0.49],'linewidth',2)
annotation('textarrow',[0.75 .75],[0.49 0.56],'linewidth',2)
annotation('textarrow',[0.55 .49],[0.3 0.3],'linewidth',2)
annotation('textarrow',[0.49 .55],[0.75 0.75],'linewidth',2)
annotation('textarrow',[0.59 .65],[0.6 0.66],'linewidth',3)
annotation('textarrow',[0.65 0.59 ],[0.39 0.45],'linewidth',3)
 annotation('textarrow',[0.39 .45],[0.66 0.6],'linewidth',3)
tstep = 0.01;
t_end = 50;
tspan = [0:tstep:t_end];
% unstable spiral
A = [0.1,1;-1,0.1];% the matrix rhs

s = [0.02;0.02]; % initial data
    
[T,S] = ode23s(@deRHS,tspan,s); 
 figure(2)
plot(S(:,1),S(:,2),'linewidth',2)
 xlabel('u','fontsize',20)
ylabel('v','fontsize',20)
hold on
plot([-10,10],[1,-1],'--',[-1,1],[-10,10],'--',0,0,'*','linewidth',2) % nullclines
 axis([-1 1 -1 1])
 
annotation('textarrow',[0.25 .25],[0.53 0.59],'linewidth',2)
annotation('textarrow',[0.75 .75],[0.54 0.47],'linewidth',2)
annotation('textarrow',[0.52 .46],[0.3 0.3],'linewidth',2)
annotation('textarrow',[0.51 .57],[0.7 0.7],'linewidth',2)

  
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% stable spiral
A = [-0.1,1;-1,-0.1]
eig(A)
s = [-.4;-1];
    
[T,S] = ode23s(@deRHS,tspan,s); 
 
figure(3)
plot(S(:,1),S(:,2),'linewidth',2)
 
hold on
plot([-10,10],[-1,1],'--',[-1,1],[10,-10],'--',0,0,'*','linewidth',2) %nullclines
hold off
axis([-1 1 -1 1])
axis([-1 1 -1 1])
annotation('textarrow',[0.58 .52],[0.22 0.22],'linewidth',2)
annotation('textarrow',[0.47 .53],[0.78 0.78],'linewidth',2)
annotation('textarrow',[0.25 .25],[0.47 0.54],'linewidth',2)
annotation('textarrow',[0.73 .73],[0.58 0.51],'linewidth',2)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% stable node
A = [-1,0.2;0.3,-0.3]% the matrix rhs
eig(A)
s = [.4;-1]; %initial data
    
[T,S] = ode23s(@deRHS,tspan,s); 
 
figure(4)
plot(S(:,1),S(:,2),'linewidth',2)
 
hold on

s = [-1;0]; % more initial data
    
[T,S] = ode23s(@deRHS,tspan,s); 
 
figure(4)
plot(S(:,1),S(:,2),'linewidth',2)  % add a trajectory
 
hold on
s = [1;0]; %more initial data
    
[T,S] = ode23s(@deRHS,tspan,s); 
 
figure(4)
plot(S(:,1),S(:,2),'linewidth',2)  % add curve
s = [-1;1];
    
[T,S] = ode23s(@deRHS,tspan,s); 
 
figure(4)
plot(S(:,1),S(:,2),'linewidth',2)

 % add nullclines and arrows
plot([-1,1],[-5,5],'--',[-10,10],[-10,10],'--',0,0,'*','linewidth',2)  % nullclines
hold off
axis([-1 1 -1 1])
annotation('textarrow',[0.46 .46],[0.19 0.27],'linewidth',2)
annotation('textarrow',[0.57 .57],[0.84 0.76],'linewidth',2)
annotation('textarrow',[0.27 .33],[0.3 0.3],'linewidth',2)
annotation('textarrow',[0.72 .65],[0.7 0.7],'linewidth',2)

function s_prime=deRHS(t,s)  % right hand side for ode system
global A
 
s_prime = A*s;