for loop, multiple plots in single plot

조회 수: 1 (최근 30일)
Ireedui Ganzorig
Ireedui Ganzorig 2020년 3월 18일
답변: Bhaskar R 2020년 3월 18일
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)

답변 (1개)

Bhaskar R
Bhaskar R 2020년 3월 18일
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);

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by