Python: index 5324 is out of Bounds for Axis 0 with size 5040

Asked

Viewed 540 times

1

Hello, everybody,

I have a Python code that reads the 5040 numbers of a TXT file to perform a simulation, as shown in Figure 1. Then the code creates a B(s) function, which is used in the commands of the Figure 2 (I skipped a part of the code between Figure 1 and Figure 2). However, when I click on Run module, Python Shell shows the error message IndexError: index 5324 is out of bounds for axis 0 with size 5040, which is best seen in Figure 3.

I’m a beginner with Python and although I’ve encountered similar errors with Numpy’s Index, no solution worked for my code.

Can someone help me? Thank you in advance, Marcos Miotti

Figure 1 - Reading TXT File and Function B(s). Figura 1

Figure 2 - Use of function B(s). Figura 2

Figure 3 - Python Shell error message. Figura 3

  • see if taking out your Dropbox file works

  • No, the same error appears.

  • I’ve already solved the problem.

  • Hi @Marcospaulomiotti ! I was editing the answer and didn’t realize that you had already solved the problem :-)

  • Milestones, avoid pasting code, listings, and errors like images: images are not text, and are only readable by people with normal vision. If you paste text, people with special needs can have the same read as audio, stackoverflow and google will be able to index your problem by Chace words in your code, and all- including users with normal vision, will be able to copy snippets of your code to a Python prompt or a text editor, to reproduce your problem and thus be able to solve it.

2 answers

0

I discovered the problem: the program tries to search the TXT file for a value at 5324th position, but there are only 5040 numbers. So, I reduced the value of the sa little. In the first code above, I wrote

while s <= 0.524 and v >= 0.0:     # MOT position in the magnetic field files

So I just switched to

while s <= 0.50 and v >= 0.0:     # MOT position in the magnetic field files

For the purposes of my code, this solution worked well. :)

0

As the space variable s can reach a maximum value of 0.524 in looping:

while s <= 0.524 and v >= 0

Within the function B(s) (which calculates the magnetic field as a function of space), this space variable is multiplied by the constant 1e4, so the maximum value that it can assume within that function is 5240 (0.524*10000).

However, the array Bfield only has 5040 elements, which were read from the file.

The error reported IndexError (array index out of limit) indicates that the algorithm is trying to access element 5234 of an array of 5040 elements, so at some point the space value s must be assuming a value of 0.5234.

The possibilities for solving the problem are: or restrict the space value in the looping while for a value less than 0.504, or check the possibility to add more values to the file containing the magnetic field data to meet all possibilities of space variation in s.

But to apply one of these solutions, it is necessary to check first if they meet the need of your program.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.