Python: index 5324 is out of bounds for axis 0 with size 5040

1

Hello, everyone,

I have a Python code that reads the 5040 numbers from a TXT file to perform a simulation, as shown in Figure 1 . Next, the code creates a B (s) function, which is used in the Figure 2 commands (I skipped a part of the code between Figure 1 and Figure 2). However, when I click on Run module , the Python Shell displays 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 the Numpy Index, no workaround worked for my code.

Can anyone help me? Thanks in advance, Marcos Miotti

Figure 1 - Reading TXT file and B (s) function.

Figure2-UseoffunctionB(s).

Figure3-ErrormessageinPythonShell.

    
asked by anonymous 25.01.2016 / 21:04

2 answers

0

I discovered the problem: the program tries to look in the TXT file for a value in 5324th position, but there are only 5040 numbers. Thus, I reduced the value of the s slightly. 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 moved to

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

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

    
26.01.2016 / 17:45
0

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

while s <= 0.524 and v >= 0

In the B(s) function (which calculates the magnetic field as a function of space), this space variable is multiplied by the constant 1e4 , so the maximum value 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 reported error IndexError (array index out of bounds) indicates that the algorithm is trying to access element 5234 from an array of 5040 elements, so at some point the value of s space must be assuming a value of 0.5234.

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

But to apply one of these solutions, you need to first check whether they meet the needs of your program.

    
26.01.2016 / 18:03