I found something this tutorial for method of line doesn't tell us.
Consider the following toy example:
eqn = With[{u = u[x, t]},
D[u, t] == D[u, x] + D[u, {x, 2}] + D[u, {x, 3}] - D[u, {x, 4}]];
ic = u[x, 0] == 0;
bc = {u[0, t] == 0, u[1, t] == 0, D[u[x, t], x] == 0 /. {{x -> 0}, {x -> 1}}};
NDSolve[{eqn, ic, bc},
u, {x, 0, 1}, {t, 0, 2},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid", "DifferenceOrder" -> 4}}]
Guess what difference order is chosen when those spatial derivatives (in this case $\frac{\partial u}{\partial x}$, $\frac{\partial ^2u}{\partial x^2}$, $\frac{\partial ^3u}{\partial x^3}$, $\frac{\partial ^4u}{\partial x^4}$) are discretized?
"What a needless question! The order is 4, as we set with "DifferenceOrder" -> 4! " About an hour ago, I thought so, too. But it's not true. Let's check the difference formula generated by NDSolve:
state = First@NDSolve`ProcessEquations[{eqn, ic, bc},
u, {x, 0, 1}, {t, 0, 2},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid", "DifferenceOrder" -> 4}}];
funcexpr = state["NumericalFunction"]["FunctionExpression"]
Introduction for
NDSolve`ProcessEquationscan be found intutorial/NDSolveStateDataandtutorial/NDSolveDAE.
Then check the "DifferenceOrder" of these NDSolve`FiniteDifferenceDerivativeFunction:
Head[#]@"DifferenceOrder" & /@ funcexpr[[2, 1]]
(* {{7}, {6}, {5}, {4}} *)
The order is not 4! Similarly, we can verify that it's the same case for PDE system:
eqn =
With[{u = u[x, t], v = v[x, t]},
{D[u, t] == D[u, x] + D[u, {x, 2}] + D[u, {x, 3}] - D[u, {x, 4}],
D[v, t] == D[v, x] + D[v, {x, 2}]}];
ic = {u[x, 0] == 0, v[x, 0] == 0};
bc = {{u[0, t] == 0, u[1, t] == 0,
D[u[x, t], x] == 0 /. {{x -> 0}, {x -> 1}}},
{v[0, t] == 0, v[1, t] == 0}};
state =
First@NDSolve`ProcessEquations[{eqn, ic, bc}, u, {x, 0, 1}, {t, 0, 2},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid", "DifferenceOrder" -> 4}}];
funcexpr = state["NumericalFunction"]["FunctionExpression"]
Head[#]@"DifferenceOrder" & /@ funcexpr[[2, 1]]
(* {{7}, {7}, {6}, {6}, {5}, {4}} *)
So, for a PDE or PDE system whose maximum spatial differential order is omax, when "DifferenceOrder" -> n is set for "TensorProductGrid", the actual difference order for m-order spatial derivative is omax + n - m.
In certain cases, this design seems to cause trouble, here's an example.
To make this post a question, I'd like to ask:
Why
NDSolvechooses this design?If the 1st question is too hard, is there a easy way (e.g. a hidden option) to make
NDSolveuse the same difference order for every spatial derivative?







fixfunction,NDSolveindeed uses the same difference order for all the spatial derivatives? 2. Whether or not thefixfunction can be applied when solving 2 coupled equations withNDSolveValue? Btw, I found when solve 2 eqns with different highest spatial-derivative orders, your conclusion in OP would be modified as For a system of PDEs whose maximum spatial differential order is omax=max{eqn_1, eqn_2, ... eqn_i} ...** – Nobody Dec 08 '21 at 07:16fixhas been broken since v11.3. 1. Well, I think it's clear if one understands the source code offix. Anyway, if you want to check, just add aSowto myfixand useReapto extract the modifiedNDSolve\StateData[……]` and do what I've done in the question. 2. Yes, it works for PDE system. 3. Thx for pointing out. The statement in the question is revised. – xzczd Dec 08 '21 at 07:41NDSovlealways use implicit representation of finite difference? If not, how could we know the scheme? – lxy May 01 '22 at 04:16MethodOfLines.NDSolveis not using finite difference method in $t$ direction, it uses an ODE or DAE solver. Please read the Introduction section of the tutorial The Numerical Method of Lines carefully. – xzczd May 01 '22 at 06:46