# How to check the correctness of my implementation of a numerical scheme for differential equation

I know the method of constructing solutions. For example I have a BVP:

$$u_{xx} + u = 0$$ subjected to: $$u_{x}(0) = f_1,$$ $$u_{x}(1) = f_2$$

If I want to check the correctness of my numerical implementation, just plug in $u(x) = \sin(x)$ and then $f_1 = \cos(0), f_2 = \cos(1)$ and then run the script for implementation. If the graphs of the numerical solution and that of $u(x) = \sin(x)$ approximately matches then I have the correct implementation.

This may present some problems when constructing solutions with not-so-simple differential equations. Currently I am having trouble finding one for: $u_t = ku_{xx}$.

Are there any other methods or is there a simple function for this one?

• – Paul Oct 6 '14 at 18:28

After you have implemented that RHS function, you should generate a range of numerical solutions on a sequence of successively finer meshes. Then you should compute the difference between your computed values to the exact value on each mesh in some norm or other metric. The choice of that norm should be based on the convergence theory that you have for your numerical method. At that point, the goal is to check to see if your method achieves the order of accuracy that the theory predicts. This is typically $O(h^{(p+k)})$ where $h$ is the mesh size, $p$ is the order of the method, and $k$ is dependent on the norm you chose.
If, for small enough $h$, your method behaves like the theory predicts, then you've made one measure of the accuracy/correctness of your implementation.
• To be clear: The method of manufactured solutions only works for $u_t - k u_{xx} = f$, where $f$ is not fixed a priori and may depend on $k$ (and, of course, your chosen exact solution $\bar u$). – Christian Clason Oct 6 '14 at 15:46
• Thanks for responding. What I have understood is that I should be implementing rather: $u_{t} - ku_{xx} = f$ subjected to: $u(x,0) = g_1, u_{x}(0,t) = g_2, u_x(1,t) = g_3$. Now a choice of $u(x,t) = 1+x^2t^2 + xt$ gives $f = 2ktx^2 + x-2t^2, g_1 = 1, g_2 = 0, g_3 = 2t^2 + t$ and if now my $norm(u_{app} - u_{ex}) \le epsilon$ at each iteration then I am good. Matched graphs are another indicator of the same conclusion. Correct? – Sohail Oct 6 '14 at 15:48
• @BillBarth I wasn't knocking it, just trying to point out to Sohail that he can apply it in his case as long as he allows for a right-hand side that may depend on $k$ (because he seemed to think the method could not be applied for his example). – Christian Clason Oct 6 '14 at 17:30