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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Pulse and Transition Metrics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by