I have the following code and I want to combine phase space plots into one single figure.
I have coded the functions, but I don’t know how to make MATLAB put them into one figure. As you see, it is the variables r, a, b, and d that changes. How do I combine them?
I also would like to plot the vector field of these phase space plots using the quiver command, but it just does not work.
%function lotkavolterra
% Plots time series and phase space diagrams.
clear all; close all;
t0 = 0;
tf = 20;
N0 = 20;
P0 = 5;
% Original plot
r = 2;
a = 1;
b = 0.2;
d = 1.5;
% Time series plots
lv = @(t,x)(lv_eq(t,x,r,a,b,d));
[t,NP] = ode45(lv,[t0,tf],[N0 P0]);
N = NP(:,1); P = NP(:,2);
figure
plot(t,N,t,P,' --');
axis([0 20 0 50])
xlabel('Time')
ylabel('predator-prey')
title(['r=',num2str(r),', a=',num2str(a),', b=',num2str(b),', d=',num2str(d)]);
saveas(gcf,'predator-prey.png')
legend('prey','predator')
% Phase space plot
figure
quiver(N,P);
axis([0 50 0 10])
%axis tight
% Change variables
r = 2;
a = 1.5;
b = 0.1;
d = 1.5;
%time series plots
lv = @(t,x)(lv_eq(t,x,r,a,b,d));
[t,NP] = ode45(lv,[t0,tf],[N0 P0]);
N = NP(:,1); P = NP(:,2);
figure
plot(t,N,t,P,' --');
axis([0 20 0 50])
xlabel('Time')
ylabel('predator-prey')
title(['r=',num2str(r),', a=',num2str(a),', b=',num2str(b),', d=',num2str(d)]);
saveas(gcf,'predator-prey.png')
legend('prey','predator')
% Phase space plot
figure
plot(N,P);
axis([0 50 0 10])
% Change variables
r = 2;
a = 1;
b = 0.2;
d = 0.5;
% Time series plots
lv = @(t,x)(lv_eq(t,x,r,a,b,d));
[t,NP] = ode45(lv,[t0,tf],[N0 P0]);
N = NP(:,1); P = NP(:,2);
figure
plot(t,N,t,P,' --');
axis([0 20 0 50])
xlabel('Time')
ylabel('predator-prey')
title(['r=',num2str(r),', a=',num2str(a),', b=',num2str(b),', d=',num2str(d)]);
saveas(gcf,'predator-prey.png')
legend('prey','predator')
% Phase space plot
figure
plot(N,P);
axis([0 50 0 10])
% Change variables
r = 0.5;
a = 1;
b = 0.2;
d = 1.5;
% Time series plots
lv = @(t,x)(lv_eq(t,x,r,a,b,d));
[t,NP] = ode45(lv,[t0,tf],[N0 P0]);
N = NP(:,1); P = NP(:,2);
figure
plot(t,N,t,P,' --');
axis([0 20 0 50])
xlabel('Time')
ylabel('predator-prey')
title(['r=',num2str(r),', a=',num2str(a),', b=',num2str(b),', d=',num2str(d)]);
saveas(gcf,'predator-prey.png')
legend('prey','predator')
% Phase space plot
figure
plot(N,P);
axis([0 50 0 10])
% FUNCTION being called from external .m file
%function dx = lv_eq(t,x,r,a,b,d)
%N = x(1);
%P = x(2);
%dN = r*N-a*P*N;
%dP = b*a*P*N-d*P;
%dx = [dN;dP];
Well, there are a few ways how multiple data series can be displayed in the same figure.
I will use a little example data set, together with corresponding colors:
First of all, when you want everything on the same axis, you will need the
holdfunction:You see that this is right in many cases, however, it can become cumbersome when the dynamic range of both quantities differ a lot (e.g. the
uvalues are smaller than 1, while thevvalues are much larger).Secondly, when you have a lot of data or different quantities, it is also possible to use
subplotto have different axes. I also used the functionlinkaxesto link the axes in the x direction. When you zoom in on either of them in MATLAB, the other will display the same x range, which allows for easier inspection of larger data sets.Subplots do waste some space, but they allow to keep some data together without overpopulating a plot.
Finally, as an example for a more complex method to plot different quantities on the same figure using the
plotyyfunction (or better yet: theyyaxisfunction since R2016a)This certainly looks crowded, but it can come in handy when you have a large difference in dynamic range of the signals.
Of course, nothing hinders you from using a combination of these techniques together:
hold ontogether withplotyyandsubplot.edit:
For
quiver, I rarely use that command, but anyhow, you are lucky I wrote some code a while back to facilitate vector field plots. You can use the same techniques as explained above. My code is far from rigorous, but here goes:When called as:
this yields:

If you want any more guidance, I found that link in my source very useful (albeit badly formatted).
Also, you might want to take a look at the MATLAB help, it is really great. Just type
help quiverordoc quiverinto MATLAB or use the links I provided above (these should give the same contents asdoc).