A05: Numerical error, conditioning, roots, and approximation
On this page
- A. Prerequisites and observable goals
- B. The central question: what did the computer actually approximate?
- C. Conditioning belongs to the problem; stability belongs to the algorithm
- D. Algebra can repair a numerically poor expression
- E. Root finding with a guarantee you can state
- F. Differentiation, integration, and interpolation are different approximations
- G. Independent practice
- H. Complete solutions: A05
- I. Retention and readiness
A. Prerequisites and observable goals#
Calculus I derivatives and integrals; A01 linear systems; C01–C02 programming.
Separate problem sensitivity from algorithmic error, bracket a root safely, compare differentiation methods, and verify a numerical answer with residuals, scale, and known solutions.
Level: Degree core, with numerical-analysis extensions clearly separated.
B. The central question: what did the computer actually approximate?#
A mathematical real number and a floating-point value are different objects. A finite floating-point format represents a finite set of values with nonuniform spacing. Arithmetic usually rounds to a representable result. In common IEEE binary64 round-to-nearest arithmetic, the unit roundoff is for the usual relative-error model away from underflow and overflow. The model with has hypotheses; it is not a rule for division by zero, nonfinite results, or every exceptional value. C09 develops the representation itself.
Numerical error includes rounding and algorithmic approximation. Modeling error is different: an exact solution to a frictionless equation may still predict a real system badly. Measurement uncertainty is another source. Reporting fifteen decimal places does not distinguish these causes or establish accuracy.
C. Conditioning belongs to the problem; stability belongs to the algorithm#
For a differentiable scalar function with nonzero relevant quantities, a small relative input perturbation approximately produces a relative output perturbation amplified by
This local relative condition number comes from , divided by and compared with . At or , choose an appropriate absolute or differently scaled measure instead of dividing by zero.
For near , . Small relative input uncertainty can be greatly amplified even by an excellent algorithm. A backward-stable algorithm produces the exact answer to a nearby problem, according to an appropriate perturbation measure. If the original problem is ill-conditioned, a nearby problem can have a very different solution. Therefore stability does not guarantee small forward error without suitable conditioning.
For linear systems, a small residual is useful evidence but does not by itself guarantee is close to the true . An almost singular can turn a small residual into a large solution error. A scaled residual such as describes a different property from forward error. Be explicit about which one you measured.
D. Algebra can repair a numerically poor expression#
Consider for large positive . The two square roots are close; each may already be rounded, and subtraction can remove their common leading digits while retaining rounding error. Rationalize the expression:
The identity is exact on the stated real domain, but the two floating-point evaluation paths can behave very differently. Near , the direct subtraction can round to zero, while the rationalized expression yields about . The issue is not that subtraction is always forbidden: subtracting exact nearby values can be well behaved. It is that errors already present in the operands may dominate the tiny desired difference.
Gaussian elimination has a related concern. A tiny pivot can create large multipliers and cancellation. Partial pivoting chooses a largest-magnitude available entry in the column, reducing those multipliers. It is a standard practical safeguard, not a universal proof that every problem is well-conditioned or every possible matrix has a small growth factor. The exact rational solver in A01 deliberately teaches a different issue: nonzero pivots and reversible algebra, not floating-point robustness.
E. Root finding with a guarantee you can state#
If is continuous on and endpoint signs differ, the Intermediate Value Theorem guarantees at least one root inside. Bisection evaluates the midpoint and retains a sign-changing half interval. After halvings, the interval width is , so its midpoint is within of some root in the retained interval. This is an absolute location bound, not a proof of uniqueness. Continuity matters: a sign change across a pole does not establish a zero.
Newton’s method uses the local tangent approximation,
Near a suitable simple root, it can converge rapidly. It can also divide by a small derivative, leave the domain, cycle, or converge to an unwanted root. For , starting at zero gives and then , a cycle. A safeguarded method combines a bracket with faster steps only when they satisfy safety conditions. The lab implements bisection because its invariant and failure modes are especially clear.
A stopping rule based only on can mislead when the function’s scale or derivative is unusual. A location bound, a scaled residual, an iteration budget, and a stagnation check answer complementary questions. The lab rejects nonfinite values and reports inability to reach its tolerance rather than silently returning an arbitrary last iterate.
F. Differentiation, integration, and interpolation are different approximations#
Forward differences approximate by , with truncation error typically under sufficient smoothness. Central differences use and typically have truncation error. Rounding effects can grow like after cancellation and division, so making smaller indefinitely is not an improvement. A rough central-difference balance suggests a step scale proportional to times relevant problem scales, not a universal hard-coded step for all functions.
Symbolic differentiation manipulates expressions according to mathematical rules. Automatic differentiation propagates local derivatives through the executed computational graph using the chain rule, avoiding a finite-difference step. It still uses finite precision and differentiates the chosen program, including its branch path and primitive definitions; discontinuities and unsupported operations require attention. Finite differences approximate derivatives through nearby function evaluations and are valuable as independent gradient checks.
Composite Simpson integration combines parabolic local approximations and requires an even positive number of subintervals. For sufficiently smooth on a finite interval, its classical error bound uses a bound on and is . A smoothness-based bound is not justified across a singularity. Module 23 derives the rule and bounds; the lab compares it with exact cubic integrals.
Interpolation constructs a function matching given data points. For distinct nodes, a polynomial of degree at most through scalar values is unique, but evaluating an expanded high-degree polynomial may be unstable and extrapolation may be poor. Piecewise low-degree interpolation and barycentric polynomial evaluation are useful continuation topics. “It passes through every measured point” does not mean it predicts unmeasured behavior correctly.
G. Independent practice#
F foundation C standard course work T transfer S optional stretch L runnable lab or project. Attempt the complete set before opening its solutions.
Derive the relative condition number of and evaluate it at . Explain why this is not a measurement of the quality of a particular implementation.
Derive the rationalized form of , state its real domain, and explain why direct subtraction can lose relative accuracy for large .
A continuous function has opposite endpoint signs on . How many interval halvings suffice to make the midpoint error bound at most ? Does this prove there is only one root?
Contrast symbolic differentiation, a central finite difference, and automatic differentiation for . Give the exact derivative and explain why a gradient check must use more than one step size or input.
Implement bisection, central_difference, and simpson in numerics_lab.py. Reject invalid brackets, nonfinite evaluations, nonpositive steps, and odd Simpson subdivision counts. Compare with , , and exact cubic integrals. Include an iteration-budget failure and a step too small to change the represented input.
Optional small hints
A05.03: the initial width is one; after halvings the midpoint error is at most . A05.04: the chain rule needs the derivative of both the outer sine and inner square.
H. Complete solutions: A05#
Compare the first step where your work differs. A corrected answer is not yet an independent success.
A05.01Show solution
Local sensitivity
, so when the expression is defined and the relative comparison is meaningful. At 0.99 this is 99. It describes how nearby exact inputs affect exact outputs. An implementation can add further error or be backward stable; the condition number is already large before any implementation is chosen. Near , uncertainty in the denominator is particularly consequential.
Back to A05.01A05.02Show solution
Preserve the domain while changing evaluation order
Multiply by the conjugate over itself:
Both original square roots are real for , and the denominator is positive there. The direct expression subtracts nearly equal rounded values, potentially leaving an error comparable to the small result. The rationalized form adds positive values and divides one by their sum. It avoids that particular cancellation, but still inherits floating-point and input-model limitations.
Back to A05.02A05.03Show solution
Count halvings, not ambiguous iteration labels
Require , equivalently . Therefore halvings suffice: the bound is . A program might label the initial midpoint as iteration one, so compare the actual retained width rather than an unexamined loop count. The sign-changing interval guarantees existence, not uniqueness; several roots may lie in it. Monotonicity or another additional condition would be needed for uniqueness.
Back to A05.03A05.04Show solution
Three mechanisms, one mathematical target
The chain rule gives . Symbolic differentiation produces that expression. A central difference evaluates and and divides their difference by , introducing truncation and rounding effects. Automatic differentiation propagates the derivative of and then sine through the computational graph; it does not use a finite perturbation. Test several ordinary and near-zero inputs and several decreasing step sizes. Agreement at one input might be accidental, especially where a true derivative is zero, while disagreement at a tiny step can come from finite-difference cancellation rather than an incorrect analytic gradient.
Back to A05.04A05.05Show solution
Reference implementation
"""A05.05: bounded numerical algorithms with explicit stopping conditions."""
import math
from collections.abc import Callable
def bisection(f: Callable[[float], float], a: float, b: float,
xtol: float = 1e-10, max_steps: int = 200) -> float:
if not all(math.isfinite(x) for x in (a, b, xtol)) or a >= b or xtol <= 0:
raise ValueError("finite a < b and positive tolerance required")
if type(max_steps) is not int or max_steps <= 0:
raise ValueError("positive step budget required")
fa, fb = f(a), f(b)
if not math.isfinite(fa) or not math.isfinite(fb):
raise ValueError("nonfinite endpoint value")
if fa == 0: return a
if fb == 0: return b
if (fa > 0) == (fb > 0):
raise ValueError("opposite endpoint signs required")
for _ in range(max_steps):
mid = a / 2 + b / 2
fm = f(mid)
if not math.isfinite(fm):
raise ValueError("nonfinite function value")
if fm == 0 or b - a <= 2 * xtol:
return mid
if mid == a or mid == b:
raise ArithmeticError("floating-point stagnation")
if (fm > 0) == (fa > 0):
a, fa = mid, fm
else:
b, fb = mid, fm
raise RuntimeError("iteration budget exhausted")
def central_difference(f: Callable, x: float, h: float) -> float:
if not math.isfinite(h) or h <= 0 or x + h == x or x - h == x:
raise ValueError("positive representable step required")
return (f(x + h) - f(x - h)) / (2 * h)
def simpson(f: Callable, a: float, b: float, n: int) -> float:
if type(n) is not int or n <= 0 or n % 2:
raise ValueError("positive even subinterval count required")
if not math.isfinite(a) or not math.isfinite(b):
raise ValueError("finite endpoints required")
h = (b - a) / n
terms = [f(a), f(b)]
terms += [(4 if i % 2 else 2) * f(a + i * h) for i in range(1, n)]
if any(not math.isfinite(x) for x in terms):
raise ValueError("nonfinite integrand sample")
return h * math.fsum(terms) / 3
if __name__ == "__main__":
print(bisection(lambda x: x * x - 2, 0, 2))
print(central_difference(math.sin, 0.3, 1e-5))
print(simpson(lambda x: x**3, 0, 1, 10))Bisection keeps a root-containing bracket under continuity and sign assumptions. The code compares signs without multiplying potentially enormous endpoint values. It also detects nonfinite evaluation, insufficient iterations, and a midpoint that no longer changes represented endpoints. Central differentiation rejects a step that does not move the input in floating-point arithmetic. Simpson’s weighted sum uses endpoint weights one, odd interior weights four, and even interior weights two, followed by . The tests include exact cubic integration, where the ideal Simpson rule is exact, and known sine/root values with explicit tolerances. Those checks establish selected implementation behavior, not error bounds for every nonsmooth function.
Back to A05.05I. Retention and readiness#
Retrieve limit versus point value, the Intermediate Value Theorem’s continuity condition, and units. Advance when a reported numerical answer includes its problem, approximation method, stopping rule, and an independent check. Further numerical analysis should cover backward error, robust factorizations, adaptive methods, and stability regions in greater depth.