Our final problem has no known exact solution. We want to find the area of the shape formed by,
The inequality has two parameters and . 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 (move along the diagonal, starting from bottom-left), we get the shape of a nice heart. When we increase while holding fixed, the heart morphs into a circle. On the other hand, when we increase while holding 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 and the ear length . Can you guess the formula for the area ?
1. Problem description
Let . Write down the expression for the area of the heart, the region that satisfies the inequality,
The parameters and 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 :
If we can write as a function of , then we can apply the area formula
But solving the sextic equation for , or even , 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 . In the inequality, the only parameters with dimension of are the radius and the ear length .
Hence, the area of our heart must consist of powers of and , and the powers must add to 2:
I have written the area as a linear combination of power terms , because there is more than one answer to finding powers and that add to 2. We could have “nice” terms like , , and , terms with a negative power like , and terms with a decimal power like . All of these have dimension of , 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 and ? We posit that, not only is the area bounded and continuous in and , 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:
There are three dimensionless coefficients , , and for us to find.
b. Boundary value analysis
Recall the equation of our heart and the expression for the area inside:
A boundary value analysis considers the extremes of the problem. We can imagine two extreme cases.
- What happens when ?
- What happens when ?
When , the heart equation simplifies to the equation of a circle,
and we know the area of a circle! Solve to get .
Next, consider and write the heart equation in polar coordinates:
When , the equation holds trivially and we do not know how is related to any more than we did before.
So consider . We can divide both sides of the equation by and say that,
I have restricted the angle so that the polar curve draws the two ears only once. We can now use the area formula:
Now we know .
In summary, by analyzing the two boundary cases, we were able to determine two unknown coefficients:
Only one coefficient remains unknown, . We use Monte Carlo simulation to estimate its value.
3. Monte Carlo simulation
We know the coefficient once we know the area of our heart:
We estimate the area by randomly generating points inside a box . 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:
If we consider a point inside the heart to be a “win,” we can solve the equation for the area of the heart:
Note that must be large enough to fully contain the heart inside.
By trial and error, I found that we should take to be the maximum of and . 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 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 and , and found . In summary, the area of our heart is given by,
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 and , 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 simulations, we get the following:
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 to be a function of the parameters and :
The number tells us the factor by which we increase the base number of simulations. I have designed 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:
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!
You can find the code in its entirety and in other languages here:
Download from GitHub