How can I plot graph with lines of different color each using for loop?
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.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. 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. for loop, graph, plot, color, legend MATLAB Answers — New Questions