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.

采纳的回答

sai charan sampara
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
v(60) = 1800090.000999 when dt = 0.001000 v(60) = 360090.005000 when dt = 0.005000 v(60) = 180090.010000 when dt = 0.010000 v(60) = 36090.050000 when dt = 0.050000 v(60) = 18090.100000 when dt = 0.100000 v(60) = 6090.300000 when dt = 0.300000
hold off
xlabel('time')
ylabel('vertical velocity')
legend(["dt1","dt2","dt3","dt4","dt5","dt6"]);

更多回答(1 个)

Dyuman Joshi
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
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.
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 CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by