Search All of the Math Forum:

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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

 Messages: [ Previous | Next ]
 Nasser Abbasi Posts: 6,677 Registered: 2/7/05
Re: Legend won't pick up on plot colors
Posted: Mar 14, 2013 1:33 AM

"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

Date Subject Author
3/14/13 Nasser Abbasi
3/14/13 Nasser Abbasi
3/14/13 Nasser Abbasi