for loop, multiple plots in single plot

2 views (last 30 days)
Ireedui Ganzorig
Ireedui Ganzorig on 18 Mar 2020
Answered: Bhaskar R on 18 Mar 2020
Hello MATLAB community,
I am working on this projectile problem and I am utilizing the for loop command for several times. But I really feel like there must be a cleaner way to do it by using only one for loop command, but I am not sure though. And also, is there any way I can find the specific index where the hPluto is 0 and so forth with the other h's?
Vi = 25;
t1 = [0:0.1:86.3];
t2 = [0:0.1:13.5];
t3 = [0:0.1:13.5];
t4 = [0:0.1:6];
g = [0.58 3.7 3.71 8.87]
for i = 1:length(t1)
hPluto = Vi .* t1 - 0.5 .* g(1) .* t1 .^ 2;
end
for i = 1:length(t2)
hMercury = Vi .* t2 - 0.5 .*g(2) .* t2 .^2;
end
for i = 1:length(t3)
hMars = Vi .* t3 - 0.5 .*g(3) .* t3 .^2;
end
for i = 1:length(t4)
hVenus = Vi .* t4 - 0.5 .*g(4) .* t4 .^2;
end
%[valmax, indmax] = max(hVenus); I used to get an idea of where the maximum value of the graph is
% so I can multiply the index by 2 to get the value of t.
plot(t1, hPluto, t2, hMercury, t3, hMars, t4, hVenus)

Answers (1)

Bhaskar R
Bhaskar R on 18 Mar 2020
Vi = 25;
t1 = [0:0.1:86.3];
t2 = [0:0.1:13.5];
t3 = [0:0.1:13.5];
t4 = [0:0.1:6];
g = [0.58 3.7 3.71 8.87];
hPluto = Vi .* t1 - 0.5 .* g(1) .* t1 .^ 2;
hMercury = Vi .* t2 - 0.5 .*g(2) .* t2 .^2;
hMars = Vi .* t3 - 0.5 .*g(3) .* t3 .^2;
hVenus = Vi .* t4 - 0.5 .*g(4) .* t4 .^2;
plot(t1, hPluto, t2, hMercury, t3, hMars, t4, hVenus);
ind = find(hPluto == 0);

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!