How can I plot graph with lines of different color each using for loop?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello,
I draw the graph v vs t using for loop and graph should include multiple line for different dt. I try to let each lines have different color and add legend, but I got same color line and legend for last loop only. Code I write is:
%initial condition
uold=0;
dt=0.01;
i=0;%count
j=1;
frhs=@(t,v) (1/1000)*(1064*t*exp(-0.95*v)-135*v^2)-9.81;
dt=[0.001, 0.005, 0.01, 0.05, 0.1, 0.3];
for j=1:length(dt)
for told=0:dt:60
i=i+1;
[unew] = RK4(told,uold,frhs,dt(j));
uold=unew;
x(i)=told;
y(i)=unew;
end
fprintf('v(60) = %f when dt = %f \n',unew,dt(j))
plot(x,y,'color',rand(1,3))
legend(sprintf('dt=%f',dt(j)))
end
xlabel('time')
ylabel('vertical velocity')
I would like to get graph which different color line with each dt and legend to show which color is which dt.
When I run the code, color is random every time I run, but all lines are same color and legend has only for dt=0.3.
댓글 수: 0
채택된 답변
sai charan sampara
2024년 4월 22일
편집: sai charan sampara
2024년 4월 22일
Hi Szu,
Firstly there are some errors to be corrercted. The variable "i" needs to be intialized to zero every time the outer loop runs. "uold" might also be neede to be intialized to zero for each iteration of outer loop. Since the arrays "x" and "y" are being overwritten they need to be cleared at the end of each iteration of outer loop to remove the values from previous iteration. Also for the inner "for" loop the step value must be "dt(j)" instead of "dt". Making those corrections will give desired output as follows:
uold=0;
%dt=0.01;
i=0;%count
%j=1;
frhs=@(t,v) (1/1000)*(1064*t*exp(-0.95*v)-135*v^2)-9.81;
dt=[0.001, 0.005, 0.01, 0.05, 0.1, 0.3];
fig=figure;
hold on
for j=1:length(dt)
i=0;
uold=0;
for told=0:dt(j):60
i=i+1;
[unew] = told+uold+dt(j);%Some random function for verifying
uold=unew;
x(i)=told;
y(i)=unew;
end
fprintf('v(60) = %f when dt = %f \n',unew,dt(j))
plot(x,y,'color',rand(1,3))
clear("x","y");
end
hold off
xlabel('time')
ylabel('vertical velocity')
legend(["dt1","dt2","dt3","dt4","dt5","dt6"]);
추가 답변 (1개)
Dyuman Joshi
2024년 4월 22일
You are over-writing the figure in each iteration, thus you only get the plot for a single iteration (i.e. the last one).
Call a figure before your for loop and retain the plots using hold on -
fig = figure;
hold on
for xyz
for abc
...
...
end
end
hold off
Remove the hold after the loops are completed.
댓글 수: 6
Dyuman Joshi
2024년 4월 22일
편집: Dyuman Joshi
2024년 4월 22일
@Szu Yu Chew, I would suggest you to -
1) Not use clear() as done in the other answer and preallocate variables instead.
See - https://in.mathworks.com/help/matlab/matlab_prog/techniques-for-improving-performance.html#buwj1nn
for reference as to why.
2) Not define legend() dynamically as done in the other answer.
You can refer to the method I have used or utilize strings like this -
str = "dt" + 1:numel(dt);
legend(str);
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!