Plot 4 diagrams in one plot

2 visualizaciones (últimos 30 días)
Mark S
Mark S el 24 de Nov. de 2020
Comentada: Mark S el 24 de Nov. de 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:

Respuestas (1)

Alan Stevens
Alan Stevens el 24 de Nov. de 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
  5 comentarios
Alan Stevens
Alan Stevens el 24 de Nov. de 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
Mark S el 24 de Nov. de 2020
Many thanks for your help, now it looks very good. You are my hero :)

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by