【发布时间】:2018-05-01 14:54:09
【问题描述】:
我尝试确定点位于半径为 1 的圆内的正方形内的概率。首先,我在圆内生成 N 个随机点,然后检查每个点是否 Ox 和 Oy 坐标都为分别小于宽度的一半高度。我知道答案接近 2/Pi,但我得到的数字接近 0,78.. 这不好。
`close all;clear all;clc;
%length of width and height
a=sqrt(2);
%radius of circle (it is 1)
raza=a*sqrt(2)/2;
%total number of points
N=1000;
%Here I generate my N random numbers inside the circle
theta = 2*pi*rand(1,N);
r = rand(1,N);
x = r.*cos(theta);
y = r.*sin(theta);
%here I count how many point are inside the square that is inscribed in the
%circle
cont = 0;
for i=1:N
if x(i) >= -a/2 && x(i) <= a/2 && y(i) >= -a/2 && y(i) <= a/2
cont = cont + 1;
end
end
%Here i get sth closer to 0,78...not 2/Pi(0,63..)
cont/N
我是否以错误的方式生成点,或者我不正确地计算正方形内的点?
`
【问题讨论】:
-
你的随机点分布不均匀。靠近圆心,它们会变得更密集。
-
谢谢!这就是问题所在。在我修改了要以这种方式计算的半径后: r = sqrt(rand(1,N)) 它完美地工作!
标签: matlab probability