5.4.10 · D2Scientific Computing (Python)

Visual walkthrough — scipy.optimize — minimize, fsolve, curve_fit, linprog

3,554 words16 min readBack to topic

We follow the parent's own Newton's Method box, but slowly, with a picture per step.


Step 0 — What is a "zero", and why do we chase it?

Before any formula, let's agree on the goal in plain words.

The picture below shows one curve crossing the flat line. That crossing point is what all four tools ultimately search for — directly (fsolve) or through the back door (minimize chases the zero of the slope).

Figure — scipy.optimize — minimize, fsolve, curve_fit, linprog

WHY this matters: you can rarely solve with algebra when is messy. So instead of solving, we will sneak up on the crossing point step by step.


Step 1 — The slope: the only local information we have

Standing at some guess (the little just counts our guesses: guess number 0, 1, 2, …), we can measure two things about the curve right where we stand:

  • its height — how far above or below the line we are, and
  • its slope — how steeply the curve is tilting there.

The figure shows the curve (cyan) and, in amber, the straight tangent ramp at .

Figure — scipy.optimize — minimize, fsolve, curve_fit, linprog

WHY a tangent line? Because a straight line is the only thing we can solve instantly. The curve is hard; a ramp is easy. So we replace the curve by its ramp near and solve the easy problem instead.


Step 2 — Replace the curve with its ramp (the Taylor idea)

Here is the one leap of faith, stated with zero jargon: close to where you stand, a smooth curve and its tangent ramp are almost the same line. Zoom in enough and every curve looks straight.

Writing "a small sideways step" as (Greek letter delta, meaning "a little change"), the ramp's height after stepping is:

This is the parent note's "keep linear terms of the Taylor expansion" — but now you can see it: we threw away the curve's bending and kept only its straight part.

Figure — scipy.optimize — minimize, fsolve, curve_fit, linprog

Step 3 — Demand the ramp's height and solve for the step

We want to land on the horizontal line. We cannot force the real curve to in one clean move — but we can force the ramp (our straight stand-in) to , because a straight line meets the axis at exactly one solvable point. So we set the approximate height to zero:

Read the fraction as pure geometry: "how far to walk = (height error) ÷ (tilt)." A tall error over a gentle tilt means a long walk; a small error over a steep tilt means a tiny nudge. That is the ramp sliding down to the axis. Because we zeroed the ramp and not the curve, lands close to the true zero, not exactly on it — which is why we must repeat.

Figure — scipy.optimize — minimize, fsolve, curve_fit, linprog

Step 4 — The Newton update: take the step, repeat

Our new, better guess is the old spot plus the step :

Now the trick: the ramp only approximated the curve, so isn't the exact zero — but it's closer (once we're near enough). So we stand there, draw a fresh tangent, slide again, and again. Each pass squeezes the gap.

Figure — scipy.optimize — minimize, fsolve, curve_fit, linprog

Step 5 — From root-finding to minimizing (the parent's big reveal)

The parent note's headline: minimizing = finding a root of its slope . Watch why, as a picture.

So to minimize , just run Newton's method on . Substitute (so , the "slope of the slope", i.e. curvature):

Figure — scipy.optimize — minimize, fsolve, curve_fit, linprog

Step 6 — Edge case A: the slope is zero (flat spot, division blows up)

Every honest walkthrough must cover the failures. Look at the update: we divide by . What if the tilt is ?

Figure — scipy.optimize — minimize, fsolve, curve_fit, linprog

Step 7 — Edge case B: wrong starting guess → the other root

The method is local: it slides to whatever zero its ramps happen to point at. Different seeds can land on different roots — exactly the parent's fsolve warning ( but ).

Figure — scipy.optimize — minimize, fsolve, curve_fit, linprog

WHY linprog doesn't have this problem: a linear objective has a constant slope that never flattens, so there's no local trap — the optimum is always a corner, found globally. That's the whole reason it's a separate tool (see Linear Programming Simplex and Convex Optimization).


Step 8 — Edge case C: a bad guess makes Newton diverge

The "error squares" magic of Step 4 was promised only when you start close. Far away, the very same tangent-slide can throw you further from the zero — or into an endless loop.

Figure — scipy.optimize — minimize, fsolve, curve_fit, linprog

Step 9 — Edge case D: a multiple root () — convergence slows to a crawl

Steps 6–8 were about the tilt vanishing where we stand. Now the subtler trap: the tilt vanishing at the very zero we're chasing. This happens when the curve doesn't cleanly cross the axis but merely touches it — a double (multiple) root, like at .

Figure — scipy.optimize — minimize, fsolve, curve_fit, linprog

Step 10 — What fsolve actually runs (not plain Newton)

We derived Newton because it is the clearest lens. But it would be dishonest to claim fsolve is Newton-with-a-leash.


The one-picture summary

Everything above, compressed: a curve, one guess, one tangent ramp, one slide to the axis, repeat — and the same picture reused with to find a valley bottom.

Recall Feynman: the whole walkthrough in plain words

You want to find where a wiggly line crosses the floor, but the line's too complicated to solve. So you cheat: stand at a guess, feel how steeply the line tilts (that tilt is the slope — imagine two dots on the curve sliding together until the chord between them becomes a perfect kiss), and pretend the curve is a straight ramp from there. A straight ramp is easy — you can instantly see where it hits the floor, so you jump to that spot. The ramp lied a little because the real line was curved, so you're not there yet, but you're closer — and because the only leftover is that little bending, the miss shrinks like a square each time, so a few jumps and you're basically dead on. That's the engine inside fsolve. To find the bottom of a valley instead, notice the bottom is where the ground is flat — where the slope hits zero — so play the exact same game on the slope, but check the ground actually curves upward there (a bowl, not a dome or a ledge) or you'll happily land on a peak. That's minimize. Four warnings: if you stand on a flat spot your ramp is horizontal and points nowhere (divide-by-zero); if the line crosses twice, where you start decides which crossing you fall into; if you start too far off, the ramp can fling you into the wilderness; and if the line merely kisses the floor instead of slicing through it, the fast squaring dies and progress crawls to a halt. Real tools don't run raw Newton — fsolve uses Powell's hybrid scheme, mixing bold Newton jumps with cautious safe steps and guessing the slopes by tiny finite-difference nudges. linprog skips all of it, because straight walls always send you to a corner.

Recall Quick check

Newton update in one line ::: — slide down the tangent to the axis. The slope is defined as… ::: the limit of the difference quotient as — a chord tightening into the tangent. Why does the same formula minimize? ::: Put ; the valley bottom is where the slope , a root of . What extra check turns a stationary point into a real minimum? ::: The curvature must be positive, (a bowl, not a dome or ledge). What breaks Newton at a flat point? ::: ⇒ horizontal ramp ⇒ divide-by-zero ⇒ step to infinity. Why can two seeds give different roots? ::: Newton is local; its tangents chain to the nearest crossing, and a curve may cross more than once. Why can a far-off guess diverge? ::: The "error squares" guarantee needs you to start close; far away the ramp is a poor stand-in and can fling iterates away or cycle. What slows Newton to linear convergence? ::: A multiple root where (the curve only touches the axis): the error halves instead of squaring. What does fsolve actually implement? ::: Powell's hybrid method (MINPACK hybrd) — Newton mixed with a trust-region safe step, using a finite-difference Jacobian.


Flashcards

Derive the Newton step from a tangent line.
Approximate , set that approximation to , solve: .
Define the slope that Newton needs.
The limit — the difference quotient (rise/run) as the run shrinks to zero.
Geometric meaning of one Newton step.
Slide down the tangent ramp at until it hits the horizontal axis; that landing is .
Why is minimizing a root problem, and what sign check is needed?
The minimum is where slope , so run Newton on : — and require so it's a minimum, not a maximum or saddle.
When does Newton lose its quadratic speed?
At a multiple root — the curve only touches the axis, so error shrinks linearly (halves per step for a double root) instead of squaring.
Does fsolve run plain safeguarded Newton?
No — it wraps MINPACK's Powell hybrid method, blending Newton steps with trust-region safe steps and a finite-difference Jacobian.