Finding minimum value in while loop

14 Ansichten (letzte 30 Tage)
Takashi Fukushima
Takashi Fukushima am 9 Nov. 2019
Hello,
I would like to find the minimum value in what is calculated in while loop.
I have written a program so far...
speed=input("Enter the speed: ");
D=input("Enter the distance: ");
angle=5;
throwing_distance = (speed^2) * sin(pi * angle / 90) / 9.81;
deviation=abs(D-(speed^2) * sin(pi * angle / 90) / 9.81)
while angle<45
deviation=abs(D-(speed^2) * sin(pi * angle / 90) / 9.81);
angle=angle+1;
end
I would like to add if statement like,
if deviation is the minimum, display the angle.
I really appreciate if someone can help me solve this problem.

Antworten (1)

Walter Roberson
Walter Roberson am 9 Nov. 2019
bestdeviation = inf;
while deviation < bestdeviation
bestdeviation = deviation;
deviation = abs(D-(speed^2) * sin(pi * angle / 90) / 9.81);
angle=angle+1;
end
  1 Kommentar
Takashi Fukushima
Takashi Fukushima am 9 Nov. 2019
Thank you very much for your response.
I came up with this solution...
speed=input("Enter the speed: ");
D=input("Enter the distance: ");
min_deviation=abs(D-(speed^2) * sin(pi * 5 / 90) / 9.81);
angle=5;
while angle<45
deviation=abs(D-(speed^2) * sin(pi * angle / 90) / 9.81);
next_deviation=abs(D-(speed^2) * sin(pi * (angle+1) / 90) / 9.81);
next_deviation2=abs(D-(speed^2) * sin(pi * (angle+2) / 90) / 9.81);
angle=angle+1;
if deviation>=next_deviation & next_deviation<=next_deviation2
disp("Optimal angle: " + angle);
disp("Optimal deviation: " + next_deviation);
end
end
disp("Optimal angle: " + 5 + " Optimal deviation: " + min_deviation);
In this way, I can get the optimal(minimum) deviation and its angle.
However, I need the last statement when the if statement is never true.
For example, the optimal angle and deviation of high speed and short D like (V>=100 and D<=10) are always 5 degree since the D is within the distance of 5 degree. However, the problem is that if the if statement is true, the program shows both optimal and minimal values
I know this is another topic, but if you can help me solve the problem, I would really appreciate.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by