Plotting 2 graphs, in the same window (not using subplot)

I have read several questions/answers regarding plotting more than one graph in the same window using subplot. However, our instructor explicitly advised that we are not to use subplot.
Here is my code. I'm using an .m function file that I am calling from Live Editor. Only the second graph is displayed; the first graph won't display at all.
function LAB04ex1
t0=0; tf=50; y0=[-1;0];
[t,Y]=ode45(@f,[t0,tf],y0,[]);
y=Y(:,1);
v=Y(:,2);
figure(1);
plot(t,y,'b-+');
plot(t,v,'ro-');
xlabel('t'); ylabel('v(t)=y''t');
legend('y(t)','v(t)=y''(t)'); %add a legend
xlabel('t'); ylabel('y');
ylim([-4.8,4.8]); %adjust the y-limits for plot 1
grid on;
figure(2);
plot(y,v); axis square; xlabel('y'); ylabel('v');
xlim([-4,4]); ylim([-4.8,4.8]) %adust the y-limits for plot 2
grid on;
end
%------------------------------------------------------------
function dYdt = f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; 8*sin(t)-4*v-3*y];
end
Here are the instructions:
Any help, feedback or advice would be greatly appreciated. Thank you.

回答 (1 件)

Star Strider
Star Strider 2026 年 2 月 15 日 1:08

1 投票

If you are not supposed to use subplot, the only other option that comes quickly to mind is to use tiledlayout.
Your code would then become --
LAB04ex1
Warning: Ignoring extra legend entries.
function LAB04ex1
t0=0; tf=50; y0=[-1;0];
[t,Y]=ode45(@f,[t0,tf],y0,[]);
y=Y(:,1);
v=Y(:,2);
figure
tiledlayout(1,2)
nexttile
plot(t,y,'b-+');
plot(t,v,'ro-');
xlabel('t'); ylabel('v(t)=y''t');
legend('y(t)','v(t)=y''(t)'); %add a legend
xlabel('t'); ylabel('y');
ylim([-4.8,4.8]); %adjust the y-limits for plot 1
grid on;
nexttile
plot(y,v); axis square; xlabel('y'); ylabel('v');
xlim([-4,4]); ylim([-4.8,4.8]) %adust the y-limits for plot 2
grid on;
axis('equal') % <- ADDED
end
%------------------------------------------------------------
function dYdt = f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; 8*sin(t)-4*v-3*y];
end
.

7 件のコメント

Alexis
Alexis 2026 年 2 月 15 日 1:45
Yes, but now the problem is I need both y(t) and v(t) to show up on the same graph in plot 1.
Walter Roberson
Walter Roberson 2026 年 2 月 15 日 1:48
Note: the warning about extra legend entries is because you have
plot(t,y,'b-+');
plot(t,v,'ro-');
without a "hold on", so the second plot replaces the first instead of adding onto the first.
Star Strider
Star Strider 2026 年 2 月 15 日 2:12
@Walter Roberson -- I didn't see that. Thank you!
Alexis
Alexis 2026 年 2 月 15 日 2:17
So, I added a "hold on" in between those two entries... and then added "hold off" before the phase plot. It's still not properly displaying the first plot.
I then combined the two entries above and got: plot(t,y,'b-+',t,v,'ro-'). It's still not displaying the correct output.
Star Strider
Star Strider 2026 年 2 月 15 日 2:40
The line style isn't the same as in the examples (they do not use markers, only lines), however it otherswise looks correct to me.
LAB04ex1
function LAB04ex1
t0=0; tf=50; y0=[-1;0];
[t,Y]=ode45(@f,[t0,tf],y0,[]);
y=Y(:,1);
v=Y(:,2);
figure
tiledlayout(1,2)
nexttile
plot(t,y,'b-+');
hold on
plot(t,v,'ro-');
hold off
xlabel('t'); ylabel('v(t)=y''(t)');
legend('y(t)','v(t)=y''(t)'); %add a legend
xlabel('t'); ylabel('y');
ylim([-4.8,4.8]); %adjust the y-limits for plot 1
grid on;
nexttile
plot(y,v); axis square; xlabel('y'); ylabel('v');
xlim([-4,4]); ylim([-4.8,4.8]) %adust the y-limits for plot 2
grid on;
axis('equal') % <- ADDED
end
%------------------------------------------------------------
function dYdt = f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; 8*sin(t)-4*v-3*y];
end
.
Alexis
Alexis 2026 年 2 月 15 日 5:03
I changed my code so that, instead of 'figure(1)' and 'figure(2)', I just used the word 'figure'. I kept everything the same. Now, it runs. Thank you ALL for your support.
Star Strider
Star Strider 2026 年 2 月 15 日 11:40
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

製品

リリース

R2025b

タグ

質問済み:

2026 年 2 月 15 日 0:33

コメント済み:

2026 年 2 月 15 日 11:40

Community Treasure Hunt

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

Start Hunting!

Translated by