5.4.10Scientific Computing (Python)

scipy.optimize — minimize, fsolve, curve_fit, linprog

1,999 words9 min readdifficulty · medium1 backlinks

WHY these tools exist

The whole family is built on one mathematical observation: at a minimum, the gradient is zero.

This is why minimization and root-finding are secretly the same problem: minimizing ff = finding a root of f\nabla f. Different APIs, same skeleton.


Deriving the engine: Newton's method (the heart of all four)

fsolve uses a multidimensional version (the Jacobian JJ replaces gg'): Δ=J1F\Delta = -J^{-1}\mathbf{F}. minimize methods like 'Newton-CG', 'BFGS' use the Hessian (or an approximation of it).


Least squares — where curve_fit comes from


linprog — the linear special case

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

Worked examples


Common mistakes (steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine you're blindfolded on a hilly field and want the lowest point. You feel which way the ground tilts and take a step downhill, again and again — that's minimize. If instead you want the spot where the ground is exactly flat at sea level, that's fsolve finding a zero. curve_fit is like bending a stretchy string to pass through scattered dots as snugly as possible. linprog is a city with straight streets and a "lowest" corner — you just walk along the walls to the best corner. Same idea every time: keep stepping until you can't improve.


Flashcards

What does scipy.optimize.minimize minimize?
A scalar objective f(x)f(\mathbf{x}), returning the x\mathbf{x} giving the smallest value.
Why are minimization and root-finding the same problem?
Minimizing ff = finding a root of its gradient f=0\nabla f = 0.
Derive the Newton update for g(x)=0g(x)=0.
Linearize g(xk+Δ)g(xk)+g(xk)Δ=0xk+1=xkg(xk)/g(xk)g(x_k+\Delta)\approx g(x_k)+g'(x_k)\Delta=0 \Rightarrow x_{k+1}=x_k - g(x_k)/g'(x_k).
Why does curve_fit square the residuals?
Squares keep errors positive (no cancellation) and give the maximum-likelihood fit under Gaussian noise.
What must you change to MAXIMIZE with linprog?
Negate the cost vector c (and negate res.fun for the true value); linprog only minimizes.
Why does an LP optimum sit at a vertex?
A linear objective has constant gradient, so it improves until it hits a corner of the feasible polytope.
What attribute of an OptimizeResult holds the solution vs the value?
.x is the solution, .fun is the objective value, .success the convergence flag.
Why does fsolve sometimes give the "wrong" root?
It's a local method — it converges to the root nearest x0; change the seed to find others.
What does the diagonal of pcov from curve_fit give you?
Variances of the fitted parameters; sqrt(diag(pcov)) are 1σ uncertainties.
When use fsolve vs minimize for a system of equations?
fsolve to make the system equal zero; minimize only if you genuinely want the smallest value of one scalar.

Connections

  • Newton's Method — the iterative core shared by minimize & fsolve
  • Gradient and Hessian — convergence conditions (f=0\nabla f=0, H0H\succeq 0)
  • Least Squares Regression — theory behind curve_fit
  • Linear Programming Simplex — algorithm inside linprog
  • NumPy polyfit — closed-form linear-fit alternative
  • Convex Optimization — when a local min is the global min

Concept Map

minimizing f = root of grad f

update rule

multidimensional via

uses

uses

minimized by Levenberg-Marquardt

is a special case of

set dS/dtheta = 0

linear version of

Stationary point grad f = 0

Newton step iteration

minimize scalar objective

fsolve roots F x = 0

curve_fit fit data

linprog linear objective

Least-squares objective S theta

Jacobian J

Hessian H

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, scipy.optimize ka funda simple hai: har problem mein hum kisi number space mein search kar rahe hote hain jab tak ek condition satisfy na ho jaye. minimize kisi function ki value ko sabse chhoti karta hai (jaise blindfold pahaad par sabse neeche jaana). fsolve woh point dhoondhta hai jahan equation zero ho jaye — yaani root finding. curve_fit noisy data ke through ek model curve fit karta hai (least squares se), aur linprog linear cost ko linear constraints ke under optimize karta hai.

Sabse pyaari baat: minimize aur fsolve andar se ek hi cheez hain. Minimum par gradient zero hota hai, to "minimize ff" ka matlab hai "f=0f'=0 ka root nikaalo". Dono ke peeche Newton's method hai — current guess par Taylor expansion karke linear part rakho, usse zero set karke step nikaalo: xk+1=xkg/gx_{k+1}=x_k - g/g'. Yahi reason hai ki tumhe x0 (starting guess) dena padta hai aur galat guess se galat ya koi root nahi milta.

Practical tips jo exam aur projects mein bachayenge: linprog hamesha minimize karta hai, to maximize karna ho to c ko negate karo aur answer -res.fun lo. Nonlinear curve_fit mein p0 zaroor do warna garbage aata hai. Result object mein .x solution hai, .fun value, aur .success convergence batata hai. fsolve ka result confirm karne ke liye full_output=True se ier==1 check karo. Bas itna yaad rakho — "My Friend Can't Lie" = Minimize, Fsolve, Curve_fit, Linprog — aur tum set ho.

Go deeper — visual, from zero

Test yourself — Scientific Computing (Python)

Connections