Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How do I stop the loop continuously going without giving a final answer? It is two separate functions that link together.

조회 수: 1 (최근 30일)
function [distance] = ballistic1(theta,V0,Cd,mass,area,tstep,plotting)
% Theta 45 V0 60 Cd 0.5 Mass 0.145 Area 42x10^-4 tstep 0.01 % plotting 1 %
rho = 1.225; % density of the air % g = 9.81; % acceleration due to gravity %
U0 = V0 * cosd(theta); W0 = V0 * sind(theta);
V_tstep = sqrt(( 2 * mass * g ) / ( rho * Cd * area ) );
k = ( mass * g ) / V_tstep;
count = 0; Sz=0; t=0;
while Sz >= 0 count = count + 1; t = t+tstep; Sx(count) = ( ( mass / k ) * U0 ) * ( 1 - exp( ( -k * t ) / mass )); Sz(count) = ( ( - mass * g ) / k ) * t + ( mass / k ) * ( W0 + ( mass * g / k )) * ( 1 - exp( ( -k / mass ) * t )); end
distance = max(Sx);
if plotting == 1 plot( Sx , Sz ); end
end
function [optimum_theta] = Bisector_M(start,finish,Cd,V0,mass,area,tstep)
yLeft = start; yRight = finish; yMid = (yLeft + yRight) / 2;
Dist_L = ballistic1(yLeft,45,0.5,0.145,0.0042,0.01,0); Dist_R = ballistic1(yRight,45,0.5,0.145,0.0042,0.01,0); Dist_M = ballistic1(yMid,45,0.5,0.145,0.0042,0.01,0); error = abs( yMid - ( yLeft + yRight)/ 2);
while error < 0.05 Dist_L = ballistic1(yLeft,45,0.5,0.145,0.0042,0.01,0); Dist_R = ballistic1(yRight,45,0.5,0.145,0.0042,0.01,0); Dist_M = ballistic1(yMid,45,0.5,0.145,0.0042,0.01,0);
if error > 0.05
if Dist_R > Dist_L
yLeft = yMid - 0.8*(yMid - yLeft);
else Dist_L > Dist_R;
yRight = yMid + 0.8*(yRight - yMid);
end
yMid = (yLeft + yRight)/2;
end
end
x1 = Dist_L; x2 = Dist_M; x3 = Dist_R; optimum_theta = yMid end
  댓글 수: 1
Adam
Adam 2018년 11월 5일
Please format all your code when posting a big block of code. It is almost unreadable otherwise.

답변 (1개)

Adam
Adam 2018년 11월 5일
편집: Adam 2018년 11월 5일
You are running a while loop on the variable error (which incidentally is a function name so you should not name a variable this) yet nothing in your loop ever changes this value so either it will never run or it will run infinitely.
To make sure it terminates make sure error updates within the loop and is guaranteed to go below 0.05 (or add an escape clause after some number of iterations if it may never get that low)
You also have an if statement inside your while loop whose condition is completely contradictory to that of the while loop so it would only get executed if error were to increase during an iteration, which would seem to imply either that it will just terminate straight away or that error can go up and down, in which case it may be a bit hit and miss when it would terminate and whether or not it is when you would want it to.

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by