Companion Website

Computational Physics

From Equations to Simulation

Chapter 11

Solving Nonlinear Equations

The companion programs make bisection, false position, Newton’s method, the secant method, and interval scanning visible step by step. The resources below connect these programs with tested numerical libraries, give independently checked roots for validation, and summarize how the mathematical and physical setting guides method choice. The examples concern real roots of equations in one variable.

Software and library resources

For larger calculations, a tested library routine is usually a good choice after the root has been isolated. The routines below can use algorithms different from the matching companion code. Several bracketing routines combine bisection with faster interpolation steps while retaining a sign-change bracket; TOMS 748 is a distinct algorithm from Brent’s method. Set tolerances according to the documentation, check the reported status, and evaluate the physical equation at the returned value.

On a narrow screen, scroll horizontally to view all language columns.

Numerical task MATLAB Python C++ Fortran
One real root in a known bracket Provide an interval on which a continuous function changes sign. fzero(fun,[a,b]) root_scalar with bracket=(a,b) and method='brentq' as a practical default; bisect and toms748 are alternatives Boost.Math bisect or toms748_solve; GSL gsl_root_fsolver_falsepos also matches the false-position example Netlib FMM zeroin; NAG c05ayf
An initial guess or two starting values Useful when a bracket is unavailable but the expected root is understood. fzero(fun,x0) searches for a sign change; for an explicit Newton iteration, use the companion code root_scalar with method='newton' or method='secant' GSL root solvers; Boost.Math newton_raphson_iterate uses an analytic derivative NAG C05 scalar root-finding routines
Several distinct real roots in a finite interval Scan continuous pieces, refine each sign-change bracket, and repeat with a finer scan. Use the companion scan or call fzero on each bracket; Chebfun roots is an external option for functions it can approximate well Scan, then call root_scalar with method='brentq' on each bracket Scan, then call Boost.Math toms748_solve on each bracket Scan, then call zeroin or c05ayf on each bracket

These scan-and-refine workflows do not certify that every root has been found. Two crossings within one scan cell can cancel the sign change, and a tangent root can show none. Split at poles, vary the scan density, and inspect the physical model. Chebfun uses a different approximation-based approach; check its results against the original function. MATLAB fzero requires a sign change and will not locate a root where a function merely touches zero.

Benchmark equations and reference roots

These values were checked independently of the companion programs. The intervals identify which root is intended. Use the residual f(root) together with the reported convergence condition; a small residual alone does not give a reliable error in x when the function is nearly flat.

Equation and interval Reference behavior Why it is useful
f(x) = sqrt(x) − cos(x),  [0,1] One root: x ≈ 0.641714370873 The bisection and false-position companion examples. The domain requires x ≥ 0.
f(x) = x + cos(x),  [−1,0] One root: x ≈ −0.739085133215 The Newton and secant companion examples. Compare the result with a bracketing solver.
f(x) = x3 − 2x − 8,  [2,3] One root: x ≈ 2.330746086125 A smooth polynomial check for the root and its residual.
f(x) = 5(1 − e−x) − x,  [4,6] Physical nonzero root: x ≈ 4.965114231744 Wien’s displacement example. Independently check the physical root with x = 5 + W0(−5e−5), where W0 is the principal Lambert W branch. The equation also has x = 0.
f(x) = ex − x sin(πx/2),  [−10.5,2] Six sign-changing roots: −9.999997110, −8.000026694, −5.999736915, −4.002904471, −1.953775386, −0.633156192 The interval-scanning companion example. With n = 1000 subintervals and bracket-width tolerance 10−7, compare the number of roots as well as their positions.
f(x) = (x − 1.021)(x − 1.027),  [0,2] Two simple roots at x = 1.021 and x = 1.027. With a uniform sign-change scan, n = 20 misses both roots in one scan cell; n = 1000 detects two brackets. Check scan density when counting roots.
f(x) = (x − 1)2,  [0,2] One double root at x = 1; the endpoint signs do not differ. A sign-change scan may miss an even-multiplicity root unless it lands exactly on a grid point.

A sign change can also occur across a discontinuity without a root: f(x) = 1/(x − 1) has opposite signs at 0 and 2, but is undefined at 1. Inspect the function before interpreting a bracket or a small reported width as a physical solution.

Choosing a root-finding method

Problem Good method to try Notes
A continuous function changes sign on a known interval Bisection The bracket shrinks predictably. Its width bounds the location error for a root inside the bracket.
A bracket is known and endpoint function values offer a useful linear estimate False position The interval remains bracketed, but one endpoint can remain fixed and convergence can be slow.
A good initial guess and an analytic derivative are available Newton’s method Often fast near a simple root; a small derivative or a poor starting guess can cause failure or select another root.
Two starting values are available but the derivative is inconvenient Secant method No derivative is needed. Monitor small secant slopes and steps that leave the physically allowed region.
Several distinct solutions may lie in a finite interval Scan, then refine each sign-change bracket Increase the scan density and inspect a plot. Sign changes alone can miss even-multiplicity roots and two crossings within one scan cell.
The equation has poles or a restricted physical domain Divide the domain into continuous pieces before bracketing In the finite square well, tangent and cotangent poles must not be mistaken for bound states. Check each energy against the allowed range.

State whether a tolerance controls the width of an interval, the change between iterates, or the residual. These quantities are not interchangeable. If f carries physical units, the numerical size of its residual also depends on how the equation is scaled. Check convergence after varying the tolerance, initial interval or guesses, and scan density where appropriate.

Selected references and documentation

← Back to Chapter 11