How do I make matlab legends match the colour of the graphs?
Here is the code I used:
x = linspace(0,2);
e = exp(1);
lin = e;
quad = e-e.*x.*x/2;
cub = e-e.*x.*x/2;
quart = e-e.*x.*x/2+e.*x.*x.*x.*x/24;
act = e.^cos(x);
mplot = plot(x,act,x,lin,x,quad,x,cub,x,quart);
legend('actual','linear','quadratic','cubic','quartic')
This produces a legend matching the right colors to actual and linear, then after that it seems to skip over red开发者_StackOverflow中文版 on the graph, but not on the legend, i.e. the legend says quadratic should be red, but the graph shows it as green, the legend says cubic should be green, but the graph shows it as purple etc.
Any help is appreciated.
The lin
curve needs to be fixed --- now you just have a bunch of points instead of a line. quad
and cub
need to be fixed as well (see below).
x = linspace(0,2);
e = exp(1);
lin = ones(size(x))*e; %#Now it's a vector with the same size as x
quad = e-e.*x.*x/2;
cub = e-e.*x.*x/2;
quart = e-e.*x.*x/2+e.*x.*x.*x.*x/24;
act = e.^cos(x);
mplot = plot(x,act,x,lin,x,quad,x,cub,x,quart);
legend('actual','linear','quadratic','cubic','quartic')
Are quad
and cub
meant to be the same? Maybe it should be:
quad = e-e.*x.*x/2;
cub = e-e.*x.*x.*x/2;
精彩评论