Python Projectile

Through these lessons we will build a code that will allow you to visualize the trajectory of your rocket. You will be able to manipulate several variables to see their effects on the path and make predictions based thereof.

Let’s get started!

Python Projectile 1.1: 1-D Motion, 1 Point

We will begin by building a “snapshot” of a vertically thrown ball in the air at a specific time with a specific initial velocity.

We need this equation: y= (v0)(t)-(0.5g)(t 2 ).

Let’s define these variables. v0= initial velocity, g= gravity, t= time, y= height of ball. Now enter them in trinket:

➢ Line 3: v0=5
➢ Line 4: g=9.81
➢ Line 5: t= 0.6
➢ Line 6: y=v0*t-0.5*g*t**2

Click “Run.” You should see this statement in the console:

“ At t=0.600 s, a ball with 
initial velocity v0=5.000 m/s
is located at the height 1.234 m.”

And you should see a ball on the plot at (0.6, 1.234).

You can experiment with different values for t. What happens to y when t gets too large? Why?

Python Projectile 1.2: 1-D Motion, Height versus Time

This visualization will show the height of the ball as a function of time. You will see the height of the ball at several increments of time, not simply as a “snapshot” of one specific moment.

Enter the following:

➢ Line 3: v0=5
➢ Line 4: g=9.81

Now, because we now want to show a function, not just a point, we need to use an array. To do this, enter the following.

➢ Line 5: t=np.linspace(0.0,0.6,10,endpoint=True)

Let’s examine this expression. All we are doing is giving the computer some guidelines as to how we want to see our function plotted. There are 3 numbers in this line: 0.0, 0.6, 10. The first one tells the computer where we want the graph to start, the second one tells it where to stop, and the last entry says how many points we want to see represented. “Endpoint=True” means simply that we want our “stop” value (0.6) to be included in the visualization. This will all become clear once we see the graph.

Now enter:

➢ Line 11: y=v0*t-0.5*g*t**2
➢ Line 17: plt.plot(t,y,'o')

We have seen Line 11’s equation before. We are plotting the same equation, just showing more points this time.

Line 17 tells the computer to plot what we have written. You can see what is being included in the graph: t, (time) and y (vertical height). The last term,‘o’, just tells the computer that we want each point to be represented with an “o.” You can change this term to an “x” to change the marker shape.

Click “Run.”

You should see a list of points in the console. These are the points generated by your expression in Line 5. You can see that our original point from Projectile 1.1 is the last point in this graph, at (0.6, 1.234).

Remember, this ball is still going straight up in the air. This graph looks like a curve because the x-axis is time, not distance. As time progresses, the ball increases in height, reaches a maximum, then begins to fall back down to Earth.

Python Projectile 1.3: 1-D Motion, Vertical Representation

Now we will write some code that will make our visualization actually look like a ball being thrown vertically: increasing and decreasing along a vertical line. To do this, we will begin with about the same code that we used in 1.2.

Enter the following:

➢ Line 16: plt.plot(x,y,'o')

This linspace expression looks very similar to the one we wrote in 1.2, however, we are now comparing y (vertical height) with x (horizontal distance). Because we are throwing the ball straight up, the horizontal distance = 0. In other words, there is no horizontal motion going on here. Because of this, the array will not vary from the y-axis.

Click “Run.”

You can clearly see the path of the ball going up and down along the same plane.

Now go to:

➢ Line 15: #plt.plot(t,y,'o')

This is already written in Line 15. Take out the “#” at the beginning of the expression. This pound sign makes the line a “comment.” Anything that comes after a “#” is effectively invisible to the computer. By removing the pound sign, that line becomes visible to the computer. Your new Line 15 should look like this:

➢ plt.plot(t,y,'o')

Click “Run.”

Now you can see the graph from 1.2 that shows height over time (t,y,’o’) in comparison to height over zero distance (x,y,'o'). These graphs are describing the same ball, the same 1-D motion.

Great job! Now we’re ready for 2-D motion!

Python Projectile 2: 2-D Motion, Vertical Representation

In this lesson we will create a visualization of a ball as if it were being thrown- like a baseball or a football. This means there will be a vertical and horizontal component, making it 2-D.

Enter the following:

➢ Line 4: t=np.arange(0.0, 1.03, 0.01)

We use this expression to tell the computer we have a series of points we want to be displayed in a function, an array. “arange” is almost exactly like “linspace” (start, stop, number of points), except for the last number. “arange” does not require “number of points,” rather, the increment of each point- in other words, “the step.” Our step here is 0.01, meaning, each point is going to be 0.01 greater than the last.

Enter the following:

➢ Line 7: v0_x=5
➢ Line 8: v0_y=5

These are the initial velocities for the y and x directions. In Projectile 1, we only had velocity in the y direction.

Enter the following:

➢ Line 11: x=v0_x*t
➢ Line 12: y=v0_y*t-0.5*g*t**2

Line 12 we have seen before. Line 11 is new. We need an equation to describe the motion in the x direction. Because there is no gravity horizontally, this equation is similar than the one used to describe the vertical velocity (Line 12).

Let’s add some labels so we know what we are looking at.

Enter the following:

➢ Line 16: plt.xlabel('horizontal distance')
➢ Line 17: plt.ylabel('vertical height')
➢ Line 18: plt.title('ball projectile')

Click “Run.”

Now you can see the trajectory of the ball being thrown. This arc is the predicted path of the ball.

Python Projectile 3: 2-D Motion, Drag Model

In Projectile 3, we will compare our predicted path from Projectile 2 with a drag model. The drag model will describe the predicted path of our ball, while also taking into account resistance due to friction (drag) of the air. This will be a more accurate representation of the true trajectory of the projectile.

Let’s set up some parameters for our drag graph.

Enter the following values for the following variables:

➢ Line 5: -for “number_of_points”- enter “100”
➢ Line 7: -for “ground_y”- enter “0.0”
➢ Line 9: -for “m”- enter “1.0”
➢ Line 11: -for “k”- enter “0.1”
➢ Line 13: -for “g”- enter “9.81”
➢ Line 16: -for “vx0”- enter “10.0”
➢ Line 18: -for “vy0”- enter “10.0”

We have some new variables in what you just entered. There are comments labeling them already in the code for reference. For example, “m” stands for mass, and “k” stands for the drag constant. It might also interest you that “mok” -stated later in the pre-written code- stands for m/k, or mass divided by the drag constant.

Now we need to plot the projectile without drag (from Projectile 2), considering x and y directions, as well as the projectile with drag, considering x and y directions. We’ll graph these on the same screen to compare them. We will write the equations for these graphs.

Enter the following:

➢ Line 23: (TAB) return vx0*t
➢ Line 26: (TAB) return vy0*t-0.5*g*t**2
➢ Line 30: (TAB) x=mok*vx0*(1.0-exp(-t/mok))
➢ Line 31: (TAB) return x
➢ Line 35: (TAB) y=-mok*g*t+mok*(vy0+mok*g)*(1.0-exp(-t/mok))
➢ Line 36: (TAB) return y

Click “Run.”

Now you will see that the graph of the projectile without taking into consideration the friction of the air extends farther than that graph which does include it. Because we have air in our atmosphere, and therefore drag, the lower projectile is a more accurate model.

You may go back through your code and make any changes you like to the parameters. In many cases, these values have been chosen objectively to give you a reasonable visualization. Try different numbers in these equations to make educated predictions about the flight of your rocket.

You can do this. There is a place for you in the computer science world. You are worthy, capable, and needed in this field.

Enjoy!