How can I get my while loop to run?
1 次查看(过去 30 天)
显示 更早的评论
I am trying to iterate with a time step of 0.0001. The point is for a given distance (x = 8.2 miles) I am to find two values of theta to allow for this. When i run the code the while loop won't run the iteration and I keep getting just the initial condition values. How do i get my loop to run the iteration with the initial conditions?
% Set Intitial Conditions
x(1) = 0;
z(1) = 0;
M(1) = 7;
K = 1.4;
R = 53.353;
A = 0.202;
m = 1.37;
g = 32.2;
[ T_atm , P_atm , ro ] = atmos_funEE(z(1));
C = sqrt(K*R*T_atm);
Vabs(1) = M(1)*C;
Vx(1) = Vabs(1)*cos(0);
Vz(1) = Vabs(1)*sin(0);
Cd(1) = hypervelcd(M(1));
Fd(1) = 0.5*ro*(Vabs(1))^2*Cd(1)*A;
Fdx = Fd(1)*cos(0);
Fdz = Fd(1)*sin(0);
ax(1) = -Fdx/m;
az(1) = -g-Fdz/m;
% Begin Iteration
I = 1;
dt = 0:0.0001
while z(I) >= 0
x(I+1) = x(I) + Vx(I).*dt
z(I+1) = z(I) + Vz(I).*dt
Vx(I+1) = Vx(I) + ax(I).*dt
Vz(I+1) = Vz(I) + az(I).*dt
theta(I+1) = arctan(Vz(I+1), Vx(I+1));
[ T_atm , P_atm , ro ] = atmos_funEE(z(I+1));
C = sqrt(K*R*T_atm);
Vabs(I+1) = M(I+1) * C;
M(I+1) = Vabs(I+1) / C;
Cd(I+1) = hypervelcd(M(I+1));
Fd(I+1) = 0.5*ro*(Vabs(I+1))^2*Cd(I+1)*A;
Fdx = Fd(I+1)*cos(theta(I+1));
Fdz = Fd(I+1)*sin(theta(I+1));
ax(I+1) = -Fdx/m;
az(I+1) = -g-Fdz/m;
I = I + 1;
end
7 个评论
per isakson
2019-7-13
It's much easier to help if the code of the question is possible to run.
dpb
2019-7-13
Problem here is
[ T_atm , P_atm , ro ] = atmos_funEE(z(1));
altho for debugging the integration issues a set of constants would suffice undoubtedly.
回答(1 个)
Vimal Rathod
2019-7-17
Hi,
I understand that your primary concern is to make the while loop run and your code to work. I see that you have used another form of code in the comment's section. If the latest code is the one in the comments which you have posted.
I = 1;
dt = 0:0.0001:10
while z >= 0
ax(I+1) = -Fdx/m;
az(I+1) = -g-Fdz/m;
In this code the while loop is taking a vector 'z' and comparing it with zero which returns a vector. Instead of that try using the value ‘z(I)’ which provides a single output Boolean value for the while condition statement. The other error which you got is due to assigning a single value in a vector to a vector.
Instead of using the below code.
x(I+1) = x(I) + Vx(I)*dt
You could try using the code snippet below where I have used a specific value of dt
x(I+1) = x(I) + Vx(I)*dt(I)
You can refer to the while loop documentation and colon operator and indexing in the MATLAB documentation. Below are the links provided for the following.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!