Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Legend won't pick up on plot colors
Replies: 3   Last Post: Mar 14, 2013 1:55 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Nasser Abbasi

Posts: 5,688
Registered: 2/7/05
Re: Legend won't pick up on plot colors
Posted: Mar 14, 2013 1:33 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"trbone76" wrote in message <khrctd$hhb$1@newscl01ah.mathworks.com>...
> I'm trying to plot two colored graphs on the same plot but the legend has the same color for both curves. Each curve has the correct color, though. I've read tons of posts on this subject but nothing seems to be working. My code is below and I can't seem to figure out what's wrong with it. Thanks in advance! (I'm using version R2012a)
>
> t0=0;
> tf=0.02;
> h=0.001;
> y0=[1;4];
> f=@(t,y) [-8003*y(1)+1999*y(2);23988*y(1)-6004*y(2)];
> f_exact=@(t,y) [exp(-7*t);4*exp(-7*t)];
>
> t = t0:h:tf;
>
> y_exact = zeros(length(y0),length(t));
> for j = 1:length(t)
> y_exact(:,j) = f_exact(t(j));
> end
>
> y = zeros(length(y0),length(t));
> y(:,1) = y0;
> for i = 1:length(t)-1
> y(:,i+1) = y(:,i) + h*f(t(i),y(:,i));
> end
>
> plot(t,y_exact,'b',t,y,'r')
> legend('Exact Solution','Euler Approximation');
> grid on
> axis([t0 tf -1 5])



You really need to have same legend as plots.

You have 4 plots and only 2 legends. (your y's are 2 rows each).

Change you plot command to this and it will work:

plot(t,y(1,:),'b',t,y(1,:),'r');

--Nasser



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.