Calculus: Differentiation
Numerical Methods
David Mayerich
Scalable Tissue Imaging and Modeling (STIM) Laboratory
Department of Electrical and Computer Engineering
Cullen College of Engineering
University of Houston
David Mayerich
STIM Laboratory, University of Houston

Finite Difference Methods
Discrete Functions
Taylor Series Approximations to Derivatives
Forward, Backward, and Central Differences
David Mayerich
STIM Laboratory, University of Houston
Specifying Functions
-
Formulaic or algorithmic:
David Mayerich
STIM Laboratory, University of Houston
double f(double x, double y, int n) {
return cos(exp(x)) + sin(y) * jn(n, x);
}-
Look-up tables:
It is common to use a uniform spacing between samples: \(x_{n+1} = x_n + x_\Delta\)
double* cos_lut(int n) {
double* lut = new double[n];
double dx = (2.0 * M_PI) / (double)n;
for(int i = 0; i < n; i++) {
double x = i * dx;
lut[i] = cos(x);
}
return lut;
}Specifying Functions
-
Formulaic or algorithmic:
David Mayerich
STIM Laboratory, University of Houston
double f(double x, double y, int n) {
return cos(exp(x)) + sin(y) * jn(n, x);
}-
Look-up tables:
It is common to use a uniform spacing between samples: \(x_{n+1} = x_n + x_\Delta\)
double* cos_lut(int n) {
double* lut = new double[n];
double dx = (2.0 * M_PI) / (double)n;
for(int i = 0; i < n; i++) {
double x = i * dx;
lut[i] = cos(x);
}
return lut;
}
int N = 100;
double theta = 1.23;
double* cos = cos_lut(N);
int xi = theta / (2 * M_PI) * N;
double cos_x = cos[xi];Numerical Differentiation
-
May not have access to the algorithm used to generate \(f(x)\)
-
Some algorithms and optimization (ex. Newton's method) rely on \(f'(x)\)
-
Can we approximate derivatives from samples or implementations of a function?
David Mayerich
STIM Laboratory, University of Houston
Approximations from Taylor Expansion
-
Start from a first-order Taylor series:
David Mayerich
STIM Laboratory, University of Houston
-
Given two known points \(c=x_i\) and \(x=x_{i+1}=x_i + x_\Delta\):
-
Solve for \(f'(x)\):
-
Note that the order in the error term is reduced:
Forward Finite Difference
David Mayerich
STIM Laboratory, University of Houston
Backwards Differences
-
Consider evaluating the function at a new position \(f(x_{i-1})\), where \(x_{i-1}=x_i - x_\Delta\):
David Mayerich
STIM Laboratory, University of Houston
-
Solve for \(f'(x)\):
Forward and Backward Differences
David Mayerich
STIM Laboratory, University of Houston
Second Order Approximations
-
Start from a 2nd-order Taylor expansion:
David Mayerich
STIM Laboratory, University of Houston
-
Substitute \(x=x_{i+1}\), \(x_i=c\), and \(x_\Delta = x_{i+1} - x_i\):
-
Substitute \(x=x_{i-1}\), \(x_i=c\), and \(x_\Delta = x_{i-1} - x_i\):
-
This formulation requires the higher-order derivative \(f''\)
Central Difference Approximation
-
Given the second-order approximation for \(f(x_{i-1})\) and \(f(x_{i+1})\) from the previous slide:
David Mayerich
STIM Laboratory, University of Houston
-
Remove \(f''\) by calculating \(f(x_{i+1})-f(x_{i-1})\):
solve for \(f'\)
Finite Differences
David Mayerich
STIM Laboratory, University of Houston
Standard Finite Difference Methods
-
First forward finite difference approximation:
David Mayerich
STIM Laboratory, University of Houston
-
First backward finite difference approximation:
-
First central finite difference approximation:
at \(O(x_\Delta)\) error
at \(O(x_\Delta)\) error
at \(O(x_\Delta^2)\) error
Mean Value Theorem
-
The mean value theorem can be derived from the central difference approximation:
David Mayerich
STIM Laboratory, University of Houston
-
Assume we want to evaluate \(f(b)-f(a)\) where \(b > a\):
where \(\theta = x_i = \frac{b - a}{2}\)
Examples
-
Compute the derivative of \(\log{x}\) using forward and central differences with 3 significant digits, \(x=100\), and \(x_\Delta=10\). How many bits are lost? What is the relative error?
David Mayerich
STIM Laboratory, University of Houston
Forward
Central
Examples
-
Compute the derivative using central differences and 3 significant figures
David Mayerich
STIM Laboratory, University of Houston
\(f(x)=e^x\) for \(x=1\) and \(x_\Delta=0.5\)
-
What is the relative error?
-
How many bits are lost if \(x_\Delta = 0.01\)
between \(5\) and \(6\) bits are lost
Examples
-
Calculate \(f'(x)\) where \(f(x)=|x|\) at \(x=0.5\) and \(x_\Delta = 0.75\)
David Mayerich
STIM Laboratory, University of Houston
G.1 Differentiation
By STIM Laboratory
G.1 Differentiation
- 79