以下是我使用的代码:
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')
这会产生一个匹配正确颜色与实际和线性相关的图例,然后它会在图表上跳过红色,但不会跳过图例,即图例中的二次方应为红色,但图表显示为绿色,传说立方体应该是绿色,但图表显示为紫色等。
感谢任何帮助。
答案 0 :(得分:2)
需要修复lin
曲线 - 现在你只有一堆点而不是一条线。 quad
和cub
也需要修复(见下文)。
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')
quad
和cub
是否相同?也许它应该是:
quad = e-e.*x.*x/2;
cub = e-e.*x.*x.*x/2;