Writing a Script for a Function

4 次查看(过去 30 天)
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
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by