burnrate = 50;
fuel = 4000;
t = fuel / burnrate;
a_thrust = + 20;
a_gravity = -9.8;
y_0 = 0;
v_0 = 0;
altitude = y_0;
velocity = v_0;
acceleration = 0;
time = 0;
while altitude >= 0
time(end + 1) = time(end) + 1;
if fuel > 0
deltaFuel = burnrate;
accel = a_thrust;
else
deltaFuel = 0;
accel = a_gravity;
end
fuel = fuel - burnrate;
acceleration(end + 1,1) = accel;
velocity(end + 1,1) = velocity(end) + accel;
altitude(end + 1,1) = altitude(end) + velocity(end);
end
posAccelIndex = diff(velocity)>0;
posAccelIndex = [true; posAccelIndex];
posAccelIndex = acceleration >= 0;
disp(sprintf('Altitude reached: %f meters', max(altitude)));
disp(sprintf('Velocity at impact: %f m/s', velocity(end)));
figure;
subplot(1,2,1)
hold on;
plot(time(posAccelIndex), velocity(posAccelIndex), 'r', 'displayname', 'Powered');
plot(time(~posAccelIndex), velocity(~posAccelIndex), 'b', 'displayname', 'Freefall');
title('Vehicle Velocity (in m/s)')
legend('show')
subplot(1,2,2)
hold on;
plot(time(posAccelIndex), altitude(posAccelIndex), 'r', 'displayname', 'Powered');
plot(time(~posAccelIndex), altitude(~posAccelIndex), 'b', 'displayname', 'Freefall');
title('Vehicle Altitude (in m)')
legend('show')
Best Answer