This week will introduce the idea of finite difference methods and how they can be used to solve boundary value problems (BVPs). We will first revisit how to perform numerical differentiation on a computer using finite differences. We will then see how the finite difference method can be used to solve BVPs for linear and nonlinear ordinary differential equations (ODEs). The code that you develop this week will form an essential role in the upcoming weeks when the focus shifts towards numerically solving partial differential equations.
Use the links below to find additional notes on
The goal of the exercise this week is to create a BVP solver that is capable of finding numerical solutions to ODEs of the form
where the domain of the problem is given by . In this equation, is a parameter. The term represents a function that depends on the spatial coordinate , the solution , and a parameter . Your solver should be able to handle all three types of boundary conditions (Dirichlet, Neumann, and Robin).
You should design your BVP solver so that it is compatible with the numerical continuation code you developed in Week 17. This will allow you to use numerical continuation to track how the solution changes under the variation of one of the parameters in the problem.
Ideally, the user should be able to select how the algebraic system that arises from discretising the problem will be solved. For example, the discrete problem could be solved using SciPy's root function, NumPy's solve function (if the problem is linear), or your own implementation of Newton's method (see the supplementary notes above on how to implement this).
There is a lot going on in this problem. These steps are designed so that you can see how to build your code up from simpler problems. Test your code extensively and only add complexity once you are certain it works correctly.
Use finite differences to find a numerical solution to
In this case, the exact solution to the problem is given by
which you can use to test your code.
Extend your code so that it can account for a source term in the ODE:
Hint: The simplest place to start is to set ; in this case, the exact solution is given by
Once you've developed code for the problem with , add an dependence into . Can you find some exact solutions for this case?
Generalise your code so that the source term can now depend on the solution as well as a parameter . Use your code to solve the Bratu problem
when and . Plot the solution as a function of . The Bratu problem appears in mathematical models of combustion and thermal runaway, in which case is the temperature.
Hint: If depends nonlinearly on the solution , then a good initial guess of the solution is usually required for the nonlinear solver (e.g. SciPy's root function or Newton's method) to converge. For this problem, a good initial guess can be found by noting that when is small, the exponential can be approximated as . The solution in this case is given by (5), which can be used to form an initial guess.
Update your code so that it can account for Dirichlet, Neumann, or Robin boundary conditions.