Labels dont show up

조회 수: 1 (최근 30일)
MC
MC 2019년 9월 24일
편집: Shubham Gupta 2019년 9월 24일
Hello,
Why does only the label for the last curve shows up? How do I get the labels to all show up on the same plot? I am graphing 3 different Euler's approximations and then an exact solution for an ODE on the same plot. Also how do I label the axis ?
f = @(t,y) t*y-y;
y0 = 0.5;
t = 0:0.2:1;
[t,y1] = euler(f,t,y0);
plot(t,y1)
legend('Euler 0.2')
hold on
t = 0:0.1:1;
[t,y3] = euler(f,t,y0);
plot(t,y3)
legend('Euler 0.1')
t = 0:0.05:1;
[t,y4] = euler(f,t,y0);
plot(t,y4)
legend('Euler 0.05')
[t,y2] = ode45(f,[0 10],y0);
plot(t,y2,'LineWidth',3)
legend('Exact solution')

답변 (1개)

Shubham Gupta
Shubham Gupta 2019년 9월 24일
편집: Shubham Gupta 2019년 9월 24일
One way to do this :
f = @(t,y) t*y-y;
y0 = 0.5;
t = 0:0.2:1;
[t,y1] = euler(f,t,y0);
plot(t,y1,'DisplayName','Euler 0.2');
hold on
t = 0:0.1:1;
[t,y3] = euler(f,t,y0);
plot(t,y3,'DisplayName','Euler 0.1');
t = 0:0.05:1;
[t,y4] = euler(f,t,y0);
plot(t,y4,'DisplayName','Euler 0.05');
[t,y2] = ode45(f,[0 10],y0);
plot(t,y2,'LineWidth',3,'DisplayName','Exact solution');
legend show
I hope it helps !

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by