我正在尝试在球体上生成随机、均匀分布的点。但是,代码正在创建似乎创建磁盘的点。我认为问题在于“phirand”定义。那里的数学不正确吗?我在 Matlab 中使用了相同的代码,它在其中工作。
代码:
import numpy as np
import pylab
from scipy.integrate import odeint
import matplotlib.pyplot as plt
#import random
import mpl_toolkits.mplot3d.axes3d as p3
import random as rand
particlecount = 10 ## of particles to generate energies for energy generation
binsize = 15 #Determines bin size for historgram of electron energies
RStart = 0.02
phi1 = 0
phi2 = 180
phi1rad = phi1*(np.pi/180)
phi2rad = phi2*(np.pi/180)
#Generate random positions for each particle between s1 and s2
ICPositions = np.array([])
for i in range(0,particlecount):
#In Spherical: Generates random position with boundaries of: S1<r<S2
thetarand = (2*np.pi)*rand.uniform(0,1) #Random # generation for component y between s1 and s2
phirand = np.arcsin((np.sin(phi2rad) - np.sin(phi1rad))*rand.uniform(0,1) + np.sin(phi1rad))
xrand = RStart*np.sin(phirand)*np.cos(thetarand)
yrand = RStart*np.sin(phirand)*np.sin(thetarand)
zrand = RStart*np.cos(phirand)
randArray = np.array([xrand,yrand,zrand])
randArray = np.array(randArray,dtype = float)
if ICPositions.size == 0:
ICPositions = np.array([randArray])
else:
ICPositions = np.append(ICPositions,[randArray],axis = 0)
print(ICPositions)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(ICPositions[:,0],ICPositions[:,1],ICPositions[:,2],c='r',marker='o')
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
plt.show()
慕斯王
相关分类