Using Python to calculate projectile motion with resistance

In summary: y[i+1] = ((2*y[i]-y[i-1]) + (f * y[i-1])) - g*(dt**2) / (1 + f) # ...& y[i+1] i = i+1 #increment i for next iteration x = x[0:i+1] #truncate x array y = y[0:i+1] #truncate y array
  • #1
Ryaners
50
2
I'm trying to write a program to calculate the trajectory of a projectile with air resistance. I've made the following function which calculates the position of the particle based on a given launch angle and initial velocity:

Python:
def traj_fr(angle, v0):             #function that computes trajectory for some launch angle & velocity
    vx0 = math.cos(angle)*v0        #compute x components of starting velocity
    vy0 = math.sin(angle)*v0        #compute y components of starting velocity
    x = np.zeros(len(time))         #initialise x array
    y = np.zeros(len(time))         #initialise y array
  
    x[0],y[0] = 0,0     #initial position at t=0s, ie motion starts at (0,0)
    x[1],y[1] = x[0] + vx0*(2*dt), y[0]+vy0*(2*dt)  #calculating 2nd elements of x & y based on init velocity
  
    i=1
    while y[i]>=0:  #loop continuous until y becomes <0, ie projectile hits ground
        f = 0.5 * gamm * (h - y[i]) * dt        #intermediate 'function'; used in calculating x & y vals below
        x[i+1] = ((2*x[i]-x[i-1]) + (f * x[i-1])) / (1 + f)                 #numerical integration to find x[i+1]...
        y[i+1] = ((2*y[i]-y[i-1]) + (f * y[i-1]) - g*(dt**2) ) / (1 + f)      # ...& y[i+1]
        i = i+1     #increment i for next iteration
  
    x = x[0:i+1]        #truncate x array
    y = y[0:i+1]        #truncate y array
    return x, y, (dt*i), x[i]       #return x, y, flight time, range of projectile

For some reason I'm getting a traceback whenever the angle 'fed' to the function is greater than 1 radian. If I restrict the input angles to anything smaller than π/3, it works. It doesn't for π/2. I can't figure out why this is happening; I've written an almost-identical program to calculate trajectory without air resistance & π/2 isn't a problem for that. Can anyone shed a bit of light on this? This is the error I'm getting:

IndexError: index 10000 is out of bounds for axis 0 with size 1000

Here is the whole program, if that helps:

Python:
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
import math
import scipy.constants as const

g = const.g     #gravitation constant
dt = 1e-3       #integration time step (delta t)
v0 = 40         #initial speed at t=0

angle = math.pi / 4     #launch angle in radians
time = np.arange(0,10, dt)      #create time axis

gamm = 0.005    #gamma (used to compute f, below)
h = 100         #height (used to compute f, below)

def traj_fr(angle, v0):             #function that computes trajectory for some launch angle & velocity
    vx0 = math.cos(angle)*v0        #compute x components of starting velocity
    vy0 = math.sin(angle)*v0        #compute y components of starting velocity
    x = np.zeros(len(time))         #initialise x array
    y = np.zeros(len(time))         #initialise y array
  
    x[0],y[0] = 0,0     #initial position at t=0s, ie motion starts at (0,0)
    x[1],y[1] = x[0] + vx0*(2*dt), y[0]+vy0*(2*dt)  #calculating 2nd elements of x & y based on init velocity
  
    i=1
    while y[i]>=0:  #loop continuous until y becomes <0, ie projectile hits ground
        f = 0.5 * gamm * (h - y[i]) * dt        #intermediate 'function'; used in calculating x & y vals below
        x[i+1] = ((2*x[i]-x[i-1]) + (f * x[i-1])) / (1 + f)                 #numerical integration to find x[i+1]...
        y[i+1] = ((2*y[i]-y[i-1]) + (f * y[i-1]) - g*(dt**2) ) / (1 + f)      # ...& y[i+1]
        i = i+1     #increment i for next iteration
  
    x = x[0:i+1]        #truncate x array
    y = y[0:i+1]        #truncate y array
    return x, y, (dt*i), x[i]       #return x, y, flight time, range of projectile

x,y,duration,distance = traj_fr(angle,v0)   #define variables for output of traj_fr function

print 'Distance: ' , distance
print 'Duration: ' , duration

n = 5
angles = np.linspace(0, math.pi/2, n)   #generate array of n angles
print 'Angles: ' , angles
maxrange = np.zeros(n)                  #generate array of n elements to take range from traj_frfor i in range(n):      #loop to run angles through traj_fr function & populate maxrange array with distance results
    x,y,duration,maxrange[i] = traj_fr(angles[i], v0)

angles = angles / 2 / math.pi * 360       #convert radians to degrees
print 'Launch Angles: ', angles

print 'Optimum Angle: ', angles[np.where(maxrange==np.max(maxrange))]

plt.plot(x,y)       #quick plot of x vs y to check trajectory
plt.xlabel('x')
plt.ylabel('y')

I've been tearing my hair out over this for hours. I'm new to programming so this is probably a basic error I don't have the know-how to spot.

Thanks in advance!

Edit: Just to add - the air resistance is height-dependent.
 
Last edited:
Technology news on Phys.org
  • #2
I ran a quick test on it; I believe the problem is that your integration time is too short - that is, the object has not hit the ground after 10 seconds. That is why the size of your position array is too small. Try increasing the range of your time axis.
 
  • Like
Likes Ryaners
  • #3
Fightfish said:
I ran a quick test on it; I believe the problem is that your integration time is too short - that is, the object has not hit the ground after 10 seconds. That is why the size of your position array is too small. Try increasing the range of your time axis.
Ahhh amazing, that's done it! Thank you so much! :)
 

1. How can Python be used to calculate projectile motion with resistance?

Python has built-in libraries such as NumPy and SciPy that allow for mathematical calculations and simulations, making it a powerful tool for calculating projectile motion. By using these libraries and writing code to represent the physical equations of motion, Python can accurately calculate the trajectory of a projectile in the presence of resistance forces.

2. What are the key parameters needed for calculating projectile motion with resistance using Python?

The key parameters needed for calculating projectile motion with resistance using Python include the initial velocity, angle of launch, mass of the projectile, and the resistance force. These parameters are used in the equations of motion to determine the trajectory of the projectile.

3. Can Python handle different types of resistance forces in projectile motion calculations?

Yes, Python can handle different types of resistance forces such as air resistance, friction, and drag. By incorporating the appropriate equations for each type of resistance force, Python can accurately calculate the trajectory of a projectile in the presence of these forces.

4. Is it necessary to have a strong background in mathematics to use Python for calculating projectile motion with resistance?

While a strong background in mathematics can be helpful, it is not necessary to use Python for calculating projectile motion with resistance. Python's built-in libraries and online resources make it accessible for beginners to learn and apply the necessary mathematical concepts for these calculations.

5. Are there any limitations to using Python for calculating projectile motion with resistance?

One limitation of using Python for calculating projectile motion with resistance is the assumption of a constant resistance force throughout the trajectory. This may not accurately represent real-world scenarios where the resistance force may change over time. Additionally, the accuracy of the calculations may also be affected by the precision of the input parameters and the complexity of the equations used.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
4
Views
586
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Introductory Physics Homework Help
2
Replies
39
Views
2K
  • Introductory Physics Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top