# Plot 4 diagrams in one plot

1 view (last 30 days)
Mark S on 24 Nov 2020
Commented: Mark S on 24 Nov 2020
Hi, I want to plot 4 diagrams in one plot. Actual i have two plots with 4 different values:
clear ;clc
mu=[0 0.1 1 10];
tspan=[0 20*pi];
x0=0.5;dx0=0;
IC=[x0 dx0]; % initial conditions
for i=1:4
dxdt=@(t,x)[x(2);
mu(i)*(1-x(1)^2)*x(2)-x(1)];
[T,X]=ode45(dxdt,tspan,IC);
figure(1)
plot(T,X(:,1))
hold on
xlabel('t')
ylabel('x(t)')
figure(2)
plot(X(:,1),X(:,2))
hold on
xlabel('x(t)')
ylabel('v(t)')
end
figure(1)
legend('mu=0','mu=0.1','mu=1','mu=10')
figure(2)
legend('mu=0','mu=0.1','mu=1','mu=10')
I want 4 diagrams for each value in one window (like on the right side of my picture). I have tried to input this code: But this doesn't work very good:
ax1 = subplot(2,2,i);
hold on
grid on
box on
It should look something like this:

Alan Stevens on 24 Nov 2020
Like this?
mu=[0 0.1 1 10];
tspan=[0 20*pi];
x0=0.5;dx0=0;
IC=[x0 dx0]; % initial conditions
for i=1:4
dxdt=@(t,x)[x(2);
mu(i)*(1-x(1)^2)*x(2)-x(1)];
[T,X]=ode45(dxdt,tspan,IC);
if i==1 || i==3
subplot(2,2,i)
plot(T,X(:,1))
xlabel('t')
ylabel('x(t)')
else
subplot(2,2,i)
plot(X(:,1),X(:,2))
xlabel('x(t)')
ylabel('v(t)')
end
end

Alan Stevens on 24 Nov 2020
The following generates all the x vs t values. You could repeat for the v vs t or v vs x etc.
mu=[0 0.1 1 10];
tspan=[0 20*pi];
x0=0.5;dx0=0;
IC=[x0 dx0]; % initial conditions
for i=1:4
dxdt=@(t,x)[x(2);
mu(i)*(1-x(1)^2)*x(2)-x(1)];
[T,X]=ode45(dxdt,tspan,IC);
if i==1 || i==2
subplot(2,2,i)
plot(T,X(:,1))
xlabel('t')
ylabel('x(t)')
elseif i ==3
subplot(2,2,3)
plot(T,X(:,1))
xlabel('t')
ylabel('x(t)')
else
subplot(2,2,4)
plot(T,X(:,1))
xlabel('t'),ylabel('x(t)')
end
end
Alan Stevens on 24 Nov 2020
Or, (finally!):
mu=[0 0.1 1 10];
tspan=[0 20*pi];
x0=0.5;dx0=0;
IC=[x0 dx0]; % initial conditions
for i=1:4
dxdt=@(t,x)[x(2);
mu(i)*(1-x(1)^2)*x(2)-x(1)];
[T,X]=ode45(dxdt,tspan,IC);
if i==1 || i==2
figure(1)
subplot(2,2,i)
plot(T,X(:,1))
xlabel('t')
ylabel('x(t)')
figure(2)
subplot(2,2,i)
plot(T,X(:,2))
xlabel('t')
ylabel('v(t)')
elseif i ==3
figure(1)
subplot(2,2,i)
plot(T,X(:,1))
xlabel('t')
ylabel('x(t)')
figure(2)
subplot(2,2,i)
plot(T,X(:,2))
xlabel('t')
ylabel('v(t)')
else
figure(1)
subplot(2,2,4)
plot(T,X(:,1))
xlabel('t'),ylabel('x(t)')
figure(2)
subplot(2,2,4)
plot(T,X(:,2))
xlabel('t'),ylabel('v(t)')
end
end
Mark S on 24 Nov 2020
Many thanks for your help, now it looks very good. You are my hero :)