Plot 4 diagrams in one plot

1 回表示 (過去 30 日間)
Mark S
Mark S 2020 年 11 月 24 日
コメント済み: Mark S 2020 年 11 月 24 日
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:

回答 (1 件)

Alan Stevens
Alan Stevens 2020 年 11 月 24 日
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 件のコメント
Alan Stevens
Alan Stevens 2020 年 11 月 24 日
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 2020 年 11 月 24 日
Many thanks for your help, now it looks very good. You are my hero :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by