Labels dont show up

1 次查看(过去 30 天)
MC
MC 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