Trajectory
Overview
The trajectory function is a simulation tool that models the flight path of a projectile using Euler's method. This method is a numerical technique used to approximate solutions to differential equations, which in this context, helps calculate the position of a projectile over time. The function takes into account the projectile's launch angle, initial speed, and other parameters such as the time step and gravitational acceleration, to simulate its path until it hits the ground.
Building a Simulation with Euler’s Method
The following Python code demonstrates how to build a simulation of a projectile's trajectory using Euler's method:
def trajectory(theta, speed=20, height=0, dt=0.01, g=-9.81): #1
vx = speed * cos(pi * theta / 180) #2
vz = speed * sin(pi * theta / 180)
t, x, z = 0, 0, height
ts, xs, zs = [t], [x], [z] #3
while z >= 0: #4
t += dt #5
vz += g * dt
x += vx * dt
z += vz * dt
ts.append(t)
xs.append(x)
zs.append(z)
return ts, xs, zs #6
#1 Additional inputs: the time step dt, gravitational field strength g, and angle theta (in degrees)
#2 Calculates the initial x and z components of velocity, converting the input angle from degrees to radians
#3 Initializes lists that hold all the values of time and the x and z positions over the course of the simulation
#4 Runs the simulation only while the cannonball is above ground
#5 Updates time, z velocity, and position. There are no forces acting in the x direction, so the x velocity is unchanged.
#6 Returns the list of t, x, and z values, giving the motion of the cannonball
Explanation
- Inputs: The function accepts several parameters, including the launch angle (
theta), initial speed, initial height, time step (dt), and gravitational acceleration (g). - Velocity Components: The initial velocity components in the x and z directions are calculated by converting the launch angle from degrees to radians.
- Initialization: Lists are initialized to store the time (
ts), x positions (xs), and z positions (zs) throughout the simulation. - Simulation Loop: The loop runs as long as the projectile is above the ground (
z >= 0), updating the time, z velocity, and positions at each step. - Output: The function returns the lists of time, x, and z values, representing the projectile's motion.
Additional Metrics
Beyond the basic trajectory, we can calculate other metrics of interest, such as the hang time and maximum height of the projectile. These can be derived from the simulated trajectory data:
def hang_time(traj):
return traj[0][-1] #1
def max_height(traj):
return max(traj[2]) #2
#1 Total time in the air is equal to the last time value, the time on the clock when the projectile hits the ground.
#2 The maximum height is the maximum among the z positions, the third list in the trajectory output.
Explanation
- Hang Time: This function calculates the total time the projectile stays in the air by returning the last time value from the trajectory data.
- Maximum Height: This function finds the maximum height reached by the projectile by returning the highest value from the z positions in the trajectory data.
To optimize these metrics, such as finding the best launch angle for maximum hang time or height, one can explore how varying the parameters affects the results.
FAQ (Frequently asked questions)
What does the trajectory function simulate?
What parameters does the trajectory function require?
When does the trajectory function stop calculating the projectile's position?
How is the trajectory function implemented using Euler's method?
What additional inputs does the trajectory function take?
How are the initial velocity components calculated in the trajectory function?
What does the trajectory function return?