I’ve been asked to calculate numerically the solution of this IVP using Euler’s explicit method on Mathlab:
$$u_t-2u_{xx}=0$$ $$u(x,0)=\sin (2\pi x), \quad x\in(0,1)$$ $$u(0,t)=u(1,t)=0, \quad t\in(0,0.05)$$
However, I find the approximation is really bad for $h = 0.04, k = 4 · 10^{-3}$ (here the exact solution is shown in blue and the red dots correspond to the approximated values):
...then it gets considerably better for a slightly inferior value of $k$: here the same plot is shown for $h = 0.04, k = 4 · 10^{-5}$:
I’d like to know how is this phenomenon called and why does it occur for this particular IVP. Is there any conditions a numerical method has to meet in order not to present this kind of problems?


You run afoul of the limited A-stability of the explicit Euler method. If you write the discretized PDE as ODE system $\dot u=F(u)$, then $L=\frac8{h^2}$ is a Lipschitz constant, one can probably reduce that by some percent. A rule-of-thump says that for $Lk>2$ the method is unstable, for $Lk\approx 1$ mostly looking sensible and with $Lk\le 0.5$ starting to give quantitatively correct results.
In your failed experiment you have $L=5000$ and $Lk=20$, thus instability. In the second case you get $Lk=0.2$ which corresponds to sensible results with the errors still visible in the plot.
You can get an even more detailed picture by applying the discrete Fourier-transform to the space discretization. Then the equations for the different frequencies are decoupled from each other. Note that the Euler iteration in space mode provides a continuing source of floating point noise to the amplitude equations in frequency mode. So even amplitudes starting at zero can be perturbed and then amplified by the instability effects.