Project 26: Numerical Derivative and Tangent Explorer

A self-contained deep-dive from the canonical Math from Foundations to Machine Learning curriculum.

  • Difficulty: Intermediate
  • Time: 1-2 weeks
  • Language: Python with NumPy and Matplotlib
  • Prerequisites: Projects 5-6
  • Merged from: HS P07; ML-Found P05

What You Will Build

Build an interactive calculus laboratory that evaluates a safe set of functions, estimates derivatives using forward, backward, and central differences, and draws the tangent line at a movable point. Plot the original function and derivative together; identify numerical critical-point candidates and classify them using nearby sign changes rather than labels alone. Add an error experiment that sweeps step size h across many orders of magnitude and compares estimates with known analytic derivatives. This reveals both truncation error at large h and floating-point cancellation at tiny h. Include continuous, nonsmooth, and discontinuous examples so the program learns to report when “a derivative value” is not trustworthy.

Real World Outcome

For f(x)=x^3-3x+1, clicking the graph displays the point, secant sample points, central-difference slope, tangent equation, and critical points. A log-scale chart shows derivative error versus h and identifies a useful range. At abs(x) near zero, the tool warns that left and right estimates disagree.

Core Question

How does a limit become a computable slope, and why can making the finite-difference step smaller eventually make the estimate worse?

Concepts You Must Understand First

  1. Difference quotient and limit: derivatives are limiting local rates, not merely symbolic rules. See Stewart, Calculus, derivative chapters.
  2. Tangent as local linear model: f(a)+f'(a)(x-a) approximates nearby values.
  3. Forward versus central difference: central difference cancels leading error and is typically more accurate for smooth functions.
  4. Continuity and differentiability: corners, jumps, and domain boundaries need one-sided or failure diagnostics.
  5. Floating-point cancellation: subtracting nearly equal function values loses significant digits. See Orland, Math for Programmers, ch. 8.

Build Milestones

  1. Implement three finite-difference estimators with explicit function-domain errors.
  2. Plot f, estimated f’, secant points, and a tangent line controlled by mouse or CLI input.
  3. Sweep h and compare each method against analytic references on polynomials, sine, exponential, and logarithm.
  4. Locate derivative sign changes and classify candidate minima/maxima with local evidence.
  5. Add diagnostics for boundaries, discontinuities, corners, overflow, and unstable estimates.

Hints in Layers

  1. Test x^2 at x=3, where the exact slope is 6 and hand calculations are easy.
  2. Compare central estimates at h and h/2; large disagreement is a practical warning signal.
  3. Estimate left and right slopes separately before declaring a derivative near suspicious points.

Common Pitfalls and Debugging

  • Symptom: error grows as h approaches zero. Cause: subtractive cancellation. Fix: graph error over h and choose a scale-aware default.
  • Symptom: a corner is reported as a smooth critical point. Cause: only a symmetric estimate was checked. Fix: compare one-sided slopes.
  • Symptom: tangent line is vertically displaced. Cause: the line formula omitted f(a) or used x instead of x-a. Fix: require it to pass exactly through (a,f(a)).

Definition of Done

  • Forward, backward, and central estimates pass known-function tests.
  • The tangent visualization updates and passes through the selected point.
  • An error-versus-h experiment demonstrates both error regimes.
  • Nonsmooth and domain-boundary cases produce meaningful warnings.
  • Critical points are supported by derivative sign evidence.
  • Outputs are reproducible and compared with analytic references.

Previous: Project 25 · Complete learning path · Next: Project 27