
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

