# Trying to solve a differential equation with a piecewise initial condition

I am trying to solve $$u_t=\frac{1}{4}u_{xx}$$ $-\infty<x<\infty,\: t>0$

With the initial condition $u(x,0)=\phi(x)$ where

$$\phi(x)= \left\{ \begin{array}{lr} 1 & |x|<1\\ 0 & |x|\geq 1 \end{array} \right.$$

So I initially typed in my piecewise function like this

ϕ[x_] := Piecewise[{{1, Abs[x]<1}, {0, Abs[x] >= 1}}]


But I have also tried this, (are they both valid, or is just one, or is neither?)

ϕ[x_] := Piecewise[{{1, -1 < x < 1}, {0, x >= 1 || x <= -1}}]


Then, following the mathematica examples I tried to solve the differential equation:

pde = D[u[x, t], t] - 1/4 D[u[x, t], {x, 2}] == 0
soln = DSolve[{pde, u[x, 0] == ϕ[x]}, u[x, t], {x, t}]


Which outputs a bunch of weird stuff, not the solution (I think) I was expecting. If my math is correct I think the solution to this would involve the error function. Any tips to where I might be going wrong would be aprreciated!

• DSolve can't handle heat conduction equation, consider NDSolve instead if a numerical solution is OK, or you can refer to this post. – xzczd Mar 26 '14 at 2:49
• You may not like this answer: use NDSolve, it works in this case. – János Tóth Mar 26 '14 at 9:00
• Observe that you may also use $\phi$[x_] = UnitBox[x/2]. – murray Mar 27 '14 at 16:05

Based on the comments above

\[Phi][x_] := Piecewise[{{1, Abs[x] < 1}, {0, Abs[x] >= 1}}];
pde = D[u[x, t], t] - 1/4 D[u[x, t], {x, 2}] == 0 ; soln =
NDSolve[{pde, u[x, 0] == \[Phi][x]}, u[x, t], {x, -5, 5}, {t, 0, 10}]
ffn[x_, t_] = u[x, t] /. soln // First
Animate[Plot[ffn[x, t], {x, -5, 5}, PlotRange -> {{-5, 5}, {0, 1}},
Filling -> Axis, ColorFunction -> "TemperatureMap",
ColorFunctionScaling -> False], {t, 0, 10},
AnimationRunning -> False] I'd like to add a solution based on FourierTransform:

eqn = D[u[x, t], t] - 1/4 D[u[x, t], {x, 2}];
ϕ[x_] = UnitBox[x/2];

teqn = FourierTransform[#, x, w] & /@ eqn
tbc = FourierTransform[ϕ[x], x, w]

(* Notice in the following code u[x, t]
actually means the Fourier transform of u[x, t] *)
tsol = DSolve[{teqn == 0 /. _[a_, x, w] :> a, tbc == u[x, 0]}, u[x, t], t]

sol = InverseFourierTransform[tsol[[1, 1, 2]], w, x, Assumptions -> t > 0]

1/2 (Erf[(1 - x)/Sqrt[t]] + Erf[(1 + x)/Sqrt[t]])


BTW, I found another bug of InverseFourierTransform: without Assumptions -> t > 0 it won't give the right answer. This bug exists at least in v8.0.4, v9.0.1, v10.0.0 . (thanks for Sector's test.)

Finally an illustration for sol:

Plot3D[sol, {x, -3, 3}, {t, 0, 10}, ColorFunction -> "TemperatureMap"] 