Labels dont show up

1 view (last 30 days)
MC
MC on 24 Sep 2019
Edited: Shubham Gupta on 24 Sep 2019
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')

Answers (1)

Shubham Gupta
Shubham Gupta on 24 Sep 2019
Edited: Shubham Gupta on 24 Sep 2019
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 !

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!