A Function Example

advertisement
A Function Example
March 15, 2005
Suppose you are hired for a summer job at Fort Richardson to study the effects air
resistance has on the flight of a projectile. In particular, you are asked to compare the
theoretical range and attained height of a ballistic projectile to the actual measured range
and height where air resistance plays a major concern. To help yourself, you decide to
write a MATLAB function to provide the theoretical range and height of a projectile. It
will be easy then to determine the theoretical values and compare with measured values.
After some research you determine that for an initial velocity v0 and angle from the
horizontal, θ, the range and height of a ballistic projectile are given by the following
relationships. (For convenience you decide to specify θ in degrees.)
1. hmax = v0y2/2g where g = 9.8 m/s2; the acceleration due to the earth’s gravity.
2. v0y is the vertical component of the projectile’s initial velocity, v0y = v0 sin( θ ).
3. dmax = 2 thmax v0x where v0x is the horizontal component of the initial velocity or
v0x = v0 cos ( θ ). That is, the range is twice the time to reach the maximum
height times the initial horizontal velocity. dmax = range.
4. thmax = v0y/g. This is the time to reach the maximum height.
With these relationships you are ready to write your MATLAB function. Using the
Editor you develop the following code;
function [hmax, dmax] = trajectory ( v0, theta )
%This scalar function computes the max height and max range
%of a ballistic projectile. v0 is the initial velocity in m/s,
%theta is the angle in degrees of the fired projectile.
%
%The outputs are hmax or the maximum height attained in meters
%and dmax or the max range attained, also in meters.
%
g = 9.8;
angle_radians = theta * pi/180;
%Convert degrees to radians
v0x = v0 * cos (angle_radians);
%Compute initial horizontal velocity
v0y = v0 * sin (angle_radians);
%Compute initial vertical velocity
hmax = v0y^2 / (2*g);
%Compute hmax
thmax = v0y/g;
%Time to maximum height
dmax = 2*thmax*v0x;
%Compute dmax
%
Once you have written and debugged this function, you save it in your MATLAB files
folder with file name trajectory. Now you are ready to use it.
In this example v0 and theta are scalars. Suppose you want to determine the height and
range of a projectile with initial velocity of 230 m/s and angle of 39o. You would enter
the following commands in the Command Window,
>> angle = 39;
>> vel = 230;
>>[h d] = trajectory (vel, angle)
h=
1.0678e+003
d=
5.2746e+003
After you have mastered this function, try writing it so that v0 and theta can be vectors,
that is, heights and ranges are computed for several initial velocities and angles with only
one call to your function. Your function will have to return vectors [h d] instead of just
scalar quantities, that is h and d will now be vectors. Test your new function by calling it
first with scalar quantities as above, and then with vector quantities, for example,
>> angle = [ 39 45 60];
>> vel = [ 230 200 250];
>> [h d] = trajectory ( vel, angle )
Download