为什么我的 python 循环每次 运行 都会给出不同的数组,以及如何让它每次都产生相同的结果?
Why does my python loop give a different array every run, and how to make it produce the same result everytime?
你可以看到下面的循环,它每次给出不同的图,20次运行中大约有1次是我想要的结果,这是一个三角傅里叶级数。如何做到每次结果都一样?
L=1
N=1000
#Array for x
x=np.linspace(-3*L,3*L,N+1)
#Array for sum
s=np.empty(N+1)
#While loop for sum
i=1
while(i<N+1):
s=(1/((2*i-1)**2))*(np.cos((2*i-1)*np.pi*x/L))+s
i=i+1
print(s)
#f(x)
y=(L/2)-(4*L/((np.pi)**2))*s
#Settings for plot
plt.plot(x,y)
这一行
s=np.empty(N+1)
生成一个充满未定义(任意)值的数组。您应该改用一个填充零的新数组:
s=np.zeros(N+1)
正如所指出的,np.empty
给您带来了问题。初始打印为 s
:
的循环
In [26]: L=1
...: N=10
...:
...: #Array for x
...: x=np.linspace(-3*L,3*L,N+1)
...: #Array for sum
...: s=np.empty(N+1)
...: print(s)
...: i=1
...: while(i<N+1):
...: s=(1/((2*i-1)**2))*(np.cos((2*i-1)*np.pi*x/L))+s
...: i=i+1
...:
[-3. -2.4 -1.8 -1.2 -0.6 0. 0.6 1.2 1.8 2.4 3. ]
In [27]: s
Out[27]:
array([-4.20872131, -2.15330145, -1.06005191, -1.93994809, -0.84669855,
1.20872131, 0.35330145, 0.46005191, 2.53994809, 2.64669855,
1.79127869])
同样的事情,但 np.zeros
:
In [28]: L=1
...: N=10
...:
...: #Array for x
...: x=np.linspace(-3*L,3*L,N+1)
...: #Array for sum
...: s=np.zeros(N+1)
...: print(s)
...: i=1
...: while(i<N+1):
...: s=(1/((2*i-1)**2))*(np.cos((2*i-1)*np.pi*x/L))+s
...: i=i+1
...:
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
In [29]: s
Out[29]:
array([-1.20872131, 0.24669855, 0.73994809, -0.73994809, -0.24669855,
1.20872131, -0.24669855, -0.73994809, 0.73994809, 0.24669855,
-1.20872131])
但是你不需要迭代。而是创建 i
值范围,并构造一个二维结果(针对 (10,) i
:
广播 (11,1) x
In [30]: i = np.arange(1,N+1)
In [31]: S = (1/((2*i-1)**2))*(np.cos((2*i-1)*np.pi*x[:,None]/L))
In [32]: S.shape
Out[32]: (11, 10)
In [33]: s.shape
Out[33]: (11,)
然后在 i
维度求和:
In [34]: S.sum(axis=1)
Out[34]:
array([-1.20872131, 0.24669855, 0.73994809, -0.73994809, -0.24669855,
1.20872131, -0.24669855, -0.73994809, 0.73994809, 0.24669855,
-1.20872131])
你可以看到下面的循环,它每次给出不同的图,20次运行中大约有1次是我想要的结果,这是一个三角傅里叶级数。如何做到每次结果都一样?
L=1
N=1000
#Array for x
x=np.linspace(-3*L,3*L,N+1)
#Array for sum
s=np.empty(N+1)
#While loop for sum
i=1
while(i<N+1):
s=(1/((2*i-1)**2))*(np.cos((2*i-1)*np.pi*x/L))+s
i=i+1
print(s)
#f(x)
y=(L/2)-(4*L/((np.pi)**2))*s
#Settings for plot
plt.plot(x,y)
这一行
s=np.empty(N+1)
生成一个充满未定义(任意)值的数组。您应该改用一个填充零的新数组:
s=np.zeros(N+1)
正如所指出的,np.empty
给您带来了问题。初始打印为 s
:
In [26]: L=1
...: N=10
...:
...: #Array for x
...: x=np.linspace(-3*L,3*L,N+1)
...: #Array for sum
...: s=np.empty(N+1)
...: print(s)
...: i=1
...: while(i<N+1):
...: s=(1/((2*i-1)**2))*(np.cos((2*i-1)*np.pi*x/L))+s
...: i=i+1
...:
[-3. -2.4 -1.8 -1.2 -0.6 0. 0.6 1.2 1.8 2.4 3. ]
In [27]: s
Out[27]:
array([-4.20872131, -2.15330145, -1.06005191, -1.93994809, -0.84669855,
1.20872131, 0.35330145, 0.46005191, 2.53994809, 2.64669855,
1.79127869])
同样的事情,但 np.zeros
:
In [28]: L=1
...: N=10
...:
...: #Array for x
...: x=np.linspace(-3*L,3*L,N+1)
...: #Array for sum
...: s=np.zeros(N+1)
...: print(s)
...: i=1
...: while(i<N+1):
...: s=(1/((2*i-1)**2))*(np.cos((2*i-1)*np.pi*x/L))+s
...: i=i+1
...:
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
In [29]: s
Out[29]:
array([-1.20872131, 0.24669855, 0.73994809, -0.73994809, -0.24669855,
1.20872131, -0.24669855, -0.73994809, 0.73994809, 0.24669855,
-1.20872131])
但是你不需要迭代。而是创建 i
值范围,并构造一个二维结果(针对 (10,) i
:
x
In [30]: i = np.arange(1,N+1)
In [31]: S = (1/((2*i-1)**2))*(np.cos((2*i-1)*np.pi*x[:,None]/L))
In [32]: S.shape
Out[32]: (11, 10)
In [33]: s.shape
Out[33]: (11,)
然后在 i
维度求和:
In [34]: S.sum(axis=1)
Out[34]:
array([-1.20872131, 0.24669855, 0.73994809, -0.73994809, -0.24669855,
1.20872131, -0.24669855, -0.73994809, 0.73994809, 0.24669855,
-1.20872131])