# Solution from NDSolveValue contradicts the boundary condition

I have a PDE problem, and here is my code:

solN = Module[{k = 1, A = 1, u0 = 1, l = 1, \[Beta] = 1, T = 10},
NDSolveValue[{D[u[x, t], t] - k D[u[x, t], x, x] ==
A Exp[-\[Beta] t] + NeumannValue[0, x == 0], u[l, t] == 0,
u[x, 0] == u0}, u, {x, 0, l}, {t, 0, T},
Method -> {"PDEDiscretization" -> {"MethodOfLines",
"SpatialDiscretization" -> {"FiniteElement"}}}]]

Manipulate[Plot[{solN[x, t]}, {x, 0, 1}, PlotRange -> {0, 1.3}], {t, 0, 1}]


From the manipulation we can see that the solution contradicts the boundary condition at x==1, so how to fix that? Edit: I know the initial conditions contradict the boundary conditions at point x==1,t==0, however this PDE has an analytical solution. The following codes show the analytical solution and its plot.

nsol = Compile[{x, t, n}, Module[{k = 1, A = 1, u0 = 1, l = 1, \[Beta] = 1},
Sum[(Sqrt*Cos[(Pi*x*(-1 + 2*i))/(2*l)]*((2*(-1)^i*Sqrt*Sqrt[l]*u0)/(E^((k*Pi^2*t*(-1 + 2*i)^2)/(4*l^2))*(Pi - 2*Pi*i)) +
(8*(-1)^i*Sqrt*A*(E^((-t)*\[Beta]) - E^(-((k*Pi^2*t*(1 - 2*i)^2)/(4*l^2))))*l^(5/2))/((-4*l^2*\[Beta] + k*Pi^2*(1 - 2*i)^2)*(Pi - 2*Pi*i))))/Sqrt[l], {i, 1, n}]]]
Manipulate[
Plot[nsol[x, t, n], {x, 0, 1},
PlotRange -> {0, 1.3}], {t, 0, 1}, {n, Range}]

• This is only a guess about the reason. But you specify conflicting boundary/initial condition. You actually specify: u[0, t] == 0, u[1, t] == 0, u[x, 0] ==1. What should the value of u[0,0] and u[1,0] be? It looks like MMA simply ignores the boundary conditions. Mar 30, 2021 at 8:58

btw, you have this

        Warning: boundary and initial conditions are inconsistent.


You can change IC to use Piecewise to make BC and IC agree.

k = 1; A = 1; u0 = 1; L = 1; β = 1; T = 10;
solNFiniteElements =
NDSolveValue[{D[u[x, t], t] - k D[u[x, t], x, x] ==
A Exp[-β t] + NeumannValue[0, x == 0],
u[L, t] == 0,
u[x, 0] == Piecewise[{{u0, 0 <= x < L}, {0, x == L}}]
},
u, {x, 0, L}, {t, 0, T},
Method -> {"PDEDiscretization" -> {"MethodOfLines",
"SpatialDiscretization" -> {"FiniteElement"}}}
]


And now

Manipulate[
Plot[solNFiniteElements[x, t0], {x, 0, 1}, PlotRange -> {0, 1.3}],
{{t0, 0, "time"}, 0, 0.5, .01, Appearance -> "Labeled"},
TrackedSymbols :> {t0}
] Due to abrupt change in solution at t=0 from 1 to zero at x=L, solution at t=0 will not be smooth. But will be at any time after t=0