I am trying write a code to work with arrays. I have arrays for variables and I am trying to call certain numbers from those variables for a defined function. I have attached a simplified snip of what I am trying to accomplish. The code works fine when I just have the variables as a single number but when I incorporate arrays it doesn't work.
import numpy as np
noe = 3
# E of members ksi
E = 29000*np.ones((1, noe))
# Area of members in^2
A = 1000*np.array([20, 30, 20])
# length of members in
L = np.array([144, 240, 144])
# moment of inertia of members in^4
I = np.array([600, 1200, 600])
def k_frame_local_6x6(E, I, A, L):
return (E/L)*np.array([[A, 0, 0, -A, 0, 0], [0, (12*I)/(L**2), (6*I)/L, 0, (-12*I)/(L**2), (6*I)/L], [0, (6*I)/L, 4*I, 0, (-6*I)/L, 2*I], [-A, 0, 0, A, 0, 0], [0, (-12*I)/(L**2), (-6*I)/L, 0, (12*I)/(L**2), (-6*I)/L], [0, (6*I)/L, 2*I, 0, (-6*I)/L, 4*I]])
m=k_frame_local_6x6(E[0, 1], I[0, 1], A[0, 1], L[0, 1])
print(m)
The Error I receive is "IndexError: too many indices for array"
When I manually enter the values I am trying to get the function to read it works, that looks like this:
def k_frame_local_6x6(E, I, A, L):
return (E/L)*np.array([[A, 0, 0, -A, 0, 0], [0, (12*I)/(L**2), (6*I)/L, 0, (-12*I)/(L**2), (6*I)/L], [0, (6*I)/L, 4*I, 0, (-6*I)/L, 2*I], [-A, 0, 0, A, 0, 0], [0, (-12*I)/(L**2), (-6*I)/L, 0, (12*I)/(L**2), (-6*I)/L], [0, (6*I)/L, 2*I, 0, (-6*I)/L, 4*I]])
m=k_frame_local_6x6(29000, 1200, 30000, 240)
print(m)
and the results I get are:
[[ 3.62500000e+06 0.00000000e+00 0.00000000e+00 -3.62500000e+06
0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 3.02083333e+01 3.62500000e+03 0.00000000e+00
-3.02083333e+01 3.62500000e+03]
[ 0.00000000e+00 3.62500000e+03 5.80000000e+05 0.00000000e+00
-3.62500000e+03 2.90000000e+05]
[ -3.62500000e+06 0.00000000e+00 0.00000000e+00 3.62500000e+06
0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 -3.02083333e+01 -3.62500000e+03 0.00000000e+00
3.02083333e+01 -3.62500000e+03]
[ 0.00000000e+00 3.62500000e+03 2.90000000e+05 0.00000000e+00
-3.62500000e+03 5.80000000e+05]]
One thing I have just noticed is with my ones array I have two sets of brackets:
E = 29000*np.ones((1, noe))
and the result is:
array([[ 29000., 29000., 29000.]])
However with the rest of the arrays I only get one bracket set:
A = 1000*np.array([20, 30, 20])
gives me:
array([20000, 30000, 20000])
Let me know if I need to clarify anything else. Thank you all!!!
A,IandLare one-dimensional arrays, so eg.A[0, 1]fails. Take a look at NumPy Indexing