I am trying to create a matrix with that is q by 3. In this case, q = 10. or each row I want the three values to be the results of the trigonometric functions described in my code below.
The problem is that I keep getting an error saying that the list index is out of range. I don't understand why it is saying it is out of range. To my eyes, my loop seems correct. Can anyone tell me what I'm overlooking/doing wrong?
# Input az matrix
az = [142.243258152,116.039625836,80.1585056414,139.614063776,87.2093336287,94.1433825229,35.5599100744,11.0328982848,177.717968103,19.0072693362]
# Construct frame of X matrix
X = [[0 for x in range(10)] for y in range(3)]
# Use az matrix to complete X matrix
f=0
for bear in az:
X[f][0] = (M.cos(bear))**2
X[f][1] = 2*M.cos(bear)*M.sin(bear)
X[f][2] = (M.sin(bear))**2
f=f+1
print X
1 Answer 1
OP's input list az has 10 elements, not 8 as supposed and the ranges of the matrix should be swapped.
Besides, sin and cos functions usually take radians as input, while az seems to contain angles misured in degrees.
This snippet:
from math import radians, cos, sin
# Input az matrix
az = [142.243258152, 116.039625836, 80.1585056414, 139.614063776, 87.2093336287, 94.1433825229, 35.5599100744, 11.0328982848, 177.717968103, 19.0072693362]
# Construct frame of X matrix
X = [[0 for x in range(3)] for y in range(10)]
# Use az matrix to complete X matrix
f=0
for bear in az:
r = radians(bear)
c = cos(r)
s = sin(r)
X[f][0] = c**2
X[f][1] = 2*c*s
X[f][2] = s**2
f=f+1
print(X)
Gives this output:
[[0.6250760791021176, -0.9682065367191874, 0.37492392089788235], [0.19271454590900655, -0.7888615840667916, 0.8072854540909934], [0.029214706063653385, 0.3368157182393228, 0.9707852939363467], [0.5801828858777331, -0.9870576575100736, 0.41981711412226685], [0.0023704299165554724, 0.09725864441922212, 0.9976295700834447], [0.0052204459914281754, -0.14412762309951216, 0.9947795540085718], [0.6617950612456389, 0.9461973539521655, 0.33820493875436103], [0.9633765287676627, 0.3756710933102597, 0.0366234712323373], [0.9984144917844932, -0.07957372378380607, 0.001585508215506806], [0.893927252777247, 0.615861411421014, 0.10607274722275291]]
azarray has 10 elements, not 8.