Modeling Projectile Motion Using Python

Any system or process can be described by some mathematical equations. Their nature may be arbitrary. Does security service of a stadium want to know fan traffic in the case of fire? Does an engineer construct a thermal generating unit for your house? Usually, using a small set of general rules and laws of nature, one can predict everything at least for the nearest future. So, in this article, we’ll continue our topic of the computer simulations and deal with the way all these calculations are performed.

Let us consider the following problem: how to hit a target in a given position with a canon? This problem has a simple analytical solution, so it will be easy to check ourselves. Let the cannon be placed in the origin, i.e. its horizontal coordinate is @$x_c=0@$ and the vertical coordinate is the same, @$y_c=0@$. This pair is an initial coordinate of a missile:

@$x(t=0)=0, \ y(t=0)=0@$

A cannon can fire a missile with the initial velocity @$v_0@$ at the angle of @$\alpha@$ to the horizon. Its motion is determined by gravity. The Newton’s second law gives us all equations needed:

@$m\vec{a} = \vec{F}@$

There is only an acceleration @$g=9.81\ \dfrac{m}{s^2}@$ due to the gravity directed downside: @$a_y=-g@$. And the horizontal motion is steady, i.e. @$a_x=0@$. Thus, the force @$\vec{F} = -mg\hat{y}@$ with @$\hat{y}@$ being the unit vector in the vertical direction. It results in the following system of equations:

Below, we show a simple numerical solution of this problem using python. Our physical background is given by the Eqns (1) and one needs to integrate them. To do this, we define a class Cannon with the constructor, accepting initial parameters:

The integration is performed in the makeShoot function, where method step is called. Main function gets lists with coordinates of a missile for two shoots with shooting angles @$45^{\circ}@$, @$30^{\circ}@$ and @$60^{\circ}@$. The ground is drawn by horizontal black line:

Problem of the missile motion can be solved analytically as well as with the PC. Let’s now check the results displayed above. Taking into account that @$\dfrac{dv}{dt} = a@$ and @$\dfrac{dx}{dt} = v@$ one can easily integrate Eqns (1) and get the rules for horizontal and vertical motions:

The integration method used is not suitable for any problem, but very simple for coding and understanding basic principles. In general case, nonlinear differential equations of arbitrary order are reduced to system of the first order equations solved simultaneously via any appropriate integration scheme like Runge-Kutta, BDF or Adams with tolerance control.