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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by