Can You Bake the Optimal Cake Using Calculus?

August 27, 2016 · Gradient Vectors, Optimization, Mathematica Demos

Problem Description

FiveThirtyEight's Oliver Roeder challenged readers to maximize the volume of a cake comprised of three cylindrical tiers subject to the constraint that the cake must fit beneath a glass cone of a prescribed size in The Riddler:

A mathematician who has a birthday coming up asks his students to make him a cake. He is very particular (he is a constructive set theorist, which explains a lot) and asks his students to make him a three-layer cake that fits under a hollow glass cone he has on his desk. He requires that the cake fill up the maximum amount of volume under the cone as possible and that the layers of the cake have straight vertical sides. What are the thicknesses of the three layers of the cake in terms of the height of the glass cone? What percentage of the cone's volume does the cake fill?

Our goal is to adjust the height and radius of each of the three tiers in order to maximize the volume of the cake.

Animation of a three-tiered cake fitting inside a cone
Adjusting the tier dimensions to maximize volume inside the cone

Drawing a Diagram

While there may be an elegant solution using only elementary techniques, this problem immediately stood out to me as one that could be solved using multivariable calculus optimization.

Let's start with a cone of height $h$ and radius $r$. Let the height of each tier of the cake be $h_1$, $h_2$, and $h_3$. Also let the remaining space between the third tier and the top of the cone be $h_0 = h - h_1 - h_2 - h_3$. Finally, let the radius of each tier be $r_1$, $r_2$, and $r_3$.

Diagram showing cake tiers inside a cone with labeled dimensions
Diagram of the three cake tiers inside a cone, with labeled dimensions

A Formula for the Volume of the Cake

The volume of the entire cone is just $V_{\text{cone}} = \frac{\pi r^2 h}{3}$. The combined volume of the three-tiered cake is the sum of the volumes of the individual cylindrical tiers:

$$V_{\text{cake}} = \pi r_3^2 h_3 + \pi r_2^2 h_2 + \pi r_1^2 h_1$$

At first, maximizing $V_{\text{cake}}$ might seem very difficult since there are so many variables. Luckily, the diagram reveals a set of four similar right triangles, which leads to the following proportions:

$$\frac{h}{r} = \frac{h_0 + h_1 + h_2}{r_3} = \frac{h_0 + h_1}{r_2} = \frac{h_0}{r_1}$$

These proportions give us:

$$r_1 = \frac{h_0 r}{h}, \quad r_2 = \frac{(h_0 + h_1) r}{h}, \quad r_3 = \frac{(h_0 + h_1 + h_2) r}{h}$$

Plugging these back into $V_{\text{cake}}$ and using the substitution $a = h_1$, $b = h_2$, $c = h_3$, $H = h$, and $R = r$ for clarity, we get the volume as a function of three variables.

Maximize the Volume Using the Gradient

This equation is perfect for finding the gradient $\nabla V = \left\langle \frac{\partial V}{\partial a}, \frac{\partial V}{\partial b}, \frac{\partial V}{\partial c} \right\rangle$. As usual, set $\nabla V = \vec{0}$ and solve. We get four solutions, the first two of which are extraneous if we require three tiers of nonzero volume:

$$(a, b, c) = \left(\frac{H}{4}, \frac{H}{4}, \frac{H}{4}\right) \quad \text{and} \quad (a, b, c) = \left(\frac{H}{6}, \frac{H}{6}, \frac{H}{6}\right)$$

Test Candidates Using the Hessian Determinant

We verify which solution is our desired maximum using the second derivative test for a function of three variables — the Hessian determinant. Testing $\left(\frac{H}{6}, \frac{H}{6}, \frac{H}{6}\right)$ gives a saddle point. On the other hand, $\left(\frac{H}{4}, \frac{H}{4}, \frac{H}{4}\right)$ gives our desired local maximum.

The Solution

With our local maximum at $(a,b,c) = \left(\frac{H}{4}, \frac{H}{4}, \frac{H}{4}\right)$, the three tiers each have height $\frac{h}{4}$, with a gap of $\frac{h}{4}$ at the top. The maximum volume is:

$$V_{\text{max}} = \frac{\pi r^2 h}{3} \cdot \frac{37}{64}$$

And the maximum percentage of the cone filled with cake:

$$\frac{V_{\text{cake}}}{V_{\text{cone}}} = \frac{37}{64} \approx 57.8\%$$

Mathematica Demo

I created a Mathematica demo of my solution. Here's the interactive manipulate:

Mathematica Manipulate demo of the optimal cake solution
Mathematica Manipulate showing the three-tier cake optimization

You can download the full Mathematica notebook:

← Back to Blog