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

lambda=3.;L=Pi*1.;Plot[phi0[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}]

h=L/pb;

grid=Range[h,L-h,h];

phi[0]=phi0[grid];
ListLinePlot[Transpose[{grid,phi[0]}]]

dt=0.1;
alpha=0.5;

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

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

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

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

(*(dF/dt-d^2F/dx^2=0)
F=F(x,t),
F^(i)_n=F(x_n,t^i),

(F_n(t))'-(  ( F_(n+1)(t)   -2F_n(t)    +   F_(n-1)(t) )/h^2)=0

((F_n^(i+1)-F_n^i)/(tau))-    (  ( F_(n+1)^i   -2F_n^i   +   F_(n-1)^i )/h^2)=0

(F_n^(i+1)-F_n^i)-(tau/h^2)*(  ( F_(n+1)^i   -2F_n^i   +   F_(n-1)^i )   )=0

F_n^(i+1)=F_n^i+(tau/h^2)*(  ( F_(n+1)^i   -2F_n^i   +   F_(n-1)^i )) ;*)

(*
maticovo

I .F^(i+1)=I. F^i+(tau/h^2) * A.F^i *)

F_n^(i+1)-(tau*α/h^2)* (  ( F_(n+1)^(i+1)   -2F_n^(i+1)   +   F_(n-1)^(i+1)))

=F_n^i+(tau/h^2)*(1-α) (  ( F_(n+1)^i   -2F_n^i   +   F_(n-1)^i )) ;

I.F^(i+1)-α(tau/h^2)*(  A.F^(i+1))=I. F^i+(1-α)(tau/h^2) * A.F^i

a