%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% Forward Euler method for the solution of ODEs of the form:
% y'(t)  =  f(t,y(t))
% The entries of this function are:
% a) Initial time
% b) Final time
% c) Number of time steps between initial and final time steps
% d) The function f(t,y(t))
% e) Initial condition y(t=0)=y0
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function [u,t,x]  =  BackwardEulerHeatEquationOneD(t0,tf,NTime,a,b,Nx,u0)

Dt  =  (tf-t0)/(NTime-1);  %  Time step constante
t   =  (linspace(t0,tf,NTime))';  %  Variable independiente, normalmente tiempo


L   =  abs(b-a);
h   =  L/(Nx-1);
x   =  (linspace(0,L,Nx))';
Identity  =  eye(Nx-2);


u           =  zeros(Nx,NTime);             %  Inicialización de la variable dependiente

u(2:end-1,1)      =  u0;

for itime=2:length(t)
    Kstar             =  Identity + (Dt/h^2)*MyToeplitz(Nx-2);
    u(2:end-1,itime)  =  Kstar\u(2:end-1,itime-1);        
end

 
