← Back to Home

Monte Carlo Simulations: How Big Is Your Heart?

Our final problem has no known exact solution. We want to find the area of the shape formed by,

(x2+y2r2)3ax2y30(x^{2} + y^{2} - r^{2})^{3} - a\,x^{2}y^{3} \leq 0

The inequality has two parameters rr and aa. They are quantities of length, so they take on a nonnegative value. Let's try out some values and see what these parameters do. I have colored the resulting shapes in red. (Note, the scales are different.)

When r=ar = a (move along the diagonal, starting from bottom-left), we get the shape of a nice heart. When we increase rr while holding aa fixed, the heart morphs into a circle. On the other hand, when we increase aa while holding rr fixed, the heart turns into two petals. Well, I see bunny ears.

Clearly, the shape (i.e. area) of our heart depends on the radius rr and the ear length aa. Can you guess the formula for the area A=A(r,a)A = A(r, a)?

1. Problem description

Let r,a0r,\,a \geq 0. Write down the expression for the area of the heart, the region that satisfies the inequality,

(x2+y2r2)3ax2y30(x^2 + y^2 - r^2)^3 - a\,x^2y^3 \leq 0

The parameters rr and aa represent the radius and ear length of the heart.

I find this problem very interesting because it looks elegant and simple, yet we cannot (at least, I can't) find the area analytically.

Try writing the equation (the boundary of the heart) in polar coordinates (R,θ)(R,\,\theta):

(R2r2)3aR5cos2(θ)sin3(θ)=0(R^2 - r^2)^3 - a\,R^5\cos^{2}(\theta)\sin^{3}(\theta) = 0

If we can write RR as a function of θ\theta, then we can apply the area formula

A(r,a)=θ1θ212R(θ)2dθ\displaystyle A(r,\,a) = \int_{\theta_{1}}^{\theta_{2}}\,\frac{1}{2}\,R(\theta)^{2}\,\,d\theta

But solving the sextic equation for RR, or even R2R^{2}, looks impossible.

2. Shape of my heart (cf. Sting)

Although we cannot find the area analytically, we can still say a few things about what the expression must look like. We do so using dimensional analysis and boundary value analysis.

a. Dimensional analysis

We know that an area has the dimension of (length)2\text{(length)}^{2}. In the inequality, the only parameters with dimension of (length)\text{(length)} are the radius rr and the ear length aa.

Hence, the area of our heart must consist of powers of rr and aa, and the powers must add to 2:

{A(r,a)=C1rm1an1+C2rm2an2+C3rm3an3+Ci’s are dimensionless constantsmi+ni=2,for alli\left\{\begin{array}{l} A(r,\,a) = C_{1}\,r^{m_{1}}a^{n_{1}} + C_{2}\,r^{m_{2}}a^{n_{2}} + C_{3}\,r^{m_{3}}a^{n_{3}} + \cdots \\ C_{i}\text{'s are dimensionless constants} \\ m_{i} + n_{i} = 2,\,\,\text{for all}\,\,i \end{array}\right.

I have written the area as a linear combination of power terms rmianir^{m_{i}}a^{n_{i}}, because there is more than one answer to finding powers mm and nn that add to 2. We could have "nice" terms like r2a0r^{2}a^{0}, r1a1r^{1}a^{1}, and r0a2r^{0}a^{2}, terms with a negative power like r1a3r^{-1}a^{3}, and terms with a decimal power like r1.4a0.6r^{1.4}a^{0.6}. All of these have dimension of (length)2\text{(length)}^{2}, so we can linearly add them to obtain the most general expression for the area.

(As an aside, decimal powers involve real numbers, of which there are uncountably many, so it is incorrect to write integer indices as I did out of convenience.)

The expression contains infinitely many terms, so it seems that we are still doomed. We now argue that only 3 of them are, in fact, possible.

First, terms with a negative power are not allowed. Soon we will show that the area of our heart makes sense even when the radius and ear length are 0. If the expression included terms with a negative power, the area would approach to infinity as the radius or ear length tends to 0. We did not see such outrageous behavior in our diagram at the beginning of this post.

Second, terms with a decimal power are not allowed. Look back at our diagram. Doesn't the shape of the heart transition smoothly as we vary the values of rr and aa? We posit that, not only is the area bounded and continuous in rr and aa, but also its first partial derivatives. Terms with a fractional power result in an unbounded derivative as the radius or ear length tends to 0. We also can't have a decimal power greater than 2, since we already eliminated terms with a negative power.

In conclusion, the area consists of only three terms:

A(r,a)=C1r2+C2ra+C3a2\boxed{A(r,\,a) = C_{1}\,r^{2} + C_{2}\,ra + C_{3}\,a^{2}}

There are three dimensionless coefficients C1C_{1}, C2C_{2}, and C3C_{3} for us to find.

b. Boundary value analysis

Recall the equation of our heart and the expression for the area inside:

{(x2+y2r2)3ax2y3=0A(r,a)=C1r2+C2ra+C3a2\left\{\begin{array}{l} (x^{2} + y^{2} - r^{2})^{3} - a\,x^{2}y^{3} = 0 \\ A(r,\,a) = C_{1}\,r^{2} + C_{2}\,ra + C_{3}\,a^{2} \end{array}\right.

A boundary value analysis considers the extremes of the problem. We can imagine two extreme cases.

  1. What happens when a=0a = 0?
  2. What happens when r=0r = 0?

When a=0a = 0, the heart equation simplifies to the equation of a circle,

x2+y2=r2x^{2} + y^{2} = r^{2}

and we know the area of a circle! Solve A(r,0)=πr2A(r,\,0) = \pi r^{2} to get C1=πC_{1} =\pi.

Next, consider r=0r = 0 and write the heart equation in polar coordinates:

R5[Racos2(θ)sin3(θ)]=0R^{5} \cdot \Bigl[R - a\,\cos^{2}(\theta)\sin^{3}(\theta)\Bigr] = 0

When R=0R = 0, the equation holds trivially and we do not know how RR is related to θ\theta any more than we did before.

So consider R>0R > 0. We can divide both sides of the equation by R5R^{5} and say that,

R=acos2(θ)sin3(θ),forθ[0,π]R = a\,\cos^{2}(\theta)\sin^{3}(\theta),\,\,\text{for}\,\,\theta \in [0,\,\pi]

I have restricted the angle θ\theta so that the polar curve draws the two ears only once. We can now use the area formula:

A(0,a)=0π12R(θ)2dθ=12a20πcos4(θ)sin6(θ)dθ=3π512a2\displaystyle \begin{aligned} A(0,\,a) &= \int_{0}^{\pi}\,\frac{1}{2}\,\,R(\theta)^{2}\,\,d\theta \\ &= \frac{1}{2}\,a^{2}\,\int_{0}^{\pi}\,\,\cos^{4}(\theta)\sin^{6}(\theta)\,\,d\theta \\ &= \frac{3\pi}{512}\,a^{2} \end{aligned}

Now we know C3C_{3}.

In summary, by analyzing the two boundary cases, we were able to determine two unknown coefficients:

A(r,a)=πr2+C2ra+3π512a2\boxed{\displaystyle A(r,\,a) = \pi r^{2} + C_{2}\,ra + \frac{3\pi}{512}\,a^{2}}

Only one coefficient remains unknown, C2C_{2}. We use Monte Carlo simulation to estimate its value.

3. Monte Carlo simulation

We know the coefficient C2C_{2} once we know the area of our heart:

C2=A(r,a)πr23π512a2ra\displaystyle C_{2} = \frac{A(r,\,a) - \pi r^{2} - \frac{3\pi}{512}\,a^{2}}{ra}

We estimate the area by randomly generating points inside a box [L,L]×[L,L][-L,\,L] \times [-L,\,L]. The probability that a random point lies inside the heart (i.e. it satisfies the inequality) depends on how large the heart is relative to the box:

p=area of the heartarea of the box=A(r,a)(2L)2\displaystyle p = \frac{\text{area of the heart}}{\text{area of the box}} = \frac{A(r,\,a)}{(2L)^{2}}

If we consider a point inside the heart to be a "win," we can solve the equation for the area of the heart:

A(r,a)(2L)2number of winsN\displaystyle A(r,\,a) \approx (2L)^{2} \cdot \frac{\text{number of wins}}{N}

Note that LL must be large enough to fully contain the heart inside.

By trial and error, I found that we should take LL to be the maximum of 1.5r1.5r and 0.25a0.25a. Our 3-term model says that the shape of the heart changes linearly with respect to either the radius or ear length. The fact that LL also changes linearly is further proof that our model is valid.

The code below shows how to calculate the area of our heart. Once again, we use an affine transformation to generate points inside the box, and vectorization to compute the area fast.

function A = calculate_area(r, a, N)
  % Set the size of the box
  L = max(1.5*r, 0.25*a);
 
  % Generate N points in the box
  x = (2*L)*rand(1, N) - L;
  y = (2*L)*rand(1, N) - L;
 
  % Check if a point is inside the heart
  criterion = ((x.^2 + y.^2 - r^2).^3 - a * x.^2 .* y.^3 <= 0);
 
  % Count how many points are inside the heart
  numWins = sum(criterion);
 
  % Return the area of the heart
  A = (2*L)^2 * (numWins / N);
end

I used r=a=1r = a = 1 and N=107N = 10^{7}, and found C20.501C_{2} \approx 0.501. In summary, the area of our heart is given by,

A(r,a)πr2+0.501ra+3π512a2\boxed{\displaystyle A(r,\,a) \approx \pi r^{2} + 0.501\,ra + \frac{3\pi}{512}\,a^{2}}

4. How good is our model?

Let's see how well our model predicts the area. We perform Monte Carlo simulation for various values of rr and aa, and take the resulting areas to be the truth. They are as close as we can get to the exact values. We compare the areas predicted by our model to those produced by Monte Carlo simulation.

With N=104N = 10^{4} simulations, we get the following:

Area predicted by our model
Area produced by Monte Carlo simulation
Absolute error between the two areas

We see that the area predicted by our model coincides with that from Monte Carlo simulation well, both in terms of the graph and the absolute error.

The absolute error, which lingers in the hundreds (sometimes over 1,000), seems large. However, relative to the area, it is not. When the radius is 100, Monte Carlo says that the area is around 35,000. Compared to this, an absolute error of 1,000 is quite small (2.86% relative error).

We also notice that the results from Monte Carlo simulation are less consistent when the radius is large. We can counteract the size effect by running more simulations for large radii and (to be safe) large ear lengths. That is, we allow NN to be a function of the parameters rr and aa:

{α=ln(1+ra)N(r,a)=floor(104(1+α))\left\{\begin{array}{l} \alpha = \ln(1 + ra) \\ N(r,\,a) = \text{floor}\bigl(10^{4} \cdot (1 + \alpha)\bigr) \end{array}\right.

The number α\alpha tells us the factor by which we increase the base number of simulations. I have designed α\alpha so that we do not need additional simulations when the radius or ear length is 0, and gradually increase the number of additional simulations as we increase the radius or ear length.

With this simple change, we get better looking results:

Area produced by Monte Carlo simulation (variable N)
Absolute error between the two areas (variable N)

5. Conclusion

Over the last four posts, we studied what Monte Carlo simulation is and how we can use it to solve various problems. It's an extremely versatile tool, in that, we can adapt it to many different fields (we considered probability theory and integration, in particular) and can even use it to solve problems for which there is no exact solution.

Along the way, we also considered several Matlab's built-in functions and explored a few programming concepts, including control statements, user-defined functions, and polymorphism.

Our journey in understanding math has only just started. Until next time!

Notes

You can find the code in its entirety and in other languages here: Download from GitHub