【发布时间】:2020-06-21 22:21:15
【问题描述】:
我需要以下方面的帮助:
L2=10^9;
nP=10^6;
x(:,1)=L2.*rand(nP,1);
y(:,1)=L2.*rand(nP,1); % Select initial partice position randomly (only positive values) NS
z(:,1)= L2.*rand(nP,1);
for ip=1:nP
r1(ip,1)= sqrt(x(ip,1)^2 + y(ip,1)^2 + z(ip,1)^2); % distance from (0,0,0) for each iteration
while r1(ip,1)>=L2
x(ip,1)=L2.*rand(1,1);
y(ip,1)=L2.*rand(1,1); % Select initial partice position randomly (only positive values) NS
z(ip,1)=L2.*rand(1,1);
r1(ip,1)= sqrt(x(ip,1)^2 + y(ip,1)^2 + z(ip,1)^2);
end
end
我面临的问题是:
1) 由于半径 L2 非常大,当我将 L2 与 rand 相乘时,我得到的值非常接近盒子的边界(例如 10^8),我希望粒子均匀分布在球体周围。
2) 同样在我运行 while 命令后,生成的分布不再均匀。
【问题讨论】:
-
为什么说点接近边界?为什么你说它们不是随机分布的?你的代码对我来说看起来是正确的。我认为您没有正确评估结果(除了我认为是拼写错误,因为存在一些语法错误)。
-
注意
10^9最好写成1e9。 -
我更正了您代码中的两个语法错误,请确保它与您正在运行的代码匹配。
-
如果您有一个球体,那么您可能希望使用球坐标生成随机点。给定球体的形状,靠近外边界的体积比靠近中心的体积大得多。对边界的一些偏见是可以预料的。
-
这个问题是询问分布在球体surface上的点,但你可能会找到一些灵感:stackoverflow.com/questions/30158433/…
标签: matlab random distribution