5.4.10 · D3Scientific Computing (Python)

Worked examples — scipy.optimize — minimize, fsolve, curve_fit, linprog

2,775 words13 min readBack to topic

This page is a case-hunting expedition. The parent note showed you the four engines and why they exist. Here we push every one of them into the corners: multiple roots, bad starting guesses, degenerate data, unbounded regions, and an exam-style twist. Nothing on this page uses a symbol we have not already earned in the parent, and where new ideas appear (local vs global, the residual as a picture) we build them from zero.

Before any code: three words you must own.


The scenario matrix

Every worked example below is tagged with a cell from this table. Together they cover the whole grid.

Cell Tool The scenario class it stress-tests
A minimize Smooth convex bowl — one global minimum, any seed works
B minimize Two valleys (non-convex) — seed decides the answer
C fsolve System with two roots — sign of seed picks the root
D fsolve Degenerate: no real root → what the solver returns
E curve_fit Nonlinear model with good p0 → recovers parameters
F curve_fit Limiting case: perfect (noise-free) data → residuals
G linprog Bounded polytope → optimum at a corner
H linprog Unbounded region → solver reports failure, not a number
I word problem Real-world allocation, translate English → matrices
J exam twist Maximize and honour an equality constraint

Cell A — a smooth convex bowl (minimize)


Cell B — two valleys, the seed decides (minimize)


Cell C — a system with two roots (fsolve)


Cell D — a degenerate system with no real root (fsolve)


Cell E — nonlinear fit with a good seed (curve_fit)


Cell F — the limiting case: perfect data (curve_fit)


Cell G — a bounded polytope, optimum at a corner (linprog)


Cell H — an unbounded region (linprog)


Cell I — real-world word problem (linprog)


Cell J — the exam twist: maximize with an equality (linprog)


Recall The one-line rule behind every cell

Convex/one-valley ( any seed, cell A/G) versus non-convex/multi-root ( seed decides, cells B/C) — always ask "could there be more than one answer?" before trusting the first number.