10/12/2010
1
Problem:
An object is shot upwards with an initial velocity of
v
0
m/s. Its height
h
(in m) after
t
seconds is given by
where
r
is a drag coefficient and g is 9 81 m/s
2
r
gt
e
r
g
v
r
h
rt
1
1
0
is a drag coefficient and g is 9.81 m/s .
If
r
= 0.35 s
‐
1
and
v
0
= 78 m/s
(i) When will be object be 80m above the ground?
(ii) When will the object hit the ground?
(iii) What maximum height will be attained?
function [ height ] = objectHeight( v0, r, t )
% OBJECTHEIGHT Calculates height of an object shot vertically.
A good first step in solving the problem is to implement the key equation as a
function m
‐
file (although it’s admittedly on the simple side and an anonymous
function could also be used).
% Inputs: v0 = initial velocity (in m/s)
%
r = drag coefficient (in 1/s)
%
t = time (in s), cannot be zero
% Outputs: height = height of object
if r == 0
error ('zero drag coefficients are not allowed');
end
g = 9.81; % gravity
height = (1 / r) * (v0 + g/r) * (1
‐
exp(
‐
r * t))
‐
g * t / r;
end

This ** preview** has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*
10/12/2010
2
>> v0 = 78; r = 0.35;
One way of plotting the function:
>> hFunc = @(t) objectHeight (v0, r, t);
>> fplot (hFunc, [0 12]);
Another way (works because the function can deal with a vector of times):
>> t = linspace(0,12,100);
>> h = objectHeight (v0, r, t);
>> plot (t, h);
Yet another way (doesn’t require function to handle vectors):
>> t = linspace(0,12,100);
>> h = ones (size(t));
% preallocate for efficiency
>> for i = 1 : length(t)
h(i) = objectHeight(v0, r, t(i));
end
>> plot (t,h)
80
100
120
0
20
40
60
Height (metres)
0
2
4
6
8
10
12
-40
-20
Time (seconds)