How do i have a plot automatically allocate colours to lines and label them in a legend

1 次查看(过去 30 天)
can anyone help me as the code below i cannot seem to get matlab to automatically allocate each new valu for r to have a different colour. it either gives all new iterations the same colour or sometimes i have it vary each interation of n with a new colour which is not what i am looking for. please help
for n=1:0.1:2
for r=0:0.1:1
y=(n^2)*(2*r);
plot(n,r,'Linewidth',3.5)
hold on
legend({'0','0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8','0.9','1'})
xlabel('x')
ylabel('y')
set(gca,'fontsize',12,'FontName', 'Times')
end
end

采纳的回答

VBBV
VBBV 2020-8-13
编辑:VBBV 2020-8-13
The equation y=(n^2)*(2*r); needs to be function of variables otherthan n and r
In your program n and r are used iteration counters which are in decimal increments.
MATLAB does not allow decimal increment iterations
Try this code
% code modified
n = 1:0.1:2;
r = 0:0.1:1;
for i=1:length(n)
for j=1:length(r)
y(i,j)=(n(i)^2)*(2*r(j));
plot(y(:,j),'Linewidth',1.5);
hold on
legend({'0','0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8','0.9','1'});
xlabel('x');
ylabel('y');
set(gca,'fontsize',12,'FontName', 'Times');
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by