1

I am having trouble with my indices in a nested for loop. Python is spitting out an index error telling me my index is out of bounds.

Below are my code and subsequent error:

from math import *
import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt
import os
croot = 1
ctip = 1
span = 1
thetaroot = 0
thetatip = 0
a0root = 0.11
a0tip = 0.11
alpha = 0
alpha0root = -2.5
alpha0tip = -2.5
thetaroot = thetaroot * arctan(1.) / 45.
thetatip = thetatip * arctan(1.) / 45.
alpha = alpha * arctan(1.) / 45.
alpha0root = alpha0root * arctan(1.) / 45.
alpha0tip = alpha0tip * arctan(1.) / 45.
n = 10
theta = np.empty(n, dtype = object)
y = np.empty(n, dtype = object)
c = np.empty(n, dtype = object)
cl = np.empty(n, dtype = object)
alp = np.empty(n, dtype = object)
a = np.empty(n, dtype = object)
rhs = np.empty(n, dtype = object)
b = np.empty(n, dtype = object)
a = np.empty(n, dtype = object)
rhs = rhs[:,None]
b = b[:,None]
a = a[:,None]
#
# Define properties at n span stations
#
pi = 4. * arctan(1.)
for i in range(0,n):
 theta[i] = i * pi / (2. * n)
 y[i] = span * 0.5 * cos(theta[i])
 c[i] = croot + (ctip - croot) * y[i] * 2. / span
 alp[i] = alpha + thetaroot - (alpha0root + (alpha0tip - alpha0root + thetaroot - thetatip) * y[i] * 2. / span)
 a[i] = a0root + (a0tip - a0root) * y[i] * 2. / span
pi = 4. * arctan(1.)
# Set up 2n x 2n system of equations for A1, A3 , ... A2n-1
for j in range(0,n):
 mu = c[j] * a[j] / (4. * span); print('mu=',mu)
 rhs[j,0] = alp[j] * sin(theta[j]) * c[j] * a[j] / (4 * span)
 for i in range(0,n):
 l = 2 * i - 1
 b[j,i] = sin(l * theta[j]) * (mu * l + sin(theta[j]))

I then receive the error:

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-70-b5bd95e69bb5> in <module>()
 45 for i in range(0,n):
 46 l = 2 * i - 1
---> 47 b[j,i] = sin(l * theta[j]) * (mu * l + sin(theta[j]))
 48 
 49 
IndexError: index 1 is out of bounds for axis 1 with size 1

How can I effectively call out both indices? In MATLAB, b(j,i) is the normal syntax.

Any help is appreciated, thanks!

Nick Tomlin
29.4k12 gold badges65 silver badges96 bronze badges
asked Dec 15, 2014 at 23:12
3
  • is this numpy - in which case where do you import the relevant libraries Commented Dec 15, 2014 at 23:17
  • Apologies, I've updated my script with the imported libraries. Numpy is being used. Commented Dec 15, 2014 at 23:19
  • b is (n,1). If something close to this worked in MATLAB its because that language expands an array if indexed beyond the current size. In numpy you have to initialize it to the correct final shape. Commented Dec 15, 2014 at 23:34

1 Answer 1

1

The call b = np.empty(n, dtype = object) with n=10 makes a one-dimensional array, but you are indexing into it (b[j,i]) as though it is a two-dimensional array.

To initialize a 10 by 10 array, you would call b = np.empty([n, n], dtype = object).

EDIT: I didn't notice this assignment: b = b[:,None] That creates this:

>>> [[None]
 [None]
 [None]
 [None]
 [None]
 [None]
 [None]
 [None]
 [None]
 [None]]

which is a two-dimensional array, but trying to index past the first element of the inner array (containing just None) is causing your error.

answered Dec 15, 2014 at 23:17
Sign up to request clarification or add additional context in comments.

9 Comments

I'm assuming you mean b = np.empty([n, n], dtype = object) ??
Oops, yes I did mean that.
So, since it is trying to index past the first element, what would you suggest as a viable alternative for the nested loop?
My suggestion was create b as b = np.empty([n, n], dtype = object), then the nested loop should work. (and remove b = b[:,None])
In doing that, I receive an error stating 'too many indeces'. It points to the line containing rhs[j,i] = alp[j] * sin(theta[j]) * c[j] * a[j] / (4 * span)
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.