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
- Difference quotient and limit: derivatives are limiting local rates, not merely symbolic rules. See Stewart, Calculus, derivative chapters.
- Tangent as local linear model:
f(a)+f'(a)(x-a)approximates nearby values. - Forward versus central difference: central difference cancels leading error and is typically more accurate for smooth functions.
- Continuity and differentiability: corners, jumps, and domain boundaries need one-sided or failure diagnostics.
- Floating-point cancellation: subtracting nearly equal function values loses significant digits. See Orland, Math for Programmers, ch. 8.
Build Milestones
- Implement three finite-difference estimators with explicit function-domain errors.
- Plot f, estimated f’, secant points, and a tangent line controlled by mouse or CLI input.
- Sweep h and compare each method against analytic references on polynomials, sine, exponential, and logarithm.
- Locate derivative sign changes and classify candidate minima/maxima with local evidence.
- Add diagnostics for boundaries, discontinuities, corners, overflow, and unstable estimates.
Hints in Layers
- Test
x^2at x=3, where the exact slope is 6 and hand calculations are easy. - Compare central estimates at h and h/2; large disagreement is a practical warning signal.
- 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 ofx-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.
Navigation
Previous: Project 25 · Complete learning path · Next: Project 27