Issue
I am working on generating a simple sine function defined over the range [0,1] as shown below. However I got an index error when assigning function[i]
. I tried to set x_n = int(x_n)
as some questions with the same error suggest but that only creates further errors.
import numpy as np
function = np.zeros(20)
x_n=np.arange(0,1,0.05) #[0,1]
for i in x_n:
function[i] = np.sin(2*np.pi*i)
Note x_n
has a step of 0.05 because I need to generate 20 values ranging from 0 to 1.
Solution
You don't need to create array zero and insert numbers in this, you can do this with vectorizing magic in NumPy
like below:
x_n=np.arange(0,1,0.05) #[0,1]
np.sin(x_n*np.pi*2)
Output:
array([ 0.00000000e+00, 3.09016994e-01, 5.87785252e-01, 8.09016994e-01,
9.51056516e-01, 1.00000000e+00, 9.51056516e-01, 8.09016994e-01,
5.87785252e-01, 3.09016994e-01, 1.22464680e-16, -3.09016994e-01,
-5.87785252e-01, -8.09016994e-01, -9.51056516e-01, -1.00000000e+00,
-9.51056516e-01, -8.09016994e-01, -5.87785252e-01, -3.09016994e-01])
Answered By - I'mahdi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.