# Inconsistent initial and boundary conditions in heat equation [duplicate]

I want to solve the heat equation in 1-D. When I evaluate the following

NDSolve[
{D[u[t, x], t] == D[u[t, x], {x, 2}],
(D[u[t, x], x] /. x -> 0) == 1,
(D[u[t, x], x] /. x -> Pi) == 1,
u[0, x] == Cos[2 x] + x},
u[t, x], {t, 0, 1}, {x, 0, Pi}][]


I get the error

initial and boundary conditions are inconsistent

but I can't see why. I use version 10.2.0.0

• Mathematica assumes 3 pde's: D[u,t]==... Are you shure about your 2 boundary conditions (which usually have to be of lower order as the pde. – Ulrich Neumann Dec 26 '17 at 14:22
• I see nothing wrong with the initial and boundary conditions, or with the answer returned. – bbgodfrey Dec 26 '17 at 14:57
• Your bc's match your ic, so that warning is erroneous. The final result looks good though. – Bill Watts Dec 26 '17 at 18:43
• Sorry my comment was wrong. NDSolve gives only a warning (perhaps pointweise inconsintent?) but the NDSolve result seems to be ok in version 11.0.1! – Ulrich Neumann Dec 26 '17 at 18:52
• To be specific, add e.g. Method -> {"MethodOfLines", "SpatialDiscretization" -> {"TensorProductGrid", "MinPoints" -> 40}} to NDSolve will resolve your problem. – xzczd Dec 27 '17 at 4:28

You can avoid this warning

NDSolve[{D[u[t, x], t] == D[u[t, x], {x, 2}], (D[u[t, x], x] /. x -> 0) ==1,
(D[u[t, x], x] /. x -> Pi) == 1, u[0, x] == Cos[2 x] + x}
,u[t, x], {t, 0, 1}, {x, 0, Pi},
Method -> {"MethodOfLines","SpatialDiscretization" -> {"TensorProductGrid",
"MinPoints" -> 35}}] []


if you enforce a smoother spacial grid("MinPoints" > 31).

An approach without refinement is "FiniteElement" (works only with NeumannValue instead of the two bc D[u[t,x],x]==... )

NDSolveValue[{D[u[t, x], t] ==D[u[t, x], {x, 2}] + NeumannValue[1, x == 0 ||x == Pi],
u[0, x] == Cos[2 x] + x}, u , {t, 0, 1}, {x, 0, Pi},
Method -> {"MethodOfLines", "TemporalVariable" -> t,"SpatialDiscretization" -> {"FiniteElement" }}]