Code gets stuck loading when I try to run it.
14 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Les Beckham
2023-11-17
编辑:Les Beckham
2023-11-17
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
0 个评论
更多回答(2 个)
Voss
2023-11-17
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
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;
% 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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!