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

조회 수: 2 (최근 30일)
Seb apple 2020년 8월 12일
편집: VBBV 2020년 8월 13일
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 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

댓글을 달려면 로그인하십시오.

카테고리

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