Introduction
Fencing a pen is one of the oldest optimization problems, and it shows the whole method in a form you can check by hand. This walkthrough takes a fixed length of fence and finds the rectangle with the most area. It builds a table, reads the rate of change between rows, and finds where that rate is zero. Then it changes the problem and solves the new version the same way.
The Starting Point
You have 40 meters of fencing and want to enclose a rectangular pen. You can choose any length and width, as long as the fence goes all the way around and you use all 40 meters. Which dimensions give the pen the most area?
Two quick guesses show why instinct isn't enough. A long, thin pen, 19 meters by 1 meter, uses all 40 meters but encloses only 19 square meters. A pen 15 meters by 5 meters encloses 75 square meters. The shape matters a great deal, and somewhere there is a best one.
Walking Through the Optimization
Step 1: Write the area in terms of one side
Name one side x meters. The fence covers two lengths and two widths, so one length plus one width uses half the fence, 20 meters. The other side must therefore be 20 - x meters.
The area of a rectangle is length times width, so the area A, in square meters, is
A = x(20 - x)
This step is the heart of optimization. The problem started with two unknown sides tied together by a constraint, a condition that limits the allowed choices: the fixed 40 meters of fence. Using the constraint to write one side in terms of the other leaves a single quantity to vary, x. Since both sides must be positive, x lies between 0 and 20.
Step 2: Tabulate the area
Compute the area for even values of x from 2 to 18:
| x (m) | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 |
|---|
| 20 - x (m) | 18 | 16 | 14 | 12 | 10 | 8 | 6 | 4 | 2 |
| Area (m²) | 36 | 64 | 84 | 96 | 100 | 96 | 84 | 64 | 36 |
The areas rise to 100 square meters at x = 10 and fall again. The table is symmetric, which makes sense. A pen 4 meters by 16 is the same pen as one 16 meters by 4, turned sideways.
Step 3: Compute the rate of change between neighboring rows
Now find how fast the area changes as x grows. Between neighboring columns, x increases by 2 meters. Divide the change in area by 2 to get the average rate of change, in square meters of area per meter of side:
| x from → to | 2 → 4 | 4 → 6 | 6 → 8 | 8 → 10 | 10 → 12 | 12 → 14 | 14 → 16 | 16 → 18 |
|---|
| Change in area (m²) | +28 | +20 | +12 | +4 | −4 | −12 | −20 | −28 |
| Rate (m² per m) | 14 | 10 | 6 | 2 | −2 | −6 | −10 | −14 |
The rate shrinks steadily: 14, 10, 6, 2. Between 8 and 10 it's small and positive. Between 10 and 12 it's small and negative. So somewhere near x = 10, the rate passes through zero.
That pattern is the signature of a peak. While the rate is positive, making x bigger adds area, so you should keep going. Once the rate turns negative, making x bigger loses area. The best value sits where the rate is zero.
Step 4: Conclude
The rate of change of area is zero at x = 10. The other side is 20 - 10 = 10 meters. The best pen is a 10 m by 10 m square, with an area of 100 square meters.
Check the ends of the allowed range too, because a best value can hide there. As x approaches 0 or 20, one side shrinks toward nothing and the area shrinks toward 0. The ends are the worst choices here, not the best. The maximum, the largest value the area reaches over the allowed range, is the 100 square meters at the square.
Step 5: Change the problem
Now suppose the pen runs along a straight river, which serves as one side. You need fence on only three sides, and you have 60 meters of it.
Let x be the length of each of the two sides that run away from the river. Those two sides use 2x meters, so the side parallel to the river gets the rest: 60 - 2x meters. The area is
A = x(60 - 2x)
and x lies between 0 and 30. Tabulate it:
| x (m) | 5 | 10 | 15 | 20 | 25 |
|---|
| 60 - 2x (m) | 50 | 40 | 30 | 20 | 10 |
| Area (m²) | 250 | 400 | 450 | 400 | 250 |
The areas peak at x = 15. A finer table, in steps of 1 meter, confirms it: 448 square meters at x = 14, 450 at x = 15, and 448 at x = 16. The rate of change goes from +2 to −2 across x = 15, so it's zero there. The best pen is 15 meters out from the river and 30 meters along it, with an area of 450 square meters.
The answer isn't a square this time. With the river supplying one side for free, it pays to make the side along the river twice as long as each of the others.
Key Considerations
The table finds the peak only approximately. A table checks the values you chose to compute. It shows the peak lies between 8 and 12, and the symmetry makes 10 a strong guess. But a table alone can't rule out a slightly higher value between rows. Calculus pins the peak exactly. For the area x(20 - x), the rate of change works out to 20 - 2x square meters per meter of side. This is a stated result; deriving it takes rules not covered here. It's zero exactly when x = 10. You can check it against the table: at x = 3 it gives 14, matching the rate between 2 and 4, and at x = 11 it gives −2, matching the rate between 10 and 12.
The common mistake: making the longest possible side. It's tempting to think a bigger side means a bigger pen. But with a fixed amount of fence, lengthening one side shortens another. A 19 m by 1 m pen has a longer side than the square, yet holds less than a fifth of the area. The best value is in the middle of the range, where the gain from one side's growth exactly balances the loss from the other's shrinking.
Check that the answer makes physical sense. The river answer, 15 by 30, uses 15 + 15 + 30 = 60 meters of fence. Both sides are positive. The area, 450 square meters, is larger than the 100 square meters from 40 meters of fence all around, which fits: more fence, and one side for free.
The same method appears in textbooks with other numbers. A standard example uses 100 feet of fencing on three sides of a garden against a wall and finds the best garden is 25 feet by 50 feet (Strang and Herman 2016a, sec. 4.7). The 1-to-2 shape is the same as in the river problem.
Summary
Writing the area in terms of one side, tabulating it, and reading the rate of change between rows locates the best pen where that rate crosses zero. Here is the full result:
Pen with fence on four sides, 40 m of fence: area x(20 - x)
| x (m) | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 |
|---|
| Area (m²) | 36 | 64 | 84 | 96 | 100 | 96 | 84 | 64 | 36 |
| Rate to next row (m² per m) | 14 | 10 | 6 | 2 | −2 | −6 | −10 | −14 | — |
Best: 10 m by 10 m, area 100 m².
Pen along a river, fence on three sides, 60 m of fence: area x(60 - 2x)
Best: 15 m out from the river by 30 m along it, area 450 m².
- The rate of change of area shrinks to zero at the best value and turns negative after it.
- The ends of the range give areas near zero, so the best value is inside the range.
- With a free side along the river, the best shape is no longer a square.
References
- Strang, Gilbert, and Edwin "Jed" Herman. 2016a. Calculus Volume 1. Houston, TX: OpenStax. CC BY-NC-SA 4.0.