Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Summary and Synthesis: Discrete Calculus and Error

How Computers Do Calculus | Numerical Methods Module 1 | COMP 521

Module Summary: Your Numerical Toolkit

This module has given you a complete foundation for numerical computing built on understanding fundamental limitations:

Part 1 established the discrete derivative problem:

Part 2 established the limits of computer arithmetic:

Part 3 provided the mathematical framework:

The diagram is a dependency chain: finite representations and finite-difference approximations lead to Taylor-series error models, which then support practical verification. The same chain supports quadrature rules.

Key Takeaways

Investigation: which derivative should we trust?

You have three plausible answers to the same derivative calculation. One uses more function evaluations, one uses a smaller step, and one agrees with a second calculation to many digits. Which evidence would make you trust an answer? None of those descriptions is sufficient by itself.

We will bring the module together using the smooth, dimensionless function from the Taylor reading,

f(x)=sin⁡(x)e−x2+x3,f(x)=\sin(x)e^{-x^2}+x^3,

at x=0.7x=0.7. The function and its derivative are dimensionless here. We will first inspect numerical evidence, then reveal an analytical reference. This ordering lets us practice a decision that is useful when an exact reference is unavailable, while still having a way to check our reasoning afterward.

Predict: name the mechanism before choosing the repair

Before running the cell, write down:

  1. The expected orders of the forward, central, and fourth-order differences.

  2. How each error should change when hh is halved in its truncation regime.

  3. Whether the smallest hh should give the most accurate answer.

  4. One observation that would make you revise your prediction.

For example, “smaller hh must help” would be contradicted by an error that stops decreasing and then grows. A measured slope is evidence about a regime; it is not a property that every collection of step sizes must display.

h=1.0e-01: 1.5259167327  1.4015937285  1.3862147135
h=1.0e-03: 1.3872721971  1.3860334098  1.3860318490
h=1.0e-05: 1.3860442370  1.3860318491  1.3860318490
h=1.0e-07: 1.3860319736  1.3860318487  1.3860318489
h=1.0e-09: 1.3860318449  1.3860317893  1.3860317523
h=1.0e-11: 1.3860357306  1.3860301795  1.3860311047
h=1.0e-13: 1.3866685578  1.3861134462  1.3859284091
h=1.0e-15: 1.4432899320  1.3877787808  1.3600232052

The columns are forward, central, and fourth-order estimates. Identify a range where the answers approach one another and a range where the extra digits stop being persuasive. Record a candidate before consulting the reference.

Agreement is useful but cannot separate a correct calculation from two methods sharing an input error, a bug, or an unresolved feature. We need evidence tied to the mechanism of approximation.

Reveal the reference and measure the regime

Differentiating the function gives

f′(x)=e−x2(cos⁡x−2xsin⁡x)+3x2.f'(x)=e^{-x^2}\bigl(\cos x-2x\sin x\bigr)+3x^2.

For this exercise, the accuracy requirement is relative derivative error at most 10-8. This is a chosen instructional target, not a default tolerance for scientific work. Because the reference is nonzero, the equivalent absolute allowance is 10−8∣f′(0.7)∣10^{-8}|f'(0.7)|.

<Figure size 800x460 with 1 Axes>
forward calls per estimate: 2 tested h values meeting target: [1.e-09]
central calls per estimate: 2 tested h values meeting target: [3.16227766e-05 1.00000000e-05 3.16227766e-06 1.00000000e-06
 3.16227766e-07 1.00000000e-07 3.16227766e-08 1.00000000e-08
 1.00000000e-10]
central4 calls per estimate: 4 tested h values meeting target: [3.16227766e-03 1.00000000e-03 3.16227766e-04 1.00000000e-04
 3.16227766e-05 1.00000000e-05 3.16227766e-06 1.00000000e-06
 3.16227766e-07 1.00000000e-07 3.16227766e-08 1.00000000e-08]

np.abs takes each discrepancy’s magnitude. The plot compares those magnitudes with a target established before inspecting the results. Setting rtol=0 ensures that isclose uses only the absolute allowance we just derived. The analytical formula is evaluated in binary64, so this comparison cannot resolve arbitrarily small errors; the last few digits of its reference are also subject to floating-point arithmetic.

The call counts describe a single estimate with no caching. They do not include the cost of the exploratory sweep, and they are not timings. Here central and forward differences both use two calls; a fourth-order estimate uses four. A more expensive estimate may earn its cost if it meets an accuracy requirement at a resolution where another stencil does not.

Transfer: change the function evaluations, keep the question

Now suppose every function value has a small deterministic perturbation:

f~(x)=f(x)+δsin⁡(kx),δ=10−7,k=104.\widetilde f(x)=f(x)+\delta\sin(kx),\qquad \delta=10^{-7},\quad k=10^4.

This is a controlled surrogate for imperfect function evaluations, not a statistical model of random noise. We still want the derivative of the original ff. Predict what happens when central differences resolve the perturbation. Will their error relative to f′f' approach zero?

Added derivative term at x=0.7: 0.0008620134342951074
Central estimate at h=1e-5: 1.386892426613606
<Figure size 800x460 with 1 Axes>

Before moving on, write a three-sentence numerical conclusion: what you computed, which evidence supports it, and which uncertainty remains. Those sentences should distinguish truncation, finite precision, and imperfect input information without treating them as interchangeable causes.

Looking Forward

These foundations lead directly to:

Every numerical result is an approximation with assumptions. State the approximation, measure the regime, and test the contract before drawing a scientific conclusion.


Quick Reference Tables

Finite Difference Formulas

MethodFormulaTruncation orderRough hh scale in well-scaled double precision
Forwardf(x+h)−f(x)h\frac{f(x+h) - f(x)}{h}O(h)O(h)ϵ≈10−8\sqrt{\epsilon} \approx 10^{-8}
Backwardf(x)−f(x−h)h\frac{f(x) - f(x-h)}{h}O(h)O(h)ϵ≈10−8\sqrt{\epsilon} \approx 10^{-8}
Centralf(x+h)−f(x−h)2h\frac{f(x+h) - f(x-h)}{2h}O(h2)O(h^2)ϵ1/3≈10−5\epsilon^{1/3} \approx 10^{-5}
4th-order−f(x+2h)+8f(x+h)−8f(x−h)+f(x−2h)12h\frac{-f(x+2h) + 8f(x+h) - 8f(x-h) + f(x-2h)}{12h}O(h4)O(h^4)ϵ1/5≈10−3\epsilon^{1/5} \approx 10^{-3}

Error Types and Mitigation

Error TypeSourceTypical scalingMitigation
Round-offFinite precisionOften amplified like ϵ/h\epsilon/h in first-derivative differences; not universalInspect operation scaling; reformulate or adjust precision/resolution
TruncationApproximation∝hp\propto h^pUse smaller h, higher-order methods
PropagationIterationProblem- and algorithm-dependentStable algorithms, invariants, and cross-resolution checks

When to Use Each Differentiation Method

SituationRecommended MethodReason
Smooth black-box functionFinite differencesUses values without differentiable source; complex-step is another option when its assumptions hold
Smooth function, moderate accuracyCentral differenceGood accuracy/cost balance
Boundary pointsOne-sided differencesSymmetric not possible
Noisy dataModel uncertainty; consider justified smoothing or avoid direct differentiationDerivatives amplify rapid variation; smoothing changes the estimation problem
Many inputs, one scalar outputReverse-mode automatic differentiationGradient arithmetic can cost a constant multiple of the primal computation; storage and primal cost still depend on the problem
Known analytical formAnalytical derivativeRemoves finite-difference truncation; evaluation cost and rounding still matter

Common Reformulations for Stability

Unstable FormStable FormWhen Needed
a−ba - bStore a meaningful relative coordinate when the model permitsNearby large coordinates; cannot recover information already lost in stored inputs
(a2−b2)/(a−b)(a^2 - b^2)/(a - b)a+ba + ba≈ba \approx b
x+1−x\sqrt{x+1} - \sqrt{x}1/(x+1+x)1/(\sqrt{x+1} + \sqrt{x})Large xx
1−cos⁡(x)1 - \cos(x)2sin⁡2(x/2)2\sin^2(x/2)Small xx
log⁡(x)−log⁡(y)\log(x) - \log(y)np.log1p((x-y)/y)Positive nearby inputs; retain their represented difference

Glossary

Automatic differentiation (AD): Technique for differentiating the operations in an implemented computation by applying the chain rule. It avoids the finite-difference truncation error and step-size choice, but still uses floating-point arithmetic and requires differentiable program operations.

Catastrophic cancellation: Severe loss of precision when subtracting nearly equal floating-point numbers. The subtraction can discard digits needed to resolve the small difference.

Central difference: Finite difference method using points symmetrically placed around the evaluation point: f′(x)≈f(x+h)−f(x−h)2hf'(x) \approx \frac{f(x+h) - f(x-h)}{2h}. Achieves O(h2)O(h^2) accuracy.

Convergence: The property that numerical results approach the true solution as a resolution parameter is refined under the method’s assumptions. Check it by observing the predicted trend in a valid refinement regime.

Floating-point arithmetic: System for representing real numbers in computers using a fixed number of bits (typically 64 for double precision), leading to finite precision.

Forward difference: Simplest finite difference using current and next point: f′(x)≈f(x+h)−f(x)hf'(x) \approx \frac{f(x+h) - f(x)}{h}. First-order accurate with O(h)O(h) error.

Machine epsilon (ϵ\epsilon): The spacing from 1 to the next larger representable floating-point value. For double precision: ϵ≈2.2×10−16\epsilon \approx 2.2 \times 10^{-16}.

Optimal step size: The value of hh that minimizes total error by balancing truncation error (decreases with h) and round-off error (increases as h decreases).

Order of accuracy: The power pp in the error scaling O(hp)O(h^p). Higher-order methods have errors that decrease more rapidly as hh is reduced.

Propagation error: Accumulation and amplification of errors through repeated calculations. Critical in long-term integrations like orbital mechanics.

Relative error: Error normalized by the true value: ∣xcomputed−xtrue∣/∣xtrue∣|x_{\mathrm{computed}}-x_{\mathrm{true}}|/|x_{\mathrm{true}}|. It is useful away from zero and misleading when the reference value is near zero.

Round-off error: Error introduced by representing real numbers with finite precision. Fundamental limitation of floating-point arithmetic.

Taylor polynomial and series: A finite polynomial matches derivatives at an expansion point, with a remainder controlling local error. The infinite series ∑n=0∞hnf(n)(x)/n!\sum_{n=0}^{\infty}h^n f^{(n)}(x)/n! equals f(x+h)f(x+h) only where the remainder tends to zero. Smoothness alone is insufficient.

Truncation error: Error from approximating an infinite process with finite terms. In finite differences, it’s the error from neglecting higher-order terms in Taylor series.


These ideas support quadrature. Root finding and time-evolution methods use the same habits of stating assumptions and measuring error.