需要更改尺寸以计算方程式

时间:2019-03-15 14:56:18

标签: python arrays numpy multidimensional-array physics

输入


def Gaussan_wave_packet():

    quantum_number = 500
    x = np.linspace(0,100,1000).astype(complex).reshape(1000,1)
    n = np.arange(1,quantum_number+1).reshape(1,500)
    x0 = 50, a = 5, l = 1, m = 0.511*10**6, L = x[-1], hbar = 6.58211951*10**(-16)
    A = (1/(4*a**2))**(1/4.0)

    psi_x0 = (np.exp((-(x - x0)**2)/(4*a**2))*np.exp(1j*l*x)).reshape(len(x),1)

    A = ( 1/(np.sqrt(np.trapz((np.conj(psi_x0[:,0])*psi_x0[:,0]), x[:,0]))))
        psi_x0_normalized = A*psi_x0[:,0].reshape(1000,1) #(1000,1)

    phi = ( np.sqrt( 2/L ) * np.sin( (n*x*np.pi) /L ) )  #(1000,500)

    En = ( ( np.power(n,2))*(np.pi**2)*(hbar**2))/(2*m*L**2) #(1,500)

    Cn = np.trapz( ( np.conj(phi)*psi_x0_normalized[:,0] ), x[:,0] )

输出


Scalar A: (0.28246850458110645+0j)
Psi0 Normalized: (1000, 1)
Phi: (1000, 500)
En: (1, 500)
/Gaussan_wave_packet", line 48, in Gaussan_wave_packet
Cn = np.trapz( ( np.conj(phi)*psi_x0_normalized[:,0] ), x[:,0] )# Needs to be (1,500)
ValueError: operands could not be broadcast together with shapes (1000,500) (1000,) 

想法

我不确定这里出了什么问题。我所有的变量似乎都具有正确的尺寸和适当的值。如输出所示,形状正确,值也正确。我正在从MATLAB中的编程问题转到python,但我不确定该值(1000,)的来源,因为它与matlab有很大不同。我是不是索引正确,逐元素乘法还是需要for循环。我不确定该怎么做。我需要Cn才能成型(1,500)。

1 个答案:

答案 0 :(得分:0)

在将phi和psi_x0_normalized都相乘之后,必须应用数组索引。我还使用了for循环将Cn设置为正确的形状,然后根据需要将其重塑为具有多列的向量。

Q = np.zeros((quantum_number,1))
Cn = np.zeros((quantum_number,1))

Q = (np.conj(phi)*psi_x0_normalized)

print("Q: " + str(Q.shape))
#print("Q: " + str(Q))
for i in range(0,quantum_number):
    Cn[i] = np.trapz( Q[:,i], x[:,0] )# Needs to be (1,500)

Cn = Cn.reshape(1,500)
print("Cn: " + str(Cn.shape))