T0[x_]:=Sin[lambda*x]

lambda=3;L=Pi*1.;Plot[T0[x],{x,0,L}]

pb=100;
A=SparseArray[
{
{i_,i_}->-2,
{i_,j_}/;Abs[i-j]==1->1
},{pb-1,pb-1}]

Identita=SparseArray[{
{i_,i_}->1
},{pb-1,pb-1}]

dx=L/pb;

grid=Range[dx,L-dx,dx]

T[0]=T0[grid]
ListLinePlot[Transpose[{grid,T[0]}]]

dt=0.1;
alpha=0.5;

solver=LinearSolve[Identita-alpha*(dt/dx^2)A]

tend=.1;pc=tend/dt
For[
n=0,n<pc,n++,
{
b=Identita.T[n]+(1-alpha)*(dt/dx^2)*A.T[n],
T[n+1]=solver[b]
}
]
ListLinePlot[Transpose[{grid,T[n]}],PlotRange->{-1,1}]

Animate[ListLinePlot[Transpose[{grid,T[n]}],PlotRange->{-1,1}],{n,0,pc,1},AnimationRunning->True]

Show[Plot[Sin[lambda*x]*Exp[-lambda^2*pc*dt],{x,0,Pi}],ListLinePlot[Transpose[{grid,T[n]}],PlotRange->{-1,1}]]

(*chyba*)

error=0;
For[
n=1,n≤pc,n++,
For[i=1,i≤pb-1,i++,
error=error+(Sin[lambda*i*dx]*Exp[-lambda^2*n*dt]-T[n][[i]])^2
]
]
error=Sqrt[dx*dt*error]

0

0.06362511390012063` pri t=0.05 , pri pb=50

0.010301564263808564` pri t=0.1 , pri pb=50

0.010678185013158278` pri t=0.1 , pri pb=100