필터 지우기
필터 지우기

Finding minimum value in while loop

조회 수: 2 (최근 30일)
Takashi Fukushima
Takashi Fukushima 2019년 11월 9일
댓글: Takashi Fukushima 2019년 11월 9일
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