diffusionCoefficient = 1;
numGridPoints = numIntervals + 1;
dx = 2 * domainLength / numIntervals;
x = -domainLength + (0:numIntervals) * dx;
dt = dx^2 / (2 * diffusionCoefficient);
alpha = dt * diffusionCoefficient / dx^2;
mainDiagonal = 2 * (1 + alpha) * ones(numGridPoints, 1);
offDiagonal = -alpha * ones(numGridPoints, 1);
A = spdiags([mainDiagonal, offDiagonal, offDiagonal], [0 -1 1], numGridPoints, numGridPoints);
identityMatrix = speye(numGridPoints);
A([1 numGridPoints], :) = identityMatrix([1 numGridPoints], :);
sigma = domainLength / 16;
u = (1 / (sigma * sqrt(2 * pi))) * exp(-0.5 * (x / sigma).^2);
plot(x, u, 'r'); hold on;
xlabel('$x$', 'Interpreter', 'latex', 'FontSize', 14);
ylabel('$u(x, t)$', 'Interpreter', 'latex', 'FontSize', 14);
title('Solution of the Diffusion Equation', 'Interpreter', 'latex', 'FontSize', 16);
for timeStep = 1:numTimeSteps
alpha * u(1:numGridPoints-2) + 2 * (1 - alpha) * u(2:numGridPoints-1) + alpha * u(3:numGridPoints); ...
if mod(timeStep, plotFrequency) == 0