필터 지우기
필터 지우기

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

조회 수: 3 (최근 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