A gap between two stone gateposts in a stone fence, with green pasture and the sea beyond
Interactive tutorial · Calculus I

Optimization

Of all the pens you could fence, boxes you could fold and cans you could roll, one is best. The derivative finds it. Scroll down: at each stop a short note explains the next idea, then the picture is yours to play with before the next one arrives.

11 stops · about 10 minutes · works on a phone

Assumptions

Everything on this page follows from the assumptions and definitions below. They are granted, not proved, so judge each one for yourself. A step that leans on one is tagged with its number.

  • A1
    The pen. The pen is a rectangle with one side along a straight river bank, which needs no fence. All of the fence, 40 m unless you change it, goes on the other three sides.
    Leaves out: a gate, the posts, a bank that curves, and any pen that is not a rectangle.
  • A2
    The sheet and the box. The sheet is a flat rectangle with no thickness, 12 cm by 12 cm unless you change it. Equal squares are cut from its four corners and the sides fold straight up into a box with no lid.
    Leaves out: the thickness of the cardboard, and tabs or tape to hold the corners together.
  • A3
    The can. The can is a cylinder with a flat top and bottom that holds exactly its stated volume, 500 cm³ unless you change it. The metal it uses is its surface area.
    Leaves out: seams and rims, a top thicker than the side, and the metal wasted when circles are cut from a sheet.
  • A4
    The cup. The cup is the same cylinder with no top: one circle and a side.
    Leaves out: a rim, a handle, and anything else that is not part of a plain cylinder.
  • D1
    A critical number of a function is a number in its domain where the derivative is zero or does not exist.
  • D2
    An absolute maximum of a function on an interval is a value at least as large as every other value it takes there. An absolute minimum is at least as small.
Scroll to begin↓
1 · A pen by the riverStop 1 of 11
Problem
Given
Top
x

The computation

    Right now,

    Stop 1 · A pen by the river

    Forty meters of fence

    You are looking down at a field beside a river. You have 40 meters of fence and want a rectangular pen. The river closes one side, so the fence only has to cover the other three.

    Call the depth of the pen x, the distance it reaches back from the river. Pick a depth and the rest is forced: whatever fence is left over becomes the side along the river.

    A shallow pen is long and thin. A deep pen uses up the fence on its two sides and has almost nothing left for the third. Somewhere in between is the pen that holds the most.

    Rests on

    Try itDrag the dot on the fence, or use the slider. Find the largest area you can. What happens to the area at each extreme?

    Stop 2 · A pen by the river

    One variable

    The area is A = x · y, where y is the side along the river. That has two variables, but they are not free. The fence ties them together:

    2x + y = 40,  so  y = 40 − 2x

    That equation is the constraint. Substitute it and the area depends on the depth alone:

    A(x) = x(40 − 2x),   0 ≤ x ≤ 20

    The interval matters. At x = 0 the pen has no depth, and at x = 20 the two sides use all the fence. Both ends give zero area.

    A graph has appeared on the right. Every pen you can build is one point on that curve.

    Try itDrag the pen and watch the dot ride along the curve. Where on the curve is the largest pen? What is the curve doing there?

    Stop 3 · A pen by the river

    The slope tells you which way to go

    The graph now carries a tangent line, and its slope is the derivative:

    A′(x) = 40 − 4x

    Where the slope is positive, a deeper pen holds more. Where it is negative, you have gone too far. The largest pen sits where the slope is exactly zero:

    40 − 4x = 0,  so  x = 10  and  y = 20

    A number where the derivative is zero is a critical number. This one gives 200 m², more than either end of the interval, so it is the absolute maximum.

    A new slider changes how much fence you have.

    Rests on

    Try itWith 40 m the best pen is twice as long as it is deep. Give yourself 60 m, then 80 m. Is the best pen always that shape?

    Stop 4 · A box from a sheet

    Cut the corners, fold the sides

    Start with a flat square sheet, 12 cm on a side. Cut the same small square out of each corner and fold the four flaps up. You have a box with no lid.

    The side of each cut square is x, and it becomes the height of the box. The top picture is the flat sheet with the cuts in orange. The bottom picture is the box it folds into.

    A small cut makes a wide, shallow tray. A big cut makes a tall box with almost no floor. You want the cut that holds the most.

    Rests on

    Try itDrag the corner of the orange square. Find the cut with the largest volume. Is it closer to a tray or to a tall box?

    Stop 5 · A box from a sheet

    Three factors, one variable

    Each cut takes x off both ends of a side, so the floor of the box is 12 − 2x on a side and the height is x:

    V(x) = x(12 − 2x)²,   0 ≤ x ≤ 6

    Multiply it out, differentiate and factor:

    V′(x) = 12x² − 96x + 144 = 12(x − 2)(x − 6)

    There are two places where the slope is zero. At x = 6 the cuts meet in the middle and nothing is left for a floor. At x = 2 the box holds 128 cm³, and that is the most it can.

    Try itSet the cut to 2 cm and compare the floor with the height. Then look at the graph near x = 6: the slope is zero there too. Why is that not the answer?

    Stop 6 · A box from a sheet

    Check the ends

    A zero slope marks a candidate, not a winner. On a closed interval the method is always the same:

    • Find the critical numbers inside the interval.
    • Work out the quantity there, and at both ends of the interval.
    • The largest of those values is the absolute maximum. The smallest is the absolute minimum.

    A new slider makes the sheet longer, up to 24 cm, while its short side stays 12 cm. The cut still cannot pass 6 cm. The derivative no longer factors nicely, so step 4 on the right uses the quadratic formula, and one of its two answers falls outside the interval.

    Rests on

    Try itStretch the sheet from 12 cm to 24 cm. The sheet doubles in size. How much does the best cut change?

    Stop 7 · A can

    Half a liter, least metal

    A can has to hold 500 cm³. It can be tall and thin or short and wide, and every one of those shapes holds the same amount. They do not use the same amount of metal.

    This time you are after a minimum: the least surface area. You control the radius r. The height adjusts so the volume stays at 500 cm³.

    The interval is different too. The radius can be any positive number, so there are no ends to check. The picture shows radii from 2 cm to 9 cm.

    Rests on

    Try itDrag the side of the can. Find the least metal you can. Then look at the shape of that can: how does its height compare with its width?

    Stop 8 · A can

    Set the slope to zero

    The metal is two circles and a side: S = 2πr² + 2πrh. The constraint is the volume, πr²h = 500, so h = 500/(πr²). Substitute:

    S(r) = 2πr² + 1000/r,   r > 0

    The first term is the two ends, which grow with the radius. The second is the side, which shrinks. Differentiate and set the slope to zero:

    S′(r) = 4πr − 1000/r² = 0,  so  r³ = 250/π  and  r ≈ 4.30 cm

    Put that radius back into the constraint and the height comes out as h = 2r. The best can is exactly as tall as it is wide.

    Try itDrag the radius toward 4.3 cm. The dot snaps to the exact critical number when you get close. Compare the height with the diameter.

    Stop 9 · A can

    Is it really the smallest?

    With no ends to compare against, a zero slope needs another argument. Take one more derivative:

    S″(r) = 4π + 2000/r³

    That is positive for every positive radius, so the graph bends upward everywhere. A curve that bends upward everywhere and has one flat spot has its lowest point there.

    Two new controls: the volume, and whether the can has a top. Without a top it is a cup, with one circle of metal and not two.

    Rests on

    Try itPredict first: will the best cup be taller or flatter than the best can? Switch the top to Open and find out. Then change the volume. Does the best shape change?

    Stop 10 · Looking back

    Flat near the top

    The best value sits where the slope is zero, and a curve with zero slope is nearly level for a while on either side. So a near miss costs almost nothing.

    The pen is back, set to a depth of 9 m where the best is 10 m. That is a 10% miss. The area is 198 m², which is 99% of the best possible.

    For the pen the rule is exact: miss the best depth by some fraction and you lose the square of that fraction of the area. A 10% miss costs 1%. A 20% miss costs 4%.

    Try itMove the pen to 8 m and check the 4%. Then try the box and the can: go 10% off the best value in each and see how little you lose.

    Stop 11 · Looking back

    The recipe, and what the stories leave out

    All three problems went the same way:

    • Name the quantity you want largest or smallest.
    • Write the constraint, and use it to get that quantity down to one variable.
    • Find the interval the variable lives on.
    • Differentiate, and find where the slope is zero.
    • Check that it is the best: compare with the ends, or use the second derivative.

    The answers are only as good as the stories. The pen has no gate and the river bank is straight. The cardboard has no thickness and needs no tabs to hold its corners. The can has no seams, and its top is no thicker than its side. Change a story and the best shape changes with it. The recipe stays the same.

    Everything is unlocked now. Play as long as you like, then scroll on to finish.

    Rests on

    Try itPick a problem and predict before you look: if you double what you are given (the fence, the sheet, the volume), does the best size double?

    Finished

    You completed the optimization tutorial

    You spent – minutes on it.

    Tell Justin you finished

    Add a note if you like: what clicked, what was confusing, or what you would like to see next.

    This emails your name, your message, and the time you spent on the tutorial to Justin. Nothing else is sent.

    The full receipt
    ClaimStatusRests onDetail
    The pen: A(x) = x(L − 2x) is largest at x = L/4, where the side along the river is twice the depth. With 40 m of fence that is 10 m by 20 m, 200 m² (stops 2 and 3).✓ ProvedA1, D1, D2A(x) = x(L - 2x) has A'(x) = L - 4x, zero only at x = L/4, where the river side is 2x and A = L^2/8 (200 m^2 for 40 m); both ends give 0.
    The box: V(x) = x(12 − 2x)² has V′(x) = 12(x − 2)(x − 6). The best cut is 2 cm, for 128 cm³ (stop 5).✓ ProvedA2, D1, D2V(x) = x(12 - 2x)^2 has V'(x) = 12x^2 - 96x + 144 = 12(x - 2)(x - 6); V(2) = 128 and V(0) = V(6) = 0, so 2 cm is the best cut.
    A longer sheet: the derivative’s two roots come from the quadratic formula, and only the smaller one is inside the interval (stop 6).✓ ProvedA2, D1, D2For a 12 by b sheet V'(x) = 12x^2 - 4(12 + b)x + 12b, with roots ((12 + b) -/+ sqrt(144 - 12b + b^2))/6; for b from 12 to 24 the smaller root is inside (0, 6) and the larger is not (best cuts 12: 2.000, 14: 2.148, 16: 2.263, 18: 2.354, 20: 2.427, 22: 2.487, 24: 2.536; doubling the sheet moves the best cut by a factor of 1.27).
    The can: S(r) = 2πr² + 2V/r is smallest where r³ = V/(2π), and there h = 2r. S″(r) is positive for every r > 0 (stops 8 and 9).✓ ProvedA3, D1, D2S(r) = 2 pi r^2 + 2V/r has S'(r) = 4 pi r - 2V/r^2, zero at r = (V/(2 pi))^(1/3), where h = 2r; S''(r) = 4 pi + 4V/r^3 > 0. For 500 cm^3, r = 4.301 cm.
    The cup: with no top the least metal is where h = r, flatter than the can (stop 9).✓ ProvedA4With no top, S(r) = pi r^2 + 2V/r is least at r = (V/pi)^(1/3), where h = r: flatter than the closed can, and the same shape for every volume.
    Missing the best depth of the pen by a fraction costs the square of that fraction of the area: 10% costs 1%, 20% costs 4% (stop 10).✓ ProvedA1A pen whose depth misses L/4 by a fraction e has (1 - e^2) of the largest area: 9 m gives 198 m^2 (99%), 8 m gives 192 m^2 (96%).
    The numbers the tool shows as you drag.✓ Checked independently—The page's own functions, run at 63 settings across the three problems, match SymPy's values, slopes and best sizes to 3.7e-15 (relative); the on-screen difference-quotient check is within 1.5e-08.
    Units: m² for the pen, cm³ for the box, cm² for the can.Reviewed—A length times a length, three lengths multiplied, and an area.
    The sentences on the cards.Reviewed—Each card was read against the formulas, looking for anything assumed but not listed. No objection stands.
    A1, A2, A3, A4, D1, D2Assumed—Granted, not proved. Whether they suit a real pen, box or can is yours to judge.

    Proved: SymPy reduced the difference between the two sides to zero. Checked independently: the page’s own functions were run against SymPy’s exact answers. Checked numerically: evidence, not proof. Reviewed: a model or a person read it. Assumed: an assumption or a definition, granted and not proved. Checks run 2026-11-12 with SymPy 1.14.0; rerun them with interactives/checks/optimization.py. Cards read by Claude Fable 5.2, 2026-11-11. How it’s checked

    Part of Calc Practice: free Calculus 1 practice. Try the optimization problems or see more interactives.