How do I find the value where two physical models agree?
How do I solve a system of two equations with two unknowns numerically?
NoteObjectives
Use scipy.optimize.fsolve to find the root of a single equation.
Extend fsolve to a system of two equations and two unknowns.
Recognise when a problem reduces to root-finding.
Why fsolve?
Many physics problems reduce to the question: at what value of x does f(x) = 0? Finding where two models agree — f(x) = g(x) — is equivalent to finding the root of h(x) = f(x) − g(x) = 0. scipy.optimize.fsolve solves this numerically for any differentiable function.
For a system of two equations in two unknowns, fsolve generalises: pass a function that returns a list of two residuals and an initial guess vector, and it returns the vector that makes both residuals zero.
Example problem: collision kinematics
Two pucks move along a frictionless track. Their positions are:
fsolve returns the vector [I₁, I₂] at which both residuals are zero — i.e., the solution to the system. For linear systems numpy.linalg.solve is more efficient, but fsolve works the same way when the equations become nonlinear.
TipKey Points
Rewrite f(x) = g(x) as h(x) = f(x) − g(x) = 0 and pass h to fsolve.
For two unknowns, return a two-element list from the residual function and pass a two-element initial guess.
Always plot the functions to check that your solution is at the right root and that the initial guess is reasonable.