You are on page 1of 9

Programming and Numerical Algorithms with MathCAD -- An Example

To show how we can do some elementary programming with MathCAD, we will


look at the problem of calculating the trajectory of a projectile. This is a classic
physics problem and is relevant to the Lego Pentathlon task "Free Shot"
As usual with mechanics problems, we begin with Newton:
F

ma

= (the arrows indicate vector quantities)


or
a

m
=
It is easiest to divide the trajectory into its vector components: the x-component
(or range) and the y-component (or elevation). Refer to the notes on the trajectory
problem to see the details.
Treating the frictional forces arising from collision with air particles analytically as
a Taylor expansion, we can to first order write:
F
friction
v = where o is called the coefficient of friction and v
is the velocity
Notice that this is just the first (linear) term in the Taylor expansion. It makes
sense because we expect the frictional forces to increase as the velocity
increases (i.e., more collisions per unit time) and to vanish when the projectile is
at rest. However, it is important to remember that there may be other "high-order"
terms that, for now, we are neglecting. Now, lets set up the equations that need to
be solved. We will do this "numerically" to show how the calculation can be
done. We will also compare it to the analytical solution that is given in the
additional notes accompanying this MathCAD worksheet.
V 10
m
s
:= "Muzzle velocity" of the launcher in m/s
V
x
( ) V cos

180
|

\
|
|
.
:= X-component of the velocity. u is the elevation angle
in degrees
Vy ( ) V sin

180
|

\
|
|
.
:= Y-component of the velocity. u is the elevation angle
in degrees
Recall that the equations of motion for the projectile can be written as:
t
V
x
d
d

m
V
x
=
where o is the coefficient of friction (units of kg/s) and m is the mass of the
projectile (units of kg). To solve differential equations like this numerically, we can
convert the derivatives into "finite differences." Recall the definition of a derivative:
x
f x ( )
d
d
0 x
f x x + ( ) f x ( )
x
lim

=
if we take the value of f at x+Ax to be f
i+1
and the value at x to be f
i
, then we can
approximate this derivative with the finite difference expression:
x
f x ( )
d
d
f
i 1 +
f
i

x
i 1 +
x
i

=
Applying this formula to our equations of motion above, we get
t
V
y
d
d
g

m
V
y
=
V
x
i 1 +
V
x
i

t
i 1 +
t
i


m
V
x
i
=
V
y
i 1 +
V
y
i

t
i 1 +
t
i

g

m
V
y
i
=
These can be rearranged into a more convenient form:
V
x
i 1 +
1

m
t
i 1 +
t
i

( )

(
(

V
x
i
=

V
y
i 1 +
g t
i 1 +
t
i

( )
1

m
t
i 1 +
t
i

( )

(
(

V
y
i
+ =
These are known as "finite difference" equations and they tell you how to
calculate the next value of Vx or Vy given the preceding value. The discrete time
difference,
t
i 1 +
t
i
can be written as At, and we should make this
interval
as small is possible to get a good approximation to the derivative. Of course, if it
is too small, then the problem will take longer to solve and MathCAD may have
convergence problems
convergence problems.
Let's proceed to solving for the velocity. First we set our discrete time interval and
the constants:
1
kg
s
:= Coefficient of friction
m 1 kg := Mass of the projectile in kg
t 5 10
2
s := Time interval in seconds
Number of points to evaluate
n 500 :=
To solve for the velocity, we will set up a small programming loop in MathCAD:

V
x
( ) :=
Here we have begun to define the x-component of the velocity. The
programming bar is added by clicking on the programming toolbar and then
clicking on "Add Line"
V
x
( ) V
x
0
V cos ( ) :=
Now, we have assigned the initial value of the velocity as the x-component of
the muzzle velocity. We will need some additional statements in our program,
so on the red square we once again click "Add Line":
V
x
( ) V
x
0
V cos ( )
i 1
:=
Here we have defined our "counter" integer i and assigned its initial value of 1. No
we extend our programming line again with "Add Line" and set up a "for-next" loop
This loop is executed for all values of i that are less than the maximum of n. Inside
the loop, the counter integer is incremented and the subsequent values of Vx are
calculated:
V
x
( ) V
x
0
V cos

180

\
|
|
.

i 0
V
x
i 1 +
1

m
t ( )

(
(

V
x
i

i i 1 +
i 0 n .. e for
V
x
:=
This last step tells MathCAD where to put the data from the calculation. It seems a
bit redundant to me, but this is the way the program's syntax is set up.
Now we do the same thing for the y-component of the velocity:
V
y
( ) V
y
0
V sin

180

\
|
|
.

i 0
V
y
i 1 +
g t ( ) 1

m
t ( )

(
(

V
y
i
+
i i 1 +
i 0 n .. e for
V
y
:=
We can list out the values of Vx and Vy by just using the "=" sign:
V
x
45 ( )
0
0
1
2
3
4
5
6
7
8
9
10
11
7.071
6.718
6.382
6.063
5.759
5.471
5.198
4.938
4.691
4.457
4.234
4 022
m
s
= V
y
45 ( )
0
0
1
2
3
4
5
6
7
8
9
10
11
7.071
6.227
5.425
4.664
3.94
3.253
2.6
1.98
1.39
0.831
0.299
0 207
m
s
=
11
12
13
14
15
4.022
3.821
3.63
3.448
...
11
12
13
14
15
-0.207
-0.687
-1.143
-1.576
...
Now that we have found the x and y components of the velocity, we can calculate
the position coordinates, x and y.
x
i 1 +
x
i
V
x
i
t + :=
i
y
i 1 +
y
i
V
y
i
t + :=
i
To evaluate these quantities, we set up a loops as before:
X
initial
0 m := Y
initial
0 m :=
x ( ) i 0
x
0
X
initial

x
i 1 +
x
i
V
x
( )
i
t +
i i 1 +
i 0 n .. e for
x
:= y ( ) i 0
y
0
Y
initial

y
i 1 +
y
i
V
y
( )
i
t +
i i 1 +
i 0 n .. e for
y
:=
Now, let's plot the trajectory for several launch angles. We will also compare the
numerical solution to the exact solution derived in the accompanying notes:
, ( ) V cos

180

\
|
|
.

1 exp

m

|

\
|
|
.

\
|
|
.
:=
, ( )
m

g
m

V sin

180

\
|
|
.
+
|

\
|
|
.
1 exp

m

|

\
|
|
.

\
|
|
.

(
(

g
m

:=
0 s 1 10
3
s , 10s .. := Give the time parameter a range of values
0 0.6 1.2 1.8 2.4 3 3.6 4.2 4.8 5.4 6
0
0.3
0.6
0.9
1.2
1.5
1.8
2.1
2.4
2.7
3
y 60 ( )
t 60 , ( )
y 45 ( )
t 45 , ( )
y 30 ( )
t 30 , ( )
x 60 ( ) t 60 , ( ) , x 45 ( ) , t 45 , ( ) , x 30 ( ) , t 30 , ( ) ,
e

You might also like