You have 20 meters of fencing and you want to build the biggest possible rectangular pen. How wide should you make it?
You could guess. Try a few widths, measure the area, guess again. But there's a faster way, one that doesn't need any guessing at all.
That's optimization. Finding the input that makes some quantity as large, or as small, as possible. Not by trial and error. By math.
What does "best" actually mean here?
Every optimization problem has the same shape: some quantity you're trying to make as big or as small as possible, and some input you're allowed to adjust. Maximize area. Minimize cost. Maximize speed. Minimize error.
We call the quantity you're optimizing the objective. The thing you're allowed to adjust is the input. For the fence, the objective is area, and the input is how you split your 20 meters between width and height.
The wild part is this: for a huge range of real problems, from rocket trajectories to neural networks, the exact same three-step idea finds the answer. Let's build that idea from scratch.
The key idea: slope tells you which way is uphill
Picture a curve, like a hill. At any point on that hill, you're either walking uphill, walking downhill, or standing on flat ground.
Here's the thing: at the very top of the hill, and nowhere else, the ground beneath your feet is flat. Walk a step in any direction from the peak and you go down. Stand exactly on the peak and, for one instant, there's no uphill or downhill left, just flat.
That "steepness under your feet" has a name: the slope. In calculus, we measure it with the derivative. And the observation above becomes a rule:
At a maximum (or minimum), the slope is exactly zero.
Drag the point along the curve and watch the tangent line. Notice what the slope does right at the top.
Drag the point along the curve above. Watch the slope shrink toward zero as you approach the peak, then flip sign the moment you pass it. That flip is the whole trick. We don't need to check every point on the curve, we just need to find where the slope crosses zero.
We write the slope of a function as , its derivative. And the rule becomes notation:
That's it. That single equation is the engine behind almost every optimization problem you'll ever see.
Solving the fence problem
Now we can actually answer the question we opened with. Say the width is . Since the total fencing is 20 m, the height must be . The area is:
We want the that makes largest. By our rule, that happens where :
Width 10, height 10. A square. No guessing required, just one derivative and one equation.
You have 20 m of fencing for a rectangular pen. Drag the width and watch the area peak at exactly one point.
Drag the width slider and watch the area curve on the right. It peaks at exactly , right where the dashed line is, and falls off symmetrically on both sides. More width isn't always better, because more width steals from height. The optimum is the one point where that trade-off balances perfectly.
This same "maximize area for fixed perimeter" logic shows up everywhere: packaging design, land use, even how cells minimize surface area for a given volume.
But what about functions with no easy formula?
Setting and solving works great when the algebra is simple. But real objectives are rarely this clean. A neural network's error function might depend on millions of inputs, with no formula you could ever solve by hand.
So instead of solving for the flat point directly, we do something sneakier: we use the slope as a compass, and take small steps.
If the slope is positive, you're heading uphill, so step left to go down. If the slope is negative, step right. Repeat, and you crawl toward the bottom of the valley, one step at a time. This is called gradient descent, and it's the single most important algorithm in modern machine learning.
Here (eta) is the learning rate: how big a step you take at each move.
A ball rolls downhill using nothing but the local slope. Push the learning rate too high and it overshoots the valley.
Press play and watch the ball roll downhill using nothing but the slope at its current position. Now push the learning rate up. Notice what happens: instead of settling into the valley, the ball starts overshooting, flying past the bottom and back up the other side. Push it further and it diverges completely, bouncing further away with every step.
Too small a learning rate crawls forever. Too large a learning rate overshoots and blows up. Every machine learning engineer spends real time tuning exactly this one number, because it decides whether training converges or explodes.
What if there's more than one valley?
Here's where it gets interesting. Gradient descent only knows about the slope right where it's standing. It has no idea what the rest of the landscape looks like.
So what happens if the landscape has more than one valley?
Click anywhere on the landscape to drop a ball. On the bumpy terrain, where it starts decides which valley it ends up in.
Click anywhere on the bumpy landscape to drop a ball. Try a few different starting points. Notice that the ball always rolls downhill, but it doesn't always end up in the same place, it settles into whichever valley is closest to where it started.
That shallow valley is called a local minimum: the lowest point nearby, but not the lowest point overall. The deep one is the global minimum: the true best answer. Gradient descent can't tell the difference. It just stops the moment the ground goes flat, wherever that happens to be.
Now switch to the convex bowl. Every starting point rolls to the same place, because there's only one valley to fall into. Convex problems always have one clean answer. Bumpy ones depend on where you start, which is exactly why training a large neural network involves randomness, multiple attempts, and a fair bit of luck.
Where this shows up in the real world
Once you see "find where the slope is zero, or crawl downhill until it is," you start noticing it everywhere:
- Machine learning. Every model you've ever used, image generators, chatbots, recommendation systems, was trained by gradient descent, minimizing an error function over millions of parameters.
- Engineering. Airplane wings, bridge trusses, and rocket trajectories are shaped by minimizing weight or fuel while satisfying safety constraints.
- Logistics. Delivery routes, flight schedules, and warehouse layouts are optimization problems minimizing time or cost across huge numbers of choices.
- Economics and finance. Portfolios are optimized to maximize expected return for a given level of risk, the exact same shape of problem as the fence.
- Biology. Evolution itself behaves like a slow, blind optimizer, nudging organisms toward traits that locally maximize survival, sometimes getting stuck in local optima along the way (think of the panda's thumb).
Different fields, wildly different objectives. Same underlying math.
The short version
Optimization means finding the input that makes some quantity as large or small as possible. The core trick is that at a maximum or minimum, the slope is exactly zero, so instead of checking every possibility, we solve one equation: .
When the objective is too complicated to solve directly, we use the slope as a compass and take small steps downhill, that's gradient descent, the algorithm training every modern machine learning model. And because gradient descent only sees the ground right under its feet, it can get stuck in a local minimum instead of finding the true global one, unless the landscape is convex.
From a fence in a field to a neural network with a billion parameters, it's the same three ideas: define what you're optimizing, find where the slope goes flat, and watch out for valleys that aren't the deepest one.
All visualizations are interactive React components running entirely in your browser. Gradient descent and the local-minimum landscape use numeric derivatives computed live as you interact. No libraries beyond React.