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

This is pretty straightforward. When you have a size of 10000, element index 10000 is out of bounds because indexing begins with 0, not 1. Therefore, the 10,000th element is index 9999, and anything larger than that is out of bounds.


Mason Wheeler's answer told you what Python was telling you. The problem occurs in this loop:

while y[i] >= 0: # conditional loop continuous until
# projectile hits ground
    gamma = 0.005 # constant of friction
    height = 100 # height at which air friction disappears
    f = 0.5 * gamma * (height - y[i]) * dt
    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 # and y[i + 1]

    i = i + 1 # increment i for next loop

The simple fix is to change the loop to something like (I don't know Python syntax, so bear with me):

while (y[i] >= 0) and (i < len(time)):

That will stop the sim when you run out of array, but it will (potentially) also stop the sim with the projectile hanging in mid-air.

What you have here is a very simple ballistic projectile simulation, modeling atmospheric friction as a linear function of altitude. QUALITATIVELY, what is happening is that your projectile is not hitting the ground in the time you allowed, and you are attempting to overrun your tracking arrays. This is caused by failure to allow sufficient time-of-flight. Observe that the greatest possible time-of-flight occurs when atmospheric friction is zero, and it is then trivial to compute a closed-form upper bound for time-of-flight. You then use that upper bound as your time, and you will allocate sufficient array space to simulate the projectile all the way to impact.

Tags:

Python