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

1 view (last 30 days)
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

Accepted Answer

VBBV
VBBV on 13 Aug 2020
Edited: VBBV on 13 Aug 2020
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!