Stephen Ryan
Stephen Ryan

Reputation: 173

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

For my physics degree, I have to take some Python lessons. I'm an absolute beginner and as such, I can't understand other answers. The code is to plot an object's trajectory with air resistance. I would really appreciate a quick fix - I think it has something to do with the time variable being too small but increasing it doesn't help.

import matplotlib.pyplot as plt
import numpy as np
import math # need math module for trigonometric functions

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

angle = math.pi/4 #math.pi = 3.14, launch angle in radians

time = np.arange(0, 10, dt) #time axis
vx0 = math.cos(angle)*v0 # starting velocity along x axis
vy0 = math.sin(angle)*v0 # starting velocity along y axis

xa = vx0*time # compute x coordinates
ya = -0.5*g*time**2 + vy0*time # compute y coordinates

def traj_fric(angle, v0): # function for trajectory

    vx0 = math.cos(angle) * v0 # for some launch angle and starting velocity
    vy0 = math.sin(angle) * v0 # compute x and y component of starting velocity

    x = np.zeros(len(time))   #initialise x and y arrays
    y = np.zeros(len(time))

    x[0], y[0], 0 #projecitle starts at 0,0
    x[1], y[1] = x[0] + vx0 * dt, y[0] + vy0 * dt # second elements of x and
                                              # y are determined by initial 
                                              # velocity
    i = 1
    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

    x = x[0:i+1] # truncate x and y arrays                                                
    y = y[0:i+1]
    return x, y, (dt*i), x[i] # return x, y, flight time, range of projectile

x, y, duration, distance = traj_fric(angle, v0)

fig1 = plt.figure()
plt.plot(xa, ya) # plot y versus x
plt.xlabel ("x")
plt.ylabel ("y")
plt.ylim(0, max(ya)+max(ya)*0.2)
plt.xlim(0, distance+distance*0.1)
plt.show()

print "Distance:" ,distance
print "Duration:" ,duration

n = 5
angles = np.linspace(0, math.pi/2, n)
maxrange = np.zeros(n)

for i in range(n):
    x,y, duration, maxrange [i] = traj_fric(angles[i], v0)

angles = angles/2/math.pi*360 #convert rad to degress

print "Optimum angle:", angles[np.where(maxrange==np.max(maxrange))]

The error is:

File "C:/Python27/Lib/site-packages/xy/projectile_fric.py", line 43, in traj_fric x[i + 1] = (2 * x[i] - x[i - 1] + f * x[i - 1])/1 + f # numerical integration to find x[i + 1]

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

Upvotes: 8

Views: 85979

Answers (5)

user1465073
user1465073

Reputation: 325

in my case it wasn't that obvious, what happened was that I have a big dataframe and the prior step was I did a drop_duplicates() on the pandas and it dropped less than 10 records out of a very big dataframe thus there was a "break" in the index count.

a .reset_index() did the trick to ensure that the indexes are continuous

Upvotes: 0

Kavya Subramanian
Kavya Subramanian

Reputation: 11

The same error happened to me also, when I'm implementing a Mask RCNN model on a custom dataset. Definition of this error is direct, that we are attempting to use an image of the index which is out of bounds.

In my case there are images in the training dataset only, but not in the validation dataset(means for testing, I don't have any images). So I included 1 image for testing, which makes my code run smoothly.

dataset_val = CustomDataset()
dataset_val.load_custom("C:/directory of your dataset path../", "val")
dataset_val.prepare()
print('Test: %d' % len(dataset_val.image_ids))

Here inside the dataset path, I've created a separate folder for training and validation. In the validation folder at least you have to add 1 image. when you will run the print command it shouldn't come to zero. If it's coming this issue will persist.

So try to do this and hopes it helps you.

Upvotes: 1

Prashant Bhardwaj
Prashant Bhardwaj

Reputation: 11

enter code heredef data_to_array(total):
random.shuffle(total)
X = np.zeros((len(total_train), 224, 224, 3)).astype('float')
y = []
for i, img_path in enumerate(total):
    img = cv2.imread('/content/gdrive/My Drive/PP/Training/COVID/COVID-19 (538).jpg')
    img = cv2.resize(img, (224, 224))
    X[i] = img - 1
    if len(re.findall('covid', '/content/gdrive/My Drive/PP/Training/COVID/COVID-19 (538).jpg')) == 3:
        y.append(0)
    else:
        y.append(1)
y = np.array(y)
return X, y

X_train, y_train = data_to_array(total_train) X_test, y_test = data_to_array(total_val)

Upvotes: 1

John R. Strohm
John R. Strohm

Reputation: 7667

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.

Upvotes: 2

Mason Wheeler
Mason Wheeler

Reputation: 84650

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.

Upvotes: 14

Related Questions