Code gets stuck loading when I try to run it.

14 visualizzazioni (ultimi 30 giorni)
I am using a while loop to calculate and add the results to an index for the velocity, height and time of a model rockets journey so I can plot it. When I try to run my code a loading circle appears in the space right above the '1' for line one. It stays like this infinitly and never outputs anything into the work space. I have attached the code below. Hopefully someone can explain to me why this wont run. To my understanding I am not outputting an insane amount of values and filling up the memory. Or have I made an infinite loop which wont load. Also ignore the plotting at the end, still working that out. Thanks
clear; clc;
Known + Initial Values
% Known Values
m = 0.5 % Mass of the model rocket in Kg
Fthrust = 160 % Force of the engine fuel pushing the rocket at take off
g = 9.81 % Acceleration due to gravity
vChute = -20 % Velocity of the rocket when the auto parachute is deployed on way down
tEngineCut = 0.15 % Total time before engine cuts in seconds
a1 = (Fthrust - m*g)/m; % Acceleration calculation
% Initial Values @ t=0
v = 0 % Velocity
t = 0 % Time
h = 0 % Height
Dt = 0.01 % Time step size in seconds
i = 1; Tstart = 0; v(i) = 0; h(i) = 0; t(i) = 0;
Segment 1 - Rocket Take Off
% While loop to run all calculations from t=0 -> t=0.15 with steps of 0.01
while Tstart < tEngineCut
v1 = a1*Tstart; % Velocity Update
h1 = 0.5*a1*(Tstart^2); % Height Update
t(i) = Tstart; % Equating Variable
h(i) = h1; % Equating Variable
v(i) = v1; % Equating Variable
Tstart = Tstart + Dt; % Time Update
i = i + 1; % Increasing n by 1
end
h2 = h1;
t2 = Tstart;
v2 = v1;
SEGMENT 2 - ROCKET FREE FALL
% While loop to calculate the velocity, height and time as the rocket
% decelerates
while v2 > vChute % all values with time incriments of 0.01 until velocity hits 20 m/s
v2 = v1 - g * (t2-Tstart); % velocity update for free fall - force of gravity, slowing down
h2 = h1 + v1 * (t2-Tstart) - 0.5 * g * (t2-Tstart)^2; % Height Update
v(i) = v2; % Equating Variable
t(i) = t2; % Equating Variable
h(i) = h2; % Equating Variable
t2 = t2 + Dt; % Time Update
i = i + 1; % Increasing n by 1
end
v3 = v2;
h3 = h2;
t3 = t2;
SEGMENT 3 - ROCKET DECENDING WITH PARACHUTE OPEN TILL THE GROUND
% While loop to calculate the height at every 0.01s with a constant
% velocity of -20 m/s
while h3 > 0
v3 = vChute; % The velocity stays constant at -20 m/s^2 as the parachute will not allow it to go any faster
h3 = h2 - vChute * (t3-t2); % Update height
v(i) = v3; % Equating Variable
t(i) = t3; % Equating Variable
h(i) = h3; % Equating Variable
t3 = t3 + Dt; % Update time
i = i +1 % Increasing n by 1
end
Plotting Graphs
% Plot of velocity (speed of rocket) vs time
plot (t(i),v(i))
hold on
plot (t(i),h(i))
xlabel('Time (s)')
ylabel('Velocity (m/s)')
title('Velocity of the rocket relative to the time')
hold off

Risposta accettata

Les Beckham
Les Beckham il 17 Nov 2023
Modificato: Les Beckham il 17 Nov 2023
In your SEGMENT 1 code you have this line where you are updating time (even though the comment says "Height Update").
I don't see n used or updated anywhere else.
t(i) = t(n-1) + Dt; % Height Update
Try changing it to this:
t(i) = t(i-1) + Dt; % Time Update

Più risposte (2)

Bruno Luong
Bruno Luong il 17 Nov 2023
First look of your code shows that you do this mistake:
  1 Commento
Oscar Kinsella
Oscar Kinsella il 17 Nov 2023
Thanks for answering. I have updated my code now to the code below. I still dont understand why it wont run. Would you have another look?
clear; clc;
Known + Initial Values
% Known Values
m = 0.5 % Mass of the model rocket in Kg
Fthrust = 160 % Force of the engine fuel pushing the rocket at take off
g = 9.81 % Acceleration due to gravity
vChute = -20 % Velocity of the rocket when the auto parachute is deployed on way down
tEngineCut = 0.15 % Total time before engine cuts in seconds
clear t v h
% Initial Values @ t=0
v = 0 % Velocity
t = 0 % Time
h = 0 % Height
Dt = 0.01 % Time step size in seconds
i = 1; v(i) = 0; h(i) = 0; t(i) = 0;
Segment 1 - Rocket Take Off
% While loop to run all calculations from t=0 -> t=0.15 with steps of 0.01
a1 = (Fthrust - m*g)/m; % Acceleration calculation
while t(i) < tEngineCut % Run while loop until the engine cuts out after 0.15s
i = i + 1; % Increasing n by 1
v(i) = a1*t(i); % Velocity Update
h(i) = 0.5*a1*t(i)^2; % Height Update
t(i) = t(n-1) + Dt; % Height Update
end
v1 = v(i);
h1 = h(i);
t1 = t(i);
SEGMENT 2 - ROCKET FREE FALL
% While loop to calculate the velocity, height and time as the rocket
% decelerates
while v(i) > vChute % all values with time incriments of 0.01 until velocity hits 20 m/s
i = i + 1; % Increasing n by 1
v(i) = v1 - g * (t(i)-t1); % velocity update for free fall - force of gravity, slowing down
h(i) = h1 + v1 * (t(i)-t1) - 0.5 * g * (t(i)-t1)^2; % Height Update
t(i) = t(i-1) + Dt; % Time Update
end
v2 = v(i);
h2 = h(i);
t2 = t(i);
SEGMENT 3 - ROCKET DECENDING WITH PARACHUTE OPEN TILL THE GROUND
% While loop to calculate the height at every 0.01s with a constant
% velocity of -20 m/s
while h(i) > 0 % Run while loop until rocket hits the ground
i = i + 1 % Increasing n by 1
v(i) = vChute; % The velocity stays constant at -20 m/s^2 as the parachute will not allow it to go any faster
h(i) = h2 + vChute * (t(i)-t2); % Update height
t(i) = t(i-1) + Dt; % Update time
end
Plotting Graphs
subplot(1,2,1)
plot(t,h,t2,h2,'o')
subplot(1,2,2)
plot(t,v,t2,v2,'o')

Accedi per commentare.


Voss
Voss il 17 Nov 2023
vChute is negative, so you should be adding it in the calculation of h3, when the rocket is falling
% h3 = h2 - vChute * (t3-t2); % Update height
h3 = h2 + vChute * (t3-t2); % Update height
Now the code runs:
% Known Values
m = 0.5 % Mass of the model rocket in Kg
m = 0.5000
Fthrust = 160 % Force of the engine fuel pushing the rocket at take off
Fthrust = 160
g = 9.81 % Acceleration due to gravity
g = 9.8100
vChute = -20 % Velocity of the rocket when the auto parachute is deployed on way down
vChute = -20
tEngineCut = 0.15 % Total time before engine cuts in seconds
tEngineCut = 0.1500
a1 = (Fthrust - m*g)/m; % Acceleration calculation
% Initial Values @ t=0
v = 0 % Velocity
v = 0
t = 0 % Time
t = 0
h = 0 % Height
h = 0
Dt = 0.01 % Time step size in seconds
Dt = 0.0100
i = 1; Tstart = 0; v(i) = 0; h(i) = 0; t(i) = 0;
% While loop to run all calculations from t=0 -> t=0.15 with steps of 0.01
while Tstart < tEngineCut
v1 = a1*Tstart; % Velocity Update
h1 = 0.5*a1*(Tstart^2); % Height Update
t(i) = Tstart; % Equating Variable
h(i) = h1; % Equating Variable
v(i) = v1; % Equating Variable
Tstart = Tstart + Dt; % Time Update
i = i + 1; % Increasing n by 1
end
h2 = h1;
t2 = Tstart;
v2 = v1;
% While loop to calculate the velocity, height and time as the rocket
% decelerates
while v2 > vChute % all values with time incriments of 0.01 until velocity hits 20 m/s
v2 = v1 - g * (t2-Tstart); % velocity update for free fall - force of gravity, slowing down
h2 = h1 + v1 * (t2-Tstart) - 0.5 * g * (t2-Tstart)^2; % Height Update
v(i) = v2; % Equating Variable
t(i) = t2; % Equating Variable
h(i) = h2; % Equating Variable
t2 = t2 + Dt; % Time Update
i = i + 1; % Increasing n by 1
end
v3 = v2;
h3 = h2;
t3 = t2;
% While loop to calculate the height at every 0.01s with a constant
% velocity of -20 m/s
while h3 > 0
v3 = vChute; % The velocity stays constant at -20 m/s^2 as the parachute will not allow it to go any faster
h3 = h2 + vChute * (t3-t2); % Update height
v(i) = v3; % Equating Variable
t(i) = t3; % Equating Variable
h(i) = h3; % Equating Variable
t3 = t3 + Dt; % Update time
i = i +1; % Increasing n by 1
end
% Plot of velocity (speed of rocket) vs time
plot(t,v)
hold on
plot(t,h)
xlabel('Time (s)')
ylabel('Velocity (m/s)')
title('Velocity of the rocket relative to the time')
hold off

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by