Finding minimum value in while loop

2 次查看(过去 30 天)
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.

回答(1 个)

Walter Roberson
Walter Roberson 2019-11-9
bestdeviation = inf;
while deviation < bestdeviation
bestdeviation = deviation;
deviation = abs(D-(speed^2) * sin(pi * angle / 90) / 9.81);
angle=angle+1;
end
  1 个评论
Takashi Fukushima
Takashi Fukushima 2019-11-9
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matched Filter and Ambiguity Function 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by