Haskell, Vectors and Simple Mechanics – part 3.

In this post we’ll continue the previous one about vectors and take a look at calculating the path of a projectile and rendering that path to the screen.
Imagine a single particle in three dimensional space, we can characterise a state for it as its position and velocity at some instant in time.
type State = (Time, Displacement, Velocity)

From basic mechanics and Newtons laws we know that if no forces act on it then not a lot happens really! Time will increase and depending on your point of view, not much else will change. However, if some forces are acting then things become more interesting. Knowing the forces acting is equivalent to knowing the acceleration of the particle and we can suggest that acceleration is a function of State.

We know from simple dynamics the relationships between velocity, acceleration and displacement

  1. d' = d + vt
  2. v' = v + at

Converting this to our Vector notation and taking small time intervals we can derive a function that takes an Accnf function, a small time interval and a State and returns a new State.

So for a given acceleration function, a (small) time step and a start State we can approximate a numerical solution with

this gives a lazily infinite list of State and so at some point we use some form of ‘take‘ function to collect a list of State that can be further processed and plotted.

As a simple example of applying this consider a projectile fired from a gun with velocity v at an angle theta to the horizontal. A detailed analytic treatment can be found here.

For our purposes we will ignore air resistance and assume a mass of 1 unit so that the only force acting on the particle is the acceleration due to gravity – this gives

To initialise the projectile with velocity v at angle theta we need to consider the horizontal and vertical components of the velocity as shown here

We can now create a solution giving a list of State

which computes a solution and just takes values while the projectile is above ground.
For display purposes I want to ‘fire’ several projectiles and vary the angle to the horizontal keeping the initial velocity the same. This function will do that for us

Rather than go through the code function by function I’ll put the ‘finished’ code below.

The ‘plot’ function calculates and shows the path of a particle fired at angles from 90 to 5 degrees varying by 5 degrees. and here are the results!

which agrees quite well with an analytical solution that would show an angle of 45 degrees having the maximum range and complementary angles giving the same range.

And just for the pretty picture doing plotSeveralProjectiles 50 180 5 gives

And finally… In the last post I showed a function

that would render n equally spaced vectors about the origin.

Because vecsAtOrigin is a specific case of a more general one of rendering vectors about a point and then translating them all to the origin it became an ‘itch’ that I really needed to ‘scratch’! Here’s the version after scratching the itch.

and here is an example using vecsAtPos

I think that’s enough for now. Next time we’ll maybe look at other, slightly more complex, forces and see if the naive step function is capable of handling them. As usual all the code is available of GitHub.

Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *

ˆ Back To Top