Matlab program with the Crank-Nicholson method for the diffusion equation
https://www.youtube.com/watch?v=HiCdEG7uw7E
%Heat diffusion in one dimension wire within
%Parameters to define the heat equation
cond = 1/2; % Heat conductivity parameter
L = 1.; % Length of the wire
T = 1.; % Final Time
%Parametes for Crank-Nocholson
Nt = 2500; % Number of time steps
Dt = T/Nt;
Nx = 50; % Number of space steps
Dx = L/Nx;
b = cond*Dt/(Dx*Dx);% Alpha Value
%Initial Condition
for n = 1:Nx-1
x(n) = (n-1)*Dx;
u(n,1) = sin(pi*x(n));
end
%Boundary Condition
for j = 1:Nt+1
u(1,j) = 0.; % Boundary is 0 for any moment
u(Nx+1,j) = 0.;
t(j) = (j-1)*Dt;
end