The exact solution Although our interest is in approximate numerical solutions of u0D au, it is convenient to know the exact analytical solution of the problem so that we can calculate the error in numerical approximations. We say that the numerical approximation, that is, the collection of un values for n D Nt, constitutes a mesh function. Equation (1.6) is the discrete counterpart of the original ODE problem (1.1), and is often referred to as a finite difference scheme or more commonly as the discrete equations of the problem.
A fundamental characteristic of these equations is that they are algebraic and can therefore be directly solved to produce a grid function, i.e. approximate values of u at grid points:un,nD Nt. Explanation There is a very intuitive explanation of the FE scheme shown in the sketch below. Since we know at the last calculated point, the differential equation gives us the slope of the solution curve: u0D au.
There are several choices of difference approaches in step 3 of the finite difference method as presented in the previous section. The accuracy of this fraction as an approximation to the derivative ofu depends on where we look for the derivative: in the middle of the intervalŒtn; tnC1or at the endpoints. The quantity must therefore be expressed by the quantities we actually produce, i.e. the numerical solution at the mesh points.
The difficulty is that evaluating the right-hand side at an arbitrary point faces the same problem as in Section 1.1.4: the point value must be expressed by discrete quantities that we calculate from the scheme, i.e., unandunC1.
Constant Time Step
Mathematical Derivation of Finite Difference Formulas
Given a function f .x/ that is sufficiently smooth (i.e. f .x/ has "enough derivatives"), a Taylor polynomial of degree m can be used to approximate the value of the function f .x/ if we subtract the values and its first derivatives know at another pointx Da. For a function of time, f .t /, related to a mesh with spacing t, we often need the Taylor polynomial approximation atf .tn˙t /givenf and its derivatives at t Dtn. This first term lees12f00.tn/tand can be taken as a measure of the error in the Forward Euler formula.
The backward difference To derive the backward difference, we use the Taylor polynomial approximation atf .tnt. The term 12f00.tn/t can be taken as a simple measure of the approximation error since it will dominate over the other terms ast !0.
Compact Operator Notation for Finite Differences
Note that for D 12 we recover the standard difference in the center and the standard arithmetic mean. By looking at the various examples above and comparing them to the underlying differential equations, we immediately see which difference approximations are used and at what point they apply.
Implementations
- Computer Language: Python
- Making a Solver Function
- Integer Division
- Doc Strings
- Formatting Numbers
- Running the Program
- Plotting the Solution
- Verifying the Implementation
- Computing the Numerical Error as a Mesh Function
- Computing the Norm of the Error Mesh Function
- Experiments with Computing and Plotting
- Memory-Saving Implementation
Most of the function names are similar to those in the alternative scientific computing language MATLAB. It is strongly recommended to equip each function with a doc string, unless the purpose of the function is not clear. Nevertheless, the following text deviates from this rule if the function is explained in the text.
The percent signs indicate "slots" in the text where the variables listed at the end of the statement are inserted. With more than one curve in the plot, we need to associate each curve with a legend. The purpose of verifying a program is to bring evidence to the property that there are no errors in the implementation.
We always need to do a verification before it makes sense to believe the calculations and do a validation (comparing the program results to physical experiments or observations). Comparison of these manual calculations with the result of the solver function is performed in the function. Instead of calling test_*()functions explicitly, one would normally ask a test framework like nose or pytest to find and execute such functions.) The full program including the above verification can be found in filedecay_v3.py8.
Now that we have some proof of correct implementation, we can compare the calculated non-values in the matrix with the exact values at the grid points to examine the error in the numerical solution. Note that suandu_e mesh functions are represented by arrays and associated with points in the array. Instead of working with erroren on the entire mesh, we often want a single number that expresses the size of the error.
It focuses on a single point with a maximum value of jfj, while other norms measure the average behavior of the function. If jjfjj2`2 (i.e. the square of the norm) is used instead of the trapezoidal integration formula, the error is t .f0/2C.fNt/2/=2. This means that the weights at the endpoints of the grid function are perturbed, but at !0 the error due to this perturbation goes to zero.
Also, people prefer to divide by the total length of the vector, Nt C1, rather than Nt. The convert -trim command removes the surrounding white areas in an image (an operation commonly known as cropping in image processing programs). Let's critically evaluate how much we really need to store in computer memory for our implementation of the method.
We have implemented this memory saving idea in filedecay_memsave.py15, which is a slight modification of the decay_plot_mpl.py16 program.
Exercises
Define a mesh function and visualize it
Differentiate a function
Experiment with divisions
At the endpoints we use forward and backward differences: .. a) Write a function differential(u, dt) that returns the discrete derivative dn of the mesh function un.
Experiment with wrong computations
Change formatting of numbers and debug