# Derivation of numerical scheme for linear transport equation on a variable stencil

The question is about automatica derivation of coefficients of numerical scheme on a variable stencil.

So, lets consider 1d transport equation $$(1)\qquad u_t+u_x=0.$$ To discretize (1), let's consider the following stencil: $$(2)\qquad (t^{n+1},x_{j-1}),\,(t^{n},x_{j-1}),\,(t^{n},x_{j}).$$ Then, let's consider the following approximation of (1) on (2): $$(3)\qquad u_j^{n+1} = a_1u_{j-1}^n+a_2u_j^n,$$ where $a_1,\,a_2$ to be determined by Taylor series expansion.

My first step seems to be as follows:

eqn = D[u[x, t], t] + D[u[x, t], x] == 0
approx = Series[u[x, t + τ], {t, 0, 2}] -
(a1 Series[u[x - h, t], {x, 0, 2}] +
a2 Series[u[x, t], {x, 0, 2}])


The question is how to derive differential consequence from (1) to obtain system of two equation on $a_i,\,i=1,2$?

• So what is the question? And do you want to do this with the computing software Mathematica? If so, please explain what specifically you are having difficulties with in implementing your algorithm in Mathematica. If not, your question belongs elsewhere, perhaps math.se. – march Nov 3 '15 at 19:29
• Sure, edited just now. – Oleg Kravchenko Nov 3 '15 at 19:37
• I'm still a little confused. Do you want to use Mathematica to show that the discretization of Eq. 1 results in Eq. 3, and as a consequence derive what $a_1$ and $a_2$ should be? – march Nov 3 '15 at 19:40
• I guess, my question is the second one. Means, that we hold stencil (2) and approximation (3), consequently. The question is how to obtain $a_1,\,a_2$ by unknow coefficient method with the usage of differential consequences of (1). – Oleg Kravchenko Nov 3 '15 at 19:44

Here's how I go about doing this type of algebra with Mathematica:

eqn = D[u[x, t], t] + D[u[x, t], x] == 0;


First, expand the function u[x, t + dt] to first order in dt around 0 and the function u[x -h, t] to first order in h around 0:

eqn1 = u[x, t + dt] == Series[u[x, t + dt], {dt, 0, 1}] // Normal
eqn2 = u[x - h, t] == Series[u[x - h, t], {h, 0, 1}] // Normal


We want to eliminate the partial derivatives, so we solve for them:

sols = First@Solve[{eqn1, eqn2}, {D[u[x, t], t], D[u[x, t], x]}]


Finally, we use the fact that the partial derivatives are negative each other because of the original differential equation to eliminate them from the equation:

differenceEquation = eqn /. sols


Finally, we solve for the next time step and collect terms on the right hand side

Collect[Equal @@ Solve[differenceEquation, u[x, dt + t]][[1, 1]], u[x, t]]


From there, you can read off a1 and a2.

• Ok, but how such derivation could be adopted on the following scheme, for example, $$u_j^{n+1} = a_1u_{j-1}^n + a_2 u_{j}^n + a_3 u_{j+1}^n, \quad \text{(Lax-Wendroff scheme)}$$ or $$u_j^{n+1} = a_1u_{j-1}^n + a_2 u_{j}^n + a_3 u_{j+1}^n + a_4 u_{j+2}^n?$$ – Oleg Kravchenko Nov 23 '15 at 18:26
• @olekravchenko. I can't think of a way of automating derivations of the different schemes, because they use different choices for the finite-difference approximations of the derivatives. I would just generalize what I did above, but in general my method requires knowledge of how the derivation is done in the first place. Nonetheless, in the meantime, I'll think on it. You might consider asking a new question about how to automate the derivation of general schemes on general stencils. I just answered this question originally assuming that all you wanted was the particular one shown. – march Nov 23 '15 at 19:00
• In fact I wrote on a variable stencil to ask question in more wide sence. Anyway, let me follow your advice and ask new question about automatical derivation on quite general stencil. – Oleg Kravchenko Nov 23 '15 at 19:39