WTF: Using F# to calculate ballistics

In building software, it is important to have the background, the algorithm and test data.

Background:

Ugh, what a weekend, a teenager was walking around in the roof of our condo with an Airsoft Pellet gun, with the orange tip covered so it looked like an assault rifle.  Needless to say it was an interesting afternoon with six cop cars and the police helicopter paying our quiet neighborhood a visit.  We were fortunate that the local news agencies have an office window policy, that is: if it isn’t viewable from their office windows, they don’t report on it.  Can’t blame the news agencies for that with the current advertisement prices dropping.  Now let’s take a look at computing a simple ballistic example, in this case, an Airsoft Pellet is fired straight up, we will assume that it is in a vacuum.

Also, do not attempt this at home, the teenager and his father had a very bad afternoon explaining all of this to the police and their neighbors.  You might consider just throwing a ball in the air instead.

Physics problem/algorithm development

The muzzle velocity for an Airsoft Pellet gun is around 300 ft/sec to 400 ft/sec, but let’s be like the rest of the world and use meters, so it is reasonable that the muzzle velocity could be around 100 m/s (from the Airsoft web page). 

To calculate the velocity of the pellet at the time that it’s velocity or speed is equal to zero, the derived calculations would be (since there is no x or horizontal velocity, and the mass is a “unit” mass):

  • y= ((Final velocity)^2 - (Initial Velocity)^2/(-2 * gravitational force)

The final velocity in this case is equal to zero so our equation becomes:

  • y= (-Initial Velocity)^2/(-2*gravitational force) or by multiplying by –1/-1 I get the equation:
  • y = (Initial Velocity)^2/(2*gravitational force) 

Which is a fairly simple equation, as long as you are able to shoot the pellet at exactly normal to the local gravitational field in a vacuum, and since it is an air rifle that could be hard to do.  In this case let’s go with it.

Back to Background

Let’s use the teenager running around on my condo’s roof on Saturday, his muzzle velocity is 100 m/s and he fires the pellet directly normal or 90 degrees to the ground surface, using a plumb bob he could determine the correct angle. 

Physics/algorithm again

Then the absolute highest the pellet could go is y=(100 m/s)^2/(2*9.8 m/s^2). 

Now units become an important part of this equation, in the upper part the equation becomes (100 m^2)/s^2/(19.6 m/s^2), which becomes about 5 meters around 510 meters above the barrel of the Airsoft pellet rifle. 

One of the problems we might encounter is that the Airsoft rifle is manually pumped and may have a tendency to vary the projectile velocity out of the barrel.  Let’s say that there is a 10% variation, and we might need to calculate this variation.  Before we write the F# code, it is important to have a way to insure your programming work is correct.

Test Data

I use Excel to get an idea of what the actual output of the F# program should be, and in this case I used a 10% variation in muzzle velocity.  You could use a graphing calculator to create the test data.

Now I look at the calculation, I have to ask myself is this correct?  Ok, let’s go with it.

The equation that I used in Excel is: Attitude Earth =Cellvalue^2/(2*9.8), where cellvalue is the cell coordinate, and I use a range of 90 to 110 in steps of 1 for the maximum attitude attained by the projectile in a vacuum and a perfect center of gravity.

Before writing my code, I usually make every attempt to create a simulation that I can use to check my programming.  I usually turn to Excel to do the initial calculation.  This gives a way to make sure I don’t make mistakes in programming and I am able to determine the form of the algorithm.

Attitude Earth Muzzle Velocity
413 90
423 91
432 92
441 93
451 94
460 95
470 96
480 97
490 98
500 99
510 100
520 101
531 102
541 103
552 104
563 105
573 106
584 107
595 108
606 109
617 110

Next we will write the F# that will solve the simulated equation.