# Numeric solution diverges

I need to get the numeric solution of a pde but the solution diverges

$$k \frac{\partial^2 u(x,t)}{ \partial x^2} = \frac{\partial u(x,t)}{\partial t}$$

where $$k = 2$$

and

$$u(x,0)=\begin{cases} 0 & x< -1 \\ -100 & -11 \end{cases}$$

here is my code:

trozos[x_] = Piecewise[{{0, x < -1}, {-100, -1 < x < 0}, {100, 0 < x < 1}, {0, x >= 1}}]

NDSolve[{k D[u[x, t], x, x] == D[u[x, t], t], u[x, 0] == trozos[x]}, u[x, t], {x, -2, 2}, {t, 0, 5}]


any help is appreciated

In 12.2, you can follow the link to HeatTransferPDEComponent to set up a transient heat transfer workflow:

trozos[x_] =
Piecewise[{{0, x < -1}, {-100, -1 < x < 0}, {100, 0 < x < 1}, {0,
x >= 1}}];
vars = {Θ[t, x], t, {x}};
Ω = Line[{{-2}, {2}}];
pars = <|"MassDensity" -> 1, "SpecificHeatCapacity" -> 1,
"ThermalConductivity" -> 2|>;
ics = Θ[0, x] == trozos[x];
eqn = HeatTransferPDEComponent[vars, pars] ==
0
Tfun = NDSolveValue[{eqn, ics}, Θ, {t, 0, 5},
x ∈ Ω];
Plot[Tfun[0.001, x], x ∈ Ω]


If you want additional refinement to sharpen the edges, you could do:

Tfun = NDSolveValue[{eqn, ics}, Θ, {t, 0, 5},
x ∈ Ω,
Method -> {"MethodOfLines", "TemporalVariable" -> t,
"SpatialDiscretization" -> {"FiniteElement",
"MeshOptions" -> {"MaxCellMeasure" -> 0.01}}}];
Plot[Tfun[0.00001, x], x ∈ Ω]