Writing a Script for a Function

4 ビュー (過去 30 日間)
Cody Baumann
Cody Baumann 2015 年 4 月 8 日
コメント済み: Azeem Aminudin 2021 年 3 月 2 日
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
Michael Haderlein 2015 年 4 月 8 日
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.
  1 件のコメント
Azeem Aminudin
Azeem Aminudin 2021 年 3 月 2 日
awesome

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by