
Re: Legend won't pick up on plot colors
Posted:
Mar 14, 2013 1:33 AM


On 3/13/2013 8:33 PM, trbone76 wrote: > 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

