# Writing a Script for a Function

6 views (last 30 days)
Cody Baumann on 8 Apr 2015
Commented: Azeem Aminudin on 2 Mar 2021
Problem: A rocket is launched vertically. At time t=0, the rocket's engine shuts down. At that time, the rocket has reached an altitude of 500 meters and is rising at a velocity of 125 meters/second. Gravity then takes over. The height of the rocket as a function of time is: h(t) = -9.8t^2 +125t +500
Create a Function called rocket_height that accepts time as an argument and returns the height of the rocket. This function does not need to validate that time is greater than 0.
Create a script that - plots height versus time for times from 0, by 0.1 seconds, until the rocket hits the ground - finds the time when the rocket starts to fall back to the ground using the max function - labels the point on the plot with an arrow pointing to the location and text about occurrence
Issue: I am not exactly sure how to structure the script so it connects with my function command. I know I will have to create a while loop that goes from 0 by 0.1 until h(t)=0. I have already created the function command.

Michael Haderlein on 8 Apr 2015
As this is a homework, I guess things like analytical solutions are not the key here. In principle, you can do this with
h=500; %initialize the height array
t=0;
while h(end)>=0
t=t+0.1;
h(end+1)=rocket_height(t);
end
t_array=0:0.1:t; %to create an array with the times corresponding to h.
You'll get a warning that the h array changes size every loop iteration. However, in this case preallocation is not too straight-forward if you do not want to use analytical solutions of your height equation. I guess you can live with that warning. That's why I create the time array a bit differently.
Azeem Aminudin on 2 Mar 2021
awesome