# Different answer using Dsolve or NDSolve to solve a PDE

I'm working on getting a numerical solution for the following PDE: $$u_t-u_{xx}=0,\ x\in[0,1], t\in[0,1],\\ u(x,0)=\sin(2\pi x),\\ u(0,t)=0,\\ u_x(1,t)=2\pi e^{-t}.$$ The hand solution for this PDE is $$u(x,t)=e^{-t}\sin(2\pi x)$$.

I tried DSolve to get the analytical solution.

DSolveValue[{D[u[x, t], {t, 1}] - D[u[x, t], x, x] == 0,
u[x, 0] == Sin[2 \[Pi] x], u[0, t] == 0,
Derivative[1, 0][u][1, t] == 2 \[Pi] E^-t}, u[x, t], {x, t}]


$$\underset{K[1]=1}{\overset{\infty }{\sum }}\sqrt{2} \sin \left(\frac{1}{2} \pi x (2 K[1]-1)\right) \left(-\frac{32 (-1)^{K[1]} \left(-\cosh (t)+e^{-\frac{1}{4} \pi ^2 t (1-2 K[1])^2}+\sinh (t)\right) \sqrt{2}}{\pi \left(4-\pi ^2 (1-2 K[1])^2\right) (1-2 K[1])^2}-\frac{128 (-1)^{K[1]} e^{-\frac{1}{4} \pi ^2 t (2 K[1]-1)^2} \sqrt{2}}{\pi (1-2 K[1])^2 \left(4 K[1]^2-4 K[1]-15\right)}\right)+2 \pi e^{-t} x$$

However, DSolve gives an answer containing Inactive[Sum].

I also tried NDSolve:

sol = NDSolveValue[{D[u[x, t], {t, 1}] - D[u[x, t], x, x] == 0,
u[x, 0] == Sin[2 \[Pi] x],
u[0, t] == 0, (D[u[x, t], x] /. x -> 1) == 2 \[Pi] E^-t},
u[x, t], {x, 0, 1}, {t, 0, 1},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid",
"MinPoints" -> 50}}];
Plot3D[{sol, E^-t Sin[2 \[Pi] x]}, {x, 0, 1}, {t, 0, 1}]


It turns out that the numerical solution differs from the analytical one.

• Separation of variables can't fullfill your ic and bc all! Nov 14, 2022 at 8:28

I verified it. Your hand solution exp(-t)*sin(2*Pi*x) is wrong. It does not verify the pde at all. It only verifies the BC/IC

pde = D[u[x, t], t] - D[u[x, t], {x, 2}] == 0
ic = u[x, 0] == Sin[2 Pi x]
bc = {u[0, t] == 0, (D[u[x, t], x] /. x -> 1) == 2 Pi  Exp[-t] }
DSolve[{pde, ic, bc}, u[x, t], {x, t}]


solNeeded = u -> Function[{x, t}, Exp[-t]*Sin[2*Pi*x]]
pde /. solNeeded


You see, it is not an identity.

most solutions for pde's come out as Fourier series and so the sum there is what one would expect. (from applying separation of variables, that is what should come out).

Mathematica's solution also agrees with Maple's

pde:=diff(u(x,t),t)-diff(u(x,t),x\$2)=0;
ic:=u(x,0)=sin(2*Pi*x);
bc:=u(0,t)=0,D[1](u)(1,t)=2*Pi*exp(-t);
pdsolve([pde,ic,bc])


does my code have any problem or am I missing something here?

Unless both Mathematica and Maple (the two most advanced CAS systems in the world) are both wrong and you are right, I would say the error is in your hand solution :)

• Thank you for the detailed answer! I've made a stupid mistake here :-P Nov 14, 2022 at 5:33