Jupyter notebook Week-06-Projectile/Week-06-projectile.ipynb
Python exercise for projectile motion
In this exercise, we will use a simple script to plot the trajectory of a projectile with inclusion of air resistance. We will also calculate the range of the projectile.
The air resistance drag force in is approximated by the following relation.
Here, is the magnitude of velocity in and is the coefficient of drag.
The following is a step-by-step guide to writing up the script. Some parts of the script have been left out for you to fill in.
Define the parameters in the problem
First we import the modules we need: numpy to provide some mathematics functions, and matplotlib for plotting.
To start describing the problem, we need to define the parameters in the problem.
the mass of the projectile
acceleration due to gravity
initial velocity (magnitude and direction)
drag coefficient
We also set the time step to track the trajectory.
Set up the variables at time zero
Next, we generate velocity of the projectile as a function of time. To do that, we create the following lists.
list to store the values of time
list to store x-component of velocity
list to store y-component of velocity
list to store x-component of acceleration
list to store y-component of acceleration
We start by putting in the initial velocity components and at .
To get the acceleration, we need to find the resultant force, which consists of the air drag and weight .
and
The x and y components of the resultant force are given by
where is the angle that the velocity forms with the positive x-axis. The force components follow the convention that they are positive when pointing to the right and upwards.
By Newton's second law, the acceleration of the projectile is thus given by
Let's implement these steps to get the velocity, drag force, and acceleration at .
Update the velocity for every time-step
To get the velocity at the next time step, we make use of the following approximation.
or
This is just a very primitive way of doing integration of acceleration to get velocity. In the literature, this is known as Euler's method.
Let us calculate 10 sets of velocities for 10 time-steps. We will use a while-loop and keep a counter to count to 10.
Update the positions
After we have obtained the velocities, we can get the and positions of the projectile using a similar concept.
or
Starting with and at , we can use Euler's method to get the positions for the 10 time-steps using the velocities found previously.
Let's put everything together
How about calculating the velocity and position all together in the same while-loop?
Also, let's stop the while-loop after the projectile drops back to the ground again. One simple way to detect this is to check whether the value of position falls below zero. So, we keep running the while-loop while .
How far can the projectile go?
Try changing the initial velocity of the projectile and the drag coefficient to see how the trajectory changes.
Keeping the initial velocity magnitude fixed, find the initial velocity angle that will give the maximum range for a specific drag coefficient!
Trivial
If you set the coefficient of drag to zero, can you get back the same trajectory you learned in Physics? Try checking the range of the projectile against the closed-form solution.