Off[General::spell1];
Clear[ForwardHeat,a,b,g, n, k, M,u]; 
ForwardHeat[a0_,b0_,g0_,n_,k_,M_] :=
Module[{a,b,g,h,s,i,j},  
  Set@@{a[t_],a0};
  Set@@{b[t_],b0};
  Set@@{g[x_],g0};
  h = (1 / n)//N;
  s = (k / h^2) //N;
u = Table[1,{n+1},{M}];
  For[i=1,i<=n+1 ,i++,
      u[[i,1]] = g[(i-1)*h] //N ];
  For[j=1,j<=M,j++,
      u[[1,j]] = a[j*k] //N ;
      u[[n+1,j]] = b[j*k] //N ];
  For[j=2,j<=M,j++,
    For[i=2,i<=n,i++,
      u[[i,j]]=  s*u[[i-1,j-1]]+
            (1-2s)*u[[i,j-1]] + s*u[[i+1,j-1]]  ];]; ]; 
On[General::spell1]

Clear[g,t,x];
g[x_]:= 4*x -4*x^2; 
a[t_]:=0;
b[t_]:=0;

ForwardHeat[a[t],b[t],g[x], 5, 0.02, 20]

ListPlot3D[u,  AxesLabel->{"t","x","u"},  
ViewPoint->{4,2,3}]

TableForm[Chop[Transpose[u]]];

ForwardHeat[a[t],b[t],g[x], 5, 0.06, 15]

ListPlot3D[u,  AxesLabel->{"t","x","u"},  
ViewPoint->{4,2,3}]

TableForm[Chop[Transpose[u]]];

Clear[g,t,x];
g[x_]:= Sin[Pi*x];
a[t_]:=0;
b[t_]:=0;

ForwardHeat[a[t],b[t],g[x], 10, 0.005125, 200]

ListPlot3D[u,  AxesLabel->{"t","x","u"},  
ViewPoint->{1,2,1}]

uu[x_,t_]:=Exp[- Pi^2 t] * Sin[Pi *x];

Error = Max[Abs[ Table[u[[i,j]]-
    uu[0.1*(i-1),0.005125*(j-1)] //N,  
{i,1,11},{j,1,200}]]]

ForwardHeat[a[t],b[t],g[x], 10, 0.006, 171]

Error = Max[Abs[ Table[u[[i,j]]-
    uu[0.1*(i-1),0.005125*(j-1)] //N,  
{i,1,11},{j,1,171}]]]