.
MATLAB: Modeling speed and height of a toy rocket using while loops
MATLABpracticewhile loop
Related Solutions
Seems like some of your equations are wrong. I changed a few things so the code does not error. Array lengths need to match in equations and indexing cannot exceed or be below zero of the variable being referenced.
m_rocket = 95.6; %total mass of rocket (gm)
m_fuel = 15.6; %mass of fuel
f_thrust = 10.7; %force of thrust in N
a_rocket = 0.35; %sqm
c_drag = 0.06; %coefficient of drag
c_dragdown = 0.75; %c drag while falling down due to parachute
g = 9.81; %m/s^2
rho_air = 1.225; %kg/cubicm
drag = 0;t(1) = 0; %time elapsed
h(1) = 0; %height m/s
v(1) = 0; %velocity m/s
a(1) = -g; %m/s
%% While loop
i=1;h_check = h(1);t_step = 0.1; %seconds
while h_check >= 0 a(i+1) = ((m_rocket * g) - (0.5 * rho_air*v(i).^2 * c_drag * a_rocket)) / (m_rocket); v(i+1) = v(i) + (a(i)*t_step); h(i+1) = h(i) + (v(i)*t_step); t(i+1) = t(i) + t_step; h_check = h(i+1); i = i + 1; if (m_fuel(i - 1) - 5 * t_step) > 0 m_fuel(i) = m_fuel(i - 1) - 5 * t_step; f_thrust(i) = f_thrust(1); else m_fuel(i) = 0; f_thrust(i) = 0; end a_thrust(i) = (f_thrust(i) - drag(i-1)) / (m_rocket); %drag(i-1)
if m_fuel(i) == 0 m_rocket = a(1);%changed m_rocket to not be an array
else m_total(i) = (a_thrust(i - 1) + g); end drag(i) = -v(i-1) * abs(v(i-1)) * .0075; v(i) = v(i-1) + m_rocket * t_step;%m_rocket
h(i) = h(i - 1) + v(i - 1) * t_step + (.5 * m_rocket * t_step .^2);%m_rocket a(i+1) = ((-m_rocket * g) + (0.5 * rho_air*v(i).^2 * c_drag * a_rocket)) / (m_rocket); endfigureplot (t,f_thrust)title('Time vs Trust')xlabel('Time')ylabel('Thrust')figureplot (t,a(2:end))%acceleration is a longer array.
title('Time vs Acceleration')xlabel('Time')ylabel('Acceleration')figureplot (t,v)title('Time vs Velocity')xlabel('Time')ylabel('Velocity')figureplot (t,h)title('Time vs Altitude')xlabel('Time')ylabel('Altitude')
Assuming inv(A) is a typo and you really meant inv(AA):
"a" and "e" are the same, so you have two rows in AA that are the same. This linear dependency creates a singular matrix AA.
Best Answer