PHOT 411: Numerical Methods for Photonics
LECTURE 04
Michaël Barbier, Spring semester (2024-2025)
Outlook
- 1D Finite difference schemes
- First derivative
- Example 1: Finding maxima/minima as roots
- General difference schemes
- Example 2: Transmission through
- Irregular difference schemes
- Ordinary differential equations
- A simple ordinary differential equation
1D Finite difference schemes
![]()
- Formulas for the derivative \(f_i' \equiv f'(x_i)\) at \(x_i\) are:
| \(\frac{f_i - f_{i-1}}{x_i - x_{i-1}} =\frac{f_i - f_{i-1}}{h}\) |
\(\frac{f_{i+1} - f_{i}}{x_{i+1} - x_{i}} = \frac{f_{i+1} - f_i}{h}\) |
\(\frac{f_{i+1} - f_{i-1}}{x_{i+1} - x_{i-1}} =\frac{f_{i+1} - f_{i-1}}{2h}\) |
Best scheme?
- For on-the-fly calculations, only “old” values available: use backward scheme
- At boundaries: forward for left, backward for right boundary
- Other cases: central difference often has a smaller error
Best scheme?
- For on-the-fly calculations, only “old” values available: use backward scheme
- At boundaries: forward for left, backward for right boundary
- Other cases: central difference often has a smaller error \[
f_{i, \textrm{central}}' = f_i' + f_{i}'''\frac{h^2}{6} + \mathcal{O(h^4)}\quad \Longrightarrow \quad E_\textrm{central} \approx f_{i}'''\frac{h^2}{6}
\] \[
f_{i, \textrm{forward}}' = f_i' + f_{i}''\frac{h}{2} + \mathcal{O(h^2)}\quad \Longrightarrow \quad E_\textrm{forward} \approx f_{i}''\frac{h}{2}
\]
Example error for sine function
- Example: \(f(x) = \sin(x)\), \(f' = \cos(x)\), \(f'' = -\sin(x)\)
- Error central difference mostly smaller than forward difference
- Central(forward) difference performs bad(good) at inflection points of the function (curvature flips sign)
Calculation of the error
- The formula for the central difference: \[
f'_{i, \textrm{central}} = \frac{f_{i+1} - f_{i-1}}{2h}
\]
- Taylor expand the function \(f_{i \pm 1}\)
\[
f_{i \pm 1} = f_{i} \pm f_{i}'h + f_{i}''\frac{h^2}{2!} \pm f_{i}'''\frac{h^3}{3!} + f_{i}''''\frac{h^4}{4!} \pm \dots
\]
\(\longrightarrow\) Use the Taylor expansion in the central difference formula
Calculation of the error
Numerator of \(\,\, f_{i,\textrm{central}}'\):
\[
f_{i+1} - f_{i-1} = \left[f_{i} + f_{i}'h + f_{i}''\frac{h^2}{2!} + f_{i}'''\frac{h^3}{3!} + f_{i}''''\frac{h^4}{4!}\right]\\
- \left[f_{i} - f_{i}'h + f_{i}''\frac{h^2}{2!} - f_{i}'''\frac{h^3}{3!} + f_{i}''''\frac{h^4}{4!}\right]
\]
\[
\Longrightarrow \qquad \frac{f_{i+1} - f_{i-1}}{2h} = \frac{2f_{i}' h + 2 f_{i}'''\frac{h^3}{3!} + \mathcal{O(h^5)}}{2h}
\]
\[
f'_{i, \textrm{central}} = f_{i}' + f_{i}'''\frac{h^2}{3!} + \mathcal{O(h^4)}
\]
Calculation of the error
\[
f'_{i, \textrm{central}} = f_{i}' + f_{i}'''\frac{h^2}{3!} + \mathcal{O(h^4)}
\]
The error \(E\) is the approx. derivative minus the real one:
\[
E \approx f'_{i, \textrm{central}} - f_{i}' = f_{i}'''\frac{h^2}{6} + \mathcal{O(h^4)}
\]
Finite differences in matlab
- Defining an expression/function in Matlab
- Function handles in Matlab:
fh = @exp
- Anonymous function handles:
fh = @(x, y, a) x.^2 + a*y
x = linspace(0, 1, 11);
f = fh(x, 2);
fh = @(x, a) exp(-a*x^2) * sin(3*x);
- functions in Matlab to help with finite differences:
fx = diff(f) / diff(x) % Forward/backward difference
F = -X.^2 + Y.^2; % Example 2D Scalar field
[Fx, Fy] = gradient(F) % Gradient
laplacian = del2(F) % Laplacian
Finding minima/maxima
- Minima/maxima at roots of the derivative
- Use root finding
x = linspace(0, 1, 11);
f = fh(x, 2);
fh = @(x, a) exp(-a*x^2) * sin(3*x);
General finite differences
- A general formula for higher derivatives:
\[
\frac{d^n f_j}{dx^n} = \frac{1}{h^n} \sum_{l=a}^b c_l f_{j+l}
\]
- Finding the coefficients \(c_l\): Taylor expansion
\[
f_{i \pm l} = f_{i} \pm f_{i}'\,lh + f_{i}''\frac{l^2h^2}{2!} \pm f_{i}'''\frac{l^3h^3}{3!} + f_{i}''''\frac{l^4h^4}{4!} \pm \dots
\]
- Fill in Taylor expansion into the formula
General finite differences
\[
\frac{d^n f_j}{dx^n}\left|_\textrm{num}\right. = \frac{1}{h^n} \sum_{l=a}^b c_l f_{j+l} = \sum_{m=0}^\infty \frac{1}{m!} h^{m - n} \left( \sum_{l=a}^b \right) \frac{d^m f_j}{dx^m}
\]
terms with \(m < n\) are zero:
\[
\textrm{For }m = 0 \dots n-1, \qquad \sum_{l = a}^b l^m c_l = 0
\]
term with \(m = n\) should be the derivative:
\[
\frac{1}{n!} \sum_{l=a}^b l^n \,c_l = 1
\]