Python Advanced #6 - Using Python functions for Physics problem to determine distance traveled based on velocity and angle

To code this, we first need to understand how the computations are performed.

The first step is to break the velocity into its x and y components.  The x-component is determined by multiplying the velocity by the cosine of the angle from the horizontal.  The y-component is determined by multiplying the velocity by the cosine of 90 degrees minus that angle.

So, if the angle from the horizontal is 30 degrees, the x-component is derived by using cosine(30), and the y-component is derived by using cosine(60).

To get the travel time, we start by dividing the y-component of the velocity by 32.2 ft/s^2.  This is derived by the following:

final velocity = initial velocity + acceleration * time

At the highest point, the y component of the velocity is zero.  This is also the halfway point of the travel time.  Setting final velocity to zero and rearranging the variables gives the following:

time = initial velocity / acceleration

On the way up the acceleration is actually deceleration.

Remember to double the time since we are only computing the time to get to the halfway point.  In other words, it needs to come back down.

We can then solve for the distance traveled by multiplying the time by the x component of the velocity.  Note that we are neglecting the impact of the forces affecting the x component of the velocity.

The Python coding for this is shown below.  In the code, we use three functions to determine the following:

1.)  x and y components of the velocity
2.)  total travel time
3.)  distance traveled

Also, note that the result of the functions are being sent to a different function.


The result of running the program with 30 degrees and 100 ft/s is given below:



Comments