我试图将具有创建的网格尺寸的值传递到一个迭代数组中,该迭代数组每次迭代(X,Y,0)仅包含3个值。我将变量视为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这是我生成的。