Programming and Numeri
Programming and Numeri
cal Algorithms with MathCAD -- An Example
cal Algorithms with MathCAD -- An Example
TTo show how o show how we can do we can do some elementasome elementary progrry programming with MathCAD, we amming with MathCAD, we wilwilll look at the problem of calculating the tr
look at the problem of calculating the trajectory of a proajectory of a projectile. This jectile. This is a clis a classicassic physics problem and is
physics problem and is relevant to the Lego Pentatrelevant to the Lego Pentathlon task "Free hlon task "Free Shot"Shot" As usual with
As usual with mechanics mechanics problems, problems, we begin with Newton:we begin with Newton:
F
F ==m m aa (the arrows (the arrows indiindicate cate vector vector quantitiquantities)es)
or or aa FF m m = =
It is easiest to divide the trajectory into its vector components: the x-component It is easiest to divide the trajectory into its vector components: the x-component (or range) and the y-component (or elevation
(or range) and the y-component (or elevation). Refer to the notes on the trajectory). Refer to the notes on the trajectory problem to see the details.
problem to see the details.
Tr
Treating the fricteating the frictional ional forforces arising from collisices arising from collision wion with air particles analytically th air particles analytically asas a Taylor expansion, we can to first order write:
a Taylor expansion, we can to first order write:
F
Ffrictionfriction==αα v v wherewhere
is cal is called the coefficient of fled the coefficient of friction and vriction and vis the velocity is the velocity Notice
Notice that this is jthat this is just the first (linear) term in the Tust the first (linear) term in the Taylor expaylor expansionansion. It m. It makesakes sense because we expect the frictional forces to increase as the velocity sense because we expect the frictional forces to increase as the velocity increases (i.e.
increases (i.e., mor, more collie collisions per unit time) and to vanish when the projectile isions per unit time) and to vanish when the projectile i ss at rest. However, it is important to remember that
at rest. However, it is important to remember that there may be other "hithere may be other "high-order"gh-order" terms t
terms that, for now, hat, for now, we are newe are neglectiglecting. Now, lets set up the equang. Now, lets set up the equations that need totions that need to be solved. We will
be solved. We will do this "numerically" to show how the calculation can bedo this "numerically" to show how the calculation can be done. We will
done. We will also compare it talso compare it to the analytical solution that is given in theo the analytical solution that is given in the additional notes accompanying this MathCAD
additional notes accompanying this MathCAD worksheetworksheet..
V
V 1010 mm ss
"Muzzle velocity" of the launcher in m/s"Muzzle velocity" of the launcher in m/s
V Vxx θθ( ( ) ) V V ccooss θ θ ππ 180 180
X-component of tX-component of the velocityhe velocity.. is the elevation angl is the elevation angl
in degrees in degrees Vy Vy( ( ) θθ) V V ssiinn θ θ ππ 180 180
Y-component of the velocity.Y-component of the velocity. is the elevation angl is the elevation angl
in degrees
Recall that the equations of motion for the projectile can be written as: tVx d d α m Vx =
where
is the coefficient of friction (units of kg/s) and m is the mass of theprojectile (units of kg). To solve differential equations like this numerically, we can convert the derivatives into "finite differences." Recall the definition of a derivative:
xf x( ) d d ∆x 0 f x( ∆x) f x ( ) ∆x lim =
if we take the value of f at x+
x 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:xf x( ) d d f i 1 f i x i 1 xi =
Applying this formula to our equations of motion above, we get
tVy d d g α mVy = Vx i 1 Vxi t i 1 ti α m Vxi = Vy i 1 Vyi t i 1 ti g α mVyi =
These can be rearranged into a more convenient form:
Vxi 1 1 α m
ti 1 ti
Vxi = Vyi 1 g t i 1 ti
1 α m
ti 1 ti
Vyi =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 ti can be written as
t, and we should make thisinterval
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
.
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 2s 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:
Vx θ( )
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"
Vx θ( ) Vx
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":
Vx θ( ) Vx 0 V cos( )θ i 1
Here we have defined our "counter" integer i and assigned its initial value of 1. N we extend our programming line again with "Add Line" and set up a "for-next" loo 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:
Vx θ( ) Vx 0 V cos θ π 180
i 0 Vxi 1 1 α m( ∆t)
Vxi i i 1 i 0 n for Vx 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:
Vy θ( ) Vy 0 V sin θ π 180
i 0 Vy i 1 g ( ∆t) 1 α m( ∆t)
Vyi i i 1 i 0 n for Vy We can list out the values of Vx and Vy by just using the "=" sign :
Vx 45( ) 0 0 1 2 3 4 5 6 7 8 9 10 7.071 6.718 6.382 6.063 5.759 5.471 5.198 4.938 4.691 4.457 4.234 m s Vy 45( ) 0 0 1 2 3 4 5 6 7 8 9 10 7.071 6.227 5.425 4.664 3.94 3.253 2.6 1.98 1.39 0.831 0.299 m s
12 13 14 15 . 3.821 3.63 3.448 ... 12 13 14 15 - . -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 xi Vxi∆t
i yi 1i yi Vyi∆t
To evaluate these quantities, we set up a loops as before:
Xinitial 0 m Yinitial 0 m x( ) θ i 0 x 0 Xinitial x i 1 xi Vx θ( )i∆t i i 1 i 0 n for x y( ) θ i 0 y 0 Yinitial y i 1 yi Vy θ( )i∆t i i 1 i 0 n 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
m α 1 exp α m τ
ψ τ θ( ) m α g m α V sin θ π 180
1 exp α m τ
g m α τ 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( )