Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

 Open In Colab

In the past lecture, we saw how to write derivatives in terms of the function evaluated at discrete points. Let’s use it and see what we’ve been building up to.

Lets take an unkown function f(x)f(x) and discretize it by sampling it at a set of coordinates. We saw that finite difference benefits from equal spacing, so lets sample it with an even step size hh.

For a function y(x)y(x) we would get:

23.03.01-Finite-difference.png

Finite difference allows us to express the derivatives of y(x)y(x) in terms of the vector elements. For step size hh, we have:

Finite Difference ApproximationFormula in terms of yiy_iOrder of Accuracy
Forward difference (1st derivative)y′(xi)≈(yi+1−yi)/hy'(x_i) \approx (y_{i+1} - y_i) / hO(h)
Backward difference (1st derivative)y′(xi)≈(yi−yi−1)/hy'(x_i) ≈ (y_i - y_{i-1}) / hO(h)
Central difference (1st derivative)y′(xi)≈(yi+1−yi−1)/(2h)y'(x_i) ≈ (y_{i+1} - y_{i-1}) / (2h)O(h²)
Forward difference (2nd derivative)y′′(xi)≈yi+2−2yi+1+yih2y''(x_i) ≈ \frac{y_{i+2} - 2y_{i+1} + y_i} {h^2}O(h)
Backward difference (2nd derivative)y′′(xi)≈(yi−2yi−1+yi−2)/h2y''(x_i) ≈ (y_i - 2y_{i-1} + y_{i-2}) / h²O(h)
Central difference (2nd derivative)y′′(xi)≈(yi+1−2yi+yi−1)/h2y''(x_i) ≈ (y_{i+1} - 2y_i + y_{i-1}) / h^2O(h²)

The finite difference method applied to the discretized function lets us write the differential equation in terms neighbouring values (or the boundary conditions)!

Example: Rocket tragectory

We are going out to launch a rocket, and let y(t) be the altitude (meters from the surface) of the rocket at time t. We know the gravity g=9.8m/s2g=9.8m/s^2.

If we want to have the rocket at 50 m off the ground 5 seconds after launching, what should be the velocity at launching? (we ignore the drag of the air resistance).

The ODE is

d2ydt2=−gyi+1−2yi+yi−1h2=−g\begin{align} \frac{d^2y}{dt^2} &= -g \\ \frac{y_{i+1} - 2y_{i} + y_{i-1}} {h^2} &= -g \end{align}

This expression only applies in interior points. We must use the boundary conditions y(0)=0y(0) = 0 and y(5)=50y(5) = 50.

Let’s take n=10n=10. Since the time interval is [0,5][0, 5] and we have n=10n=10, therefore, h=0.5h=0.5. Using the finite difference approximated derivatives, we have

y0=0y_0 = 0
yi−1−2yi+yi+1=−gh2,  i=1,2,...,n−1y_{i-1} - 2y_i + y_{i+1} = -gh^2, \;i = 1, 2, ..., n-1
y10=50y_{10} = 50

This looks awfully familiar...

It’s just our familiar linear system!

[101−21⋱⋱⋱1−211][y0y1...yn−1yn]=[0−gh2...−gh250]\begin{bmatrix} 1 & 0 & & & \\ 1 & -2 & 1 & & \\ & \ddots & \ddots & \ddots & \\ & & 1& -2& 1 \\ & & & &1 \end{bmatrix}\left[\begin{array}{c} y_0 \\y_1 \\ ... \\ y_{n-1}\\y_n \end{array}\right] = \left[\begin{array}{c} 0 \\-gh^2 \\ ... \\ -gh^2 \\50\end{array}\right]

and how do we solve linear systems?!?

[[ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 1. -2.  1.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  1. -2.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  1. -2.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1. -2.  1.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  1. -2.  1.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  1. -2.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1. -2.  1.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  1. -2.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  1. -2.  1.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.]]
[ 0.   -2.45 -2.45 -2.45 -2.45 -2.45 -2.45 -2.45 -2.45 -2.45 50.  ]

<Figure size 1000x800 with 1 Axes>

For the velocity, y′(0)y'(0) we can calculate using the forward difference:

32.05

which is pretty good compared to the analytic answer 34.5 .

Example: Neuman boundary condition

Using finite difference method to solve the following linear boundary value problem:

y′′(x)=−4y(x)+4xy''(x) = -4y(x) +4x

with the boundary conditions: y(0)=0y(0) = 0 and y′(π/2)=0y'(\pi/2)=0.

The Dirichlet boundary condition is simple:

y0=0y_0 = 0

The interior points are a little more complex. We need to move all the terms with the dependant quantity (yy) to the left and everything else (including xx terms) to the right.

y′′(x)=−4y(x)+4xyi−1−2yi+yi+1h2+4yi=4xiyi−1−2yi+yi+1+4h2yi=−4h2xi\begin{align} y''(x) &= -4y(x) +4x \\ \frac{y_{i-1} - 2 y_i + y_{i+1}}{h^2} +4y_i &= 4x_i \\ y_{i-1} - 2y_i + y_{i+1} + 4 h^2 y_i&= -4 h^2 x_i \end{align}

for i=1,2,...,n−1i = 1, 2, ..., n-1

But what should we do for the boundary y′(π/2)=0y'(\pi/2) = 0?

We can actually just write it out exactly in finite difference:

yi−yi−1h=0yi−yi−1=0\begin{align} \frac{y_i - y_{i-1}}{h} &= 0 \\ y_i - y_{i-1} = 0 \end{align}

Using matrix notation, we have:

[101−2+4h21⋱⋱⋱1−2+4h21−11][y0y1...yn−1yn]=[04h2x1...4h2xn−14h2xn]\begin{bmatrix} 1 & 0 & & & \\ 1 & -2+4h^2 & 1 & & \\ & \ddots & \ddots & \ddots & \\ & & 1& -2+4h^2 & 1 \\ & & & -1 & 1 \end{bmatrix}\left[\begin{array}{c} y_0 \\y_1 \\ ... \\ y_{n-1}\\y_n \end{array}\right] = \left[\begin{array}{c} 0 \\4h^2x_1 \\ ... \\ 4h^2x_{n-1} \\4h^2x_{n}\end{array}\right]

Lets see what we get compared to the analytic answer y=x+0.5sin⁡2xy=x+0.5 \sin2x

<Figure size 1000x800 with 1 Axes>
Higher order accuracy boundary

Alternately, as a bit of a trick to gain accuracy, the Neuman condition at the boundary can be considered with the central difference for yny_n (despite yn+1y_{n+1} not existing!):

yn+1−yn−12h=0yn+1=yn−1\begin{align} \frac{y_{n+1}-y_{n-1}}{2h} &= 0\\ y_{n+1} &= y_{n-1} \end{align}

We substitute this into the equation for the interior points to get:

2yn−1−2yn−h2(−4yn+4xn)=02y_{n-1}-2y_n -h^2(-4y_n+4x_n)=0

which should have second order accuracy.

Now we have

[101−2+4h21⋱⋱⋱1−2+4h212−2+4h2][y0y1...yn−1yn]=[04h2x1...4h2xn−14h2xn]\begin{bmatrix} 1 & 0 & & & \\ 1 & -2+4h^2 & 1 & & \\ & \ddots & \ddots & \ddots & \\ & & 1& -2+4h^2 & 1 \\ & & & 2 &-2+4h^2 \end{bmatrix}\left[\begin{array}{c} y_0 \\y_1 \\ ... \\ y_{n-1}\\y_n \end{array}\right] = \left[\begin{array}{c} 0 \\4h^2x_1 \\ ... \\ 4h^2x_{n-1} \\4h^2x_{n}\end{array}\right]

Lets compare this solution vs the previous formula in terms of y′(π/2)y'(\pi/2)

Old
1.5641951194959582
1.5678968928038604
1.5691723081328415
1.569759027706592
New
1.5641814012620596
1.5678951310352263
1.5691718937395551
1.569758892476149
True
1.5707963267948966

The higher order implementation is more accurate (for the same computational complexity), but the step size still domainates the quality of the answer.

Example 3: Nonlinear systems

Solve the boundary value problem,

y′′=−3yy′\begin{align} y'' &= -3 y y' \end{align}

with y(0)=0y(0) = 0 and y(2)=1y(2) = 1.

The Dirichlet boundary conditions are easy, and the interior points are given by:

yi−1−2yi+yi+1h2=−3yi[yi+1−yi−12h]\begin{align} \frac{y_{i-1} -2 y_i +y_{i+1}}{h^2} &= -3 y_i \bigg[ \frac{y_{i+1} - y_{i-1}}{2h} \bigg] \end{align}

How do we solve this?

Since this system is nonlinear we will need to use a nonlinear solver which is a root finder! In particular, we need to find the root of the residual,

yi−1−2yi+yi+1+3hyi[yi+1−yi−1]=0R⃗(y⃗)=0⃗\begin{align} y_{i-1} -2 y_i +y_{i+1} + 3 h y_i [y_{i+1} - y_{i-1}] &=0 \\ \vec{R}(\vec{y}) &= \vec{0} \end{align}
 message: The solution converged.
 success: True
  status: 1
     fun: [-3.711e-26  2.691e-11 -9.336e-11  1.270e-10 -4.548e-12
           -1.767e-10  1.645e-10 -2.525e-11 -5.023e-11  3.169e-11
            0.000e+00]
       x: [-3.711e-26  3.024e-01  5.545e-01  7.347e-01  8.498e-01
            9.181e-01  9.570e-01  9.785e-01  9.902e-01  9.966e-01
            1.000e+00]
  method: hybr
    nfev: 21
    fjac: [[-7.178e-01 -6.963e-01 ...  2.602e-17  0.000e+00]
           [ 5.736e-01 -5.914e-01 ...  2.989e-03  0.000e+00]
           ...
           [ 1.560e-02 -1.609e-02 ... -8.190e-01  0.000e+00]
           [ 0.000e+00 -0.000e+00 ...  0.000e+00 -1.000e+00]]
       r: [-1.393e+00  1.342e+00 ... -1.040e+00 -1.000e+00]
     qtf: [-2.429e-10  5.987e-10 -2.227e-09  3.264e-10 -1.230e-09
            2.292e-09  2.220e-09 -3.106e-09 -8.640e-10  5.212e-10
            0.000e+00]
<Figure size 640x480 with 1 Axes>

Example 4: Multidimensional

Solve the discrete Poisson equation on the unit square,

−Δu=1-\Delta u = 1 on Ω=[0,1]2\Omega=[0,1]^2

with

u=0u=0 on ∂Ω\partial \Omega

The interior points are given by −Δu=1-\Delta u = 1 on Ω=[0,1]2\Omega=[0,1]^2

For central difference on a 2D grid with spacing h in both x and y directions, the discrete Laplacian at a point (i,j) is:

(u(i+1,j)+u(i−1,j)+u(i,j+1)+u(i,j−1)−4u(i,j))/h2=1(u(i+1,j) + u(i-1,j) + u(i,j+1) + u(i,j-1) - 4u(i,j)) / h^2=1
<Figure size 640x480 with 2 Axes>
<Figure size 640x480 with 1 Axes>

#Summary

Benefits

  1. Simplicity: The finite difference method is relatively easy to understand and implement, making it accessible for many users.

  2. Efficiency: It can be computationally efficient, especially for problems with simple geometries and boundary conditions.

  3. Structured Grids: Works well with structured grids, which can simplify the discretization process.

  4. High-Order Approximations: It is possible to obtain high-order approximations, which can improve the accuracy of the solution.

Drawbacks

  1. Limited Flexibility: The method is less flexible compared to other methods like finite element or finite volume methods, particularly for complex geometries.

  2. Stability Issues: Finite difference methods can suffer from stability issues, especially for certain types of partial differential equations.

  3. Boundary Conditions: Handling complex boundary conditions can be challenging.

  4. Accuracy: The accuracy of the method can be limited by the discretization error and round-off error.