How do I add a legend to my plot?

2 views (last 30 days)
%Define the variables using syms - syms creates symbolic variables
syms y(t) y0
%define the ordinary differential equation
ode = diff(y,t) == cos(t^2);
%solve the differential equation using dsolve
ysol = dsolve(ode,y(0)==y0);
%Create a figure called Task 1
figure ('Name','Task 1')
%Pick 3 different initial conditions for which the solution exists
%Conditions are the y values (y values are 1, 1.5, and 3 at varying t
%values. See the t values below.
conds = [1 1.5 3]'; %some values of y0
f = matlabFunction(subs(ysol,y0,conds));
t = linspace(0,50);
y = f(t);
%plotting the 3 equations
plot(t,y,'linewidth',2)
title('Symbolic Solutions')
xlabel('t')
ylabel('y(t)')
grid on

Accepted Answer

Star Strider
Star Strider on 31 Jul 2019
To add the legend, either use sprintfc (undocumented although quite useful):
lgndc = sprintfc('IC = %.1f',conds);
legend(lgndc, 'Location','E')
or compose:
lgndc = compose('IC = %.1f',conds);
legend(lgndc, 'Location','E')
both produce the same result.
Put these lines after your ‘grid on’ call.
  14 Comments
Vincenzo  Dragone
Vincenzo Dragone on 31 Jul 2019
Star Strider,
Would you be able to help me with another question?
Using Euler's Method, I need to approximate the solution to the IVP over some t range which I get to choose and then plot it.
My initial condition is y(0)=1 like in Task 1 code.
Here is all the code I have so far.
%% Task 1: Solve the ODE using the Symbolic Math Package and dsolve()
%Define the variables using syms - syms creates symbolic variables
syms y(t) y0 y1 h ode y2
%define the ordinary differential equation
ode = diff(y,t) == cos(t^2);
%solve the differential equation using dsolve
ysol = dsolve(ode,y(0)==y0);
%Create a figure called Task 1
figure ('Name','Task 1')
%Pick 3 different initial conditions for which the solution exists
%Conditions are the y values (y values are 1, 1.5, and 3 at varying t
%values. See the t values below.
conds = [1 1.5 2]'; %some values of y0
f = matlabFunction(subs(ysol,y0,conds));
t = linspace(0,5);
y = f(t);
%plotting the 3 equations
plot(t,y,'linewidth',2)
title('Symbolic Solutions')
xlabel('t')
ylabel('y(t)')
grid on
lgndc = sprintfc('IC = %.1f',conds);
legend(lgndc, 'Location','E')
%% Task 2: Euler's Method
%Pick a single initial condition from Task 1
%The initial condition we will use is y(0)=1
%Use Euler's method to approximate the solution to this IVP over some t
%range which you choose
%Create a figure called Task 2
figure ('Name','Task 2')
%The range of t is from 0 to 1
%We are using the initial condition that y(0)=1, but we want to know y(t)
%when t=1
%In other words we want to find y(1)
y = y0;
yout = y;
t0 = 0;
h = 0.5;
tfinal = 50;
for t= t0 : h : tfinal-h
y = y + h*ode;
yout = [yout;y]
end
Star Strider
Star Strider on 31 Jul 2019
Jim Riggs already appears to have answered that to your satisfaction: How do you use Euler's Method to approximate the solution?
I doubt that I could improve on his Answer:.

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!