How can I get my while loop to run?

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 Comments

HINT:
>> dt = 0:0.0001
dt =
0
>>
So i bounded dt to go from 0 to 10 with the same step size like so:
I = 1;
dt = 0:0.0001:10
while z >= 0
ax(I+1) = -Fdx/m;
az(I+1) = -g-Fdz/m;
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));
end
However I now get this error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in Hypervelocityproject2 (line 31)
x(I+1) = x(I) + Vx(I)*dt
I tried using .* to mulitply and that does not work either.
Well, yes, now dt is the full vector...you don't want the vector of times, you just need a timestep dt to use for the integration step.
I've noticed that my loop is only running twice. I think this is due to the fact that I have I = 1. I changed the dt to just be 0.0001 and let it run but I think there is something wrong with the the I. I just can't figure out how to get past it.
...
z(1)=0;
...
while z >= 0
...
az(I+1) = -g-Fdz/m;
z(I+1) = z(I) + Vz(I)*dt
Vz(I+1) = Vz(I) + az(I)*dt
...
What happens to z when you add a negative acceleration integrated to a velocity and then a displacement and add to an initial position of 0? What does that do to continuing your while loop?
Use the debugger and step through and see what occurs as you do...since the components are separable, it might make simpler to solve x,z independently in code sections as I sorta' did above by pulling out only z to reduce clutter...
It's much easier to help if the code of the question is possible to run.
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.

Sign in to comment.

Answers (1)

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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

B M
on 13 Jul 2019

Answered:

on 17 Jul 2019

Community Treasure Hunt

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

Start Hunting!