Adding Labels to PDE Graph

I was wondering how to add a label to a graph of multiple PDEs.
I want to label each line of the graph differently.
The functions I have are as follows:
function [pl,ql,pr,qr] = heatbc(xl,ul,xr,ur,t)
pl = ul;
ql = 0;
pr = ur;
qr = 0;
end
function u0 = heatic(x)
u0 = x.*(1-x);
end
function [c,f,s] = heatpde(x,t,u,dudx)
c = 1;
f = dudx;
s = 0;
end
With the main code being as such:
x = linspace(0,1,10);
t = [0 0.1 0.2 0.3];
m = 0;
sol = pdepe(m,@heatpde,@heatic,@heatbc,x,t)
u = sol(:,:,1);
plot(x,u);
hold on;
So far the graph looks as such:
I would just like to know how I would code a legend that labels the purple, orange, red and blue lines individually.
Thanks in advance!

 Risposta accettata

Alan Stevens
Alan Stevens il 17 Ott 2020
One way is to do the following with your plot command:
plot(x,sol);
lg1 = ['t = ',num2str(t(1))];
lg2 = ['t = ',num2str(t(2))];
lg3 = ['t = ',num2str(t(3))];
lg4 = ['t = ',num2str(t(4))];
legend(lg1,lg2,lg3,lg4)

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by