Mathematica Program-calculate the area enclosed by the image corresponding to the Function

Source: Internet
Author: User

[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)

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.