如何将列表转换为contou理解的2x2数组

2024-04-19 05:53:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将具有已创建网格维度的值传递到一个迭代数组中,该数组在每次迭代中只包含3个值(X,Y,0)。我将变量视为3d向量,以便可以执行叉积、点积和范数函数,以便python正确处理phi函数。最终的目标是建立一个2体系统的拉格朗日等值线图。你知道吗

我经常遇到的错误是:

TypeError: Input z must be a 2D array.
import matplotlib.pyplot as plt
import numpy as np

#initial values
m1=3.0
m2=1.0
a=1.0
n=1.5*a

#x and y evenly spaced gridpoint values
x=np.arange(-n,n,0.01)
y=np.arange(-n,n,0.01)
X,Y=np.meshgrid(x,y)

mtot=m1+m2
#the distance from the bodies to the center of mass of the system
x1=-(m2/mtot)*a
x2=(m1/mtot)*a
X1=np.array([x1,0,0])
X2=np.array([x1,0,0])

om=np.sqrt(mtot/(a**3)) #omega term
OM=np.array([0,0,om])

phi=[]
#more arrays
r=np.array([X,Y])
R=np.array([])

#gravitational potential function
for i in range(len(x)):
    for j in range(len(y)):
        R=np.array([r[0,i,j],r[1,j,i],0])  
        denom1 = np.linalg.norm(R-X1) 
        denom2 = np.linalg.norm(R-X2)
        term1 = -m1/denom1
        term2 = -m2/denom2
        oa=np.cross(R,OM) 
        term3=0.5*np.dot(oa,oa)
        phi.append([term1+term2-term3]) 

#the line below is the formula I am trying to implement with the loop above
#phi= -m1/abs(r-x1)-m2/abs(r-x2)-0.5*(om**2)*r**2

#omitted code where output is saved to a file. 

PHI=np.array(phi)
PHI=np.reshape(PHI,(300,300)) 

phi[phi<-10]=-10

fig=plt.figure()
ax=fig.add_subplot(111)
ax.contourf(X,Y,phi,20, cmap='jet')
plt.show()

结果应在每个网格点生成有效重力势能的等值线图。Lagrange Contour Plot这是我生成的。你知道吗


Tags: theto网格npplt数组arrayx1