[Problem] Try to find the area of the image enclosed by the following curve.
X ^ 2 + y ^ 2 ≤ 16; x ^ 2/36 + y ^ 2 ≤ 1; (x-2) ^ 2 + (Y + 1) ≤ 9;
FirstAlgorithm:
Area [X1 _, X2 _, Y1 _, Y2 _, step _]: = block [{NX, NY, I, j, XX, YY, S = 0 }, nx = integerpart [(x2-X1)/step]; ny = integerpart [(Y2-Y1)/step]; for [I = 1, I ≤ NX, I ++, for [j = 1, j ≤ NY, J ++, xx = X1 + I * step; YY = Y1 + J * step; if [XX ^ 2 + YY ^ 2 ≤ 16 & XX ^ 2/36 + YY ^ 2 ≤ 1 & (XX-2) ^ 2 + (yy + 1) ^ 2 ≤ 9, S = S + step ^ 2]; return [s]
[Running result]
Area [-2, 6,-2, 2, 0.01] 8.8312 area [-1, 4,-1, 1, 0.001] 8.83922
The idea of the first method is very simple. It refers to dividing the rectangle area and accumulating the area of the small rectangle falling in the target area.
The approximate degree of area is related to the precision step of subdivision. The algorithm is clumsy and has a long computing time. The algorithm complexity is O (1/step ^ 2 ).
Method 2: Enlightenment of Monte Carlo algorithm based on probability
Areamc [X1 _, X2 _, Y1 _, Y2 _, N _]: = block [{RX, Ry, I, S, Calin = 0 }, for [I = 1, I ≤ n, I ++, RX = random [real, {x1, x2}]; ry = random [real, {y1, Y2}]; if [RX ^ 2 + Ry ^ 2 ≤ 16 & RX ^ 2/36 + Ry ^ 2 ≤ 1 & (RX-2) ^ 2 + (RY + 1) ^ 2 ≤ 9, Calin = Calin + 1]; S = (x2-X1) * (Y2-Y1) * n [Calin/n]; return [s]
Areamc [-2, 6,-2, 2, 1000000] 8.85734
This algorithm is an approximate algorithm. The complexity of the algorithm depends on the number of randomly generated points n, and the complexity is O (n)