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
Discrete Functions
Taylor Series Approximations to Derivatives
Forward, Backward, and Central Differences
David Mayerich
STIM Laboratory, University of Houston
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;
}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];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
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:
David Mayerich
STIM Laboratory, University of Houston
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)\):
David Mayerich
STIM Laboratory, University of Houston
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''\)
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'\)
David Mayerich
STIM Laboratory, University of Houston
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
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}\)
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
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
Calculate \(f'(x)\) where \(f(x)=|x|\) at \(x=0.5\) and \(x_\Delta = 0.75\)
David Mayerich
STIM Laboratory, University of Houston