Bug introduced in 12.1.1 or earlier - Fixed in Version: 12.2.0
Suppose you have the DE
$$ \frac{d}{dx} \left( c(x) \left[\frac{d}{dx}u(x)\right] \right) + n(x) = 0 $$
and you want to solve for $u(x)$ with some BCs with given $c(x)$ and $n(x)$. I thought that solving this with the formulations
de1 = D[c[x]*D[u[x], x], x] + n[x];
de2 = Inactive[Div][c[x]*Inactive[Grad][u[x], {x}], {x}] + n[x];
which at least in a symbolic form are the same in 1D
de1 == Activate@de2
True
would yield the same results. But no no no! I do not get the same results, see below, I do not understand why. Can you help me out? I am working with Mathematica 12.0.0.0
Let's define some region boundaries for $x$ through xReg
, impose some BCs with uBC
, define $c$ and $n$, and finally set up a solver usol
for given de
.
xReg = {-3, 10};
uBC = {0, 7};
c[x_] := (5 + Sin[x])*(7 + 2*Cos[x]);
n[x_] := 50*Sin[x];
bc = {
DirichletCondition[u[x] == uBC[[1]], x == xReg[[1]]],
DirichletCondition[u[x] == uBC[[2]], x == xReg[[2]]]
};
usol[de_] :=
NDSolveValue[{de == 0, bc}, u, {x, xReg[[1]], xReg[[2]]}];
In Mathematica 12.0.0.0 I get the following different results after solving de1
and de2
u1 = usol[de1];
u2 = usol[de2];
Plot[{u1[x], u2[x]}, {x, xReg[[1]], xReg[[2]]}, PlotRange -> All,
PlotLegends -> {"D", "Inactive - Div - Grad"}]
I simply do not understand why. I have read parts of the documentation (Formal Partial Differential Equations), but the use of Inactive
is somehow unclear to me in this example. In terms of a naive observation, the solution u1
obtained with D
seems to be right, which yiels n1
in the figure below, i.e., n1
$\approx$ -n
. n2
, computed from u2
with Inactive
does not show good results (yellow and green curves corresponding to n2
and n22
based on u2
are on top of each other).
n1 = D[c[x]*D[u1[x], x], x];
n2 = Div[c[x]*Grad[u2[x], {x}], {x}];
n22 = D[c[x]*D[u2[x], x], x];
Plot[{n1, n2, n22, -n[x]}, {x, xReg[[1]], xReg[[2]]},
PlotLegends -> {"n1", "n2", "n22", "-n"}]
Further questions:
- Is this solved in newer Mathematica versions?
- Does the internal FEM do something weird to the DE? If yes, then I am concerned that the solution of user21 in my other old question might be questionable due to the usage of
Inactive
withDiv
andGrad
in the provided nonlinear 1D example.
NeumannValue[0,pred]
on the boundary in the FEM method? $\endgroup$