Numerical MethodUnit 47 min read
Numerical Differentiation – Forward, Backward, Central, Error Analysis
Unit 4 of Numerical Method: covers definitions of numerical differentiation, forward/backward/central difference formulas, truncation and round‑off errors, error analysis, algorithmic implementation, C programming, and real‑world applications such as speed estimation from GPS data and rate of change of transaction coun
Key points
- Numerical differentiation approximates derivatives using discrete data points.
- Forward, backward, and central difference formulas differ in truncation error and stability.
- Truncation error is \(O(h^2)\) for central, \(O(h)\) for forward/backward; round‑off error grows as \(h\) decreases.
- Practical algorithms combine truncation and round‑off to choose optimal step size.
- Applications include velocity estimation from GPS, rate of change of stock prices, and traffic flow analysis.
1. Introduction
Numerical differentiation is the process of estimating the derivative of a function when only discrete values of are available. It is indispensable in data analysis, engineering simulations, and real‑time control systems where analytical derivatives are unavailable or expensive to compute.
The basic idea is to replace the limit definition
by a finite difference that uses a small but finite step .
2. Forward, Backward, and Central Difference Formulas
| Formula | Approximation | Truncation Error | Order |
|---|---|---|---|
| Forward | |||
| Backward | |||
| Central |
The central difference is generally preferred because its truncation error is two orders higher.
The figure shows the sine function and the points used in the central difference formula.
3. Error Sources
| Source | Description | Typical Size | Mitigation |
|---|---|---|---|
| Truncation | Difference between exact derivative and finite‑difference formula | or | Use higher‑order formulas, reduce |
| Round‑off | Finite precision arithmetic introduces small errors | Choose not too small, use extended precision | |
| Cancellation | Subtracting nearly equal numbers amplifies errors | Large when | Use central difference, increase slightly |
| Discretization | Data points are not uniformly spaced | Depends on spacing | Interpolate to uniform grid or use non‑uniform formulas |
The total error is approximately the sum of truncation and round‑off errors. An optimal step size balances the two:
4. Worked Example – Analytical Derivative of
Compute for at using central difference with .
Exact derivative: .
Central difference:
Numerical values:
Error: .
The truncation error is , close to the observed error.
5. Worked Example – Real‑World Application: Speed Estimation from GPS
A Pathao delivery bike records its position every second.
Positions (in meters) at times s:
We want the instantaneous speed at s.
Using central difference:
If the actual speed (from a high‑precision sensor) is , the error is (≈16.7 %).
The error arises from both truncation (due to non‑linear acceleration) and measurement noise.
6. Algorithm for Numerical Differentiation
flowchart TD
"Start" --> "Input f(x), x, h"
"Choose scheme" --> "If central then"
"Compute f(x+h)" --> "Compute f(x-h)"
"Compute derivative" --> "Output f'(x)"
"If forward/backward then"
"Compute f(x±h)" --> "Compute derivative"
"Output f'(x)"
"End"The algorithm is straightforward: evaluate the function at the required points and apply the chosen finite‑difference formula.
7. C Implementation
#include <stdio.h>
#include <math.h>
/* Forward difference */
double forward_diff(double (*f)(double), double x, double h) {
return (f(x + h) - f(x)) / h;
}
/* Backward difference */
double backward_diff(double (*f)(double), double x, double h) {
return (f(x) - f(x - h)) / h;
}
/* Central difference */
double central_diff(double (*f)(double), double x, double h) {
return (f(x + h) - f(x - h)) / (2.0 * h);
}
/* Example function: sin(x) */
double func(double x) {
return sin(x);
}
int main(void) {
double x = M_PI / 4.0;
double h = 0.1;
printf("Forward: %f\n", forward_diff(func, x, h));
printf("Backward: %f\n", backward_diff(func, x, h));
printf("Central: %f\n", central_diff(func, x, h));
return 0;
}
Compile with gcc -std=c11 -O2 -lm diff.c -o diff and run.
8. Error Analysis – Truncation vs Round‑off
The truncation error for central difference is
The round‑off error, assuming machine epsilon , is
Total error: .
Plotting vs shows a U‑shaped curve: decreasing reduces truncation error until round‑off dominates.
The minimum of the black curve gives the optimal .
9. Applications
| Domain | Idea Used | How |
|---|---|---|
| Transportation | Velocity estimation from GPS | Central difference on successive GPS coordinates |
| Finance | Rate of change of stock prices | Forward difference on daily closing prices |
| Healthcare | Heart rate variability analysis | Derivative of ECG signal sampled at high frequency |
| Engineering | Stress analysis in materials | Numerical derivative of displacement field from sensors |
In the real world
- Pathao delivery bikes: GPS positions sampled every second are differentiated to compute instantaneous speed, enabling dynamic routing and driver performance monitoring.
- eSewa transaction monitoring: The number of transactions per minute is recorded; a forward difference gives the rate of change, helping detect sudden surges or fraud.
- NEPSE price charts: Daily closing prices are differentiated to estimate the daily return rate, used by traders for momentum strategies.
10. Summary
Numerical differentiation transforms discrete data into derivative estimates using finite differences. The central difference formula offers the best accuracy for smooth functions, but practical use requires balancing truncation and round‑off errors by selecting an appropriate step size. The concepts are directly applicable to everyday technologies such as GPS navigation, financial analytics, and health monitoring systems.
In the real world
- Pathao – GPS coordinates of delivery vehicles are differentiated to compute speed and acceleration, enabling real‑time traffic management.
- eSewa – Transaction counts per minute are differentiated to detect abnormal spikes, indicating potential security incidents.
- NEPSE – Daily closing prices are differentiated to calculate daily returns, informing trading decisions.
Exam tip
- Memorize the forward, backward, and central difference formulas and their truncation error orders.
- Be able to derive the truncation error using Taylor series.
- Practice computing derivatives for simple functions and for real‑world data sets.
- Understand how to choose an optimal step size by balancing truncation and round‑off errors.
- For coding questions, write clear, modular C functions for each difference scheme.
Device that records position over time, derivative gives velocity. (Image: Siarhei Besarab, CC BY-SA 4.0, via Wikimedia Commons)
Based on the TU BIT syllabus for Numerical Method (BIT203), unit 4.
Discussion
Loading…