2016-02-28 102 views
0

两个重叠的圆的面积实际上我有两个相交的圆如图中查找使用蒙特卡洛方法

我想找到分别使用在Matlab蒙特卡罗方法中的每个部分的面积指定。

代码没有正确绘制矩形或圆圈 我想我的x和y的计算出了什么问题,我对解决它的几何方程没有太多的了解,所以我需要关于方程。

enter image description here 这是我到目前为止的代码:

n=1000; 
%supposing that a rectangle will contain both circles so : 
% the mid point of the distance between 2 circles will be (0,6) 
% then by adding the radius of the left and right circles the total distance 
% will be 27 , 11 from the left and 16 from the right 
% width of rectangle = 24 

x=27.*rand(n-1)-11; 
y=24.*rand(n-1)+2; 
count=0; 

for i=1:n 

if((x(i))^2+(y(i))^2<=25 && (x(i))^2+(y(i)-12)^2<=100) 
count=count+1;   
    figure(2); 
    plot(x(i),y(i),'b+') 
    hold on 

elseif(~(x(i))^2+(y(i))^2<=25 &&(x(i))^2+(y(i)-12)^2<=100) 
    figure(2); 
    plot(x(i),y(i),'y+') 
    hold on 

else 
    figure(2); 
    plot(x(i),y(i),'r+') 

end 

end 
+0

究竟什么是你的问题?模糊的*“我需要帮助”*没有解释你想要什么。请描述代码正在做什么,正确和不正确。请描述你想要的。 [帮助]页面提供了很多关于如何写出一个好问题的建议。另见[mcve]。 – AdrianHHH

+0

“我需要帮助”关于几何方程式,我编辑它的任何方式,感谢您的通知 – Suzy

回答

2

这里是我发现的错误:

x = 27*rand(n,1)-5 
y = 24*rand(n,1)-12 

矩形程度是不正确的,如果你使用兰特(N-1)将由(n-1)矩阵给你一个(n-1)。

第一如果:

(x(i))^2+(y(i))^2<=25 && (x(i)-12)^2+(y(i))^2<=100 

大圆的中心是在x = 12不是Y = 12

第二IF:

~(x(i))^2+(y(i))^2<=25 &&(x(i)-12)^2+(y(i))^2<=100 

此代码可以通过使用逻辑索引来改进。

例如,使用R,你可以做(​​Matlab代码留作锻炼; Tibial):

n = 10000 
x = 27*runif(n)-5 
y = 24*runif(n)-12 
plot(x,y) 

r = (x^2 + y^2)<=25 & ((x-12)^2 + y^2)<=100 
g = (x^2 + y^2)<=25 
b = ((x-12)^2 + y^2)<=100 
points(x[g],y[g],col="green") 
points(x[b],y[b],col="blue") 
points(x[r],y[r],col="red") 

这给:

Colored regions

+0

非常感谢主席先生,这真的很有用:) – Suzy