MATLAB Created 2026-10-24 Updated 2026-11-05
MATLAB is a numerical programming language and computing environment organized around arrays and matrix operations. A matrix multiplication expresses a mathematical linear-algebra operation directly, while elementwise multiplication applies independently to corresponding entries; confusing the two changes the computation. The environment includes visualization and numerical libraries, with additional capabilities supplied through toolboxes. Most ordinary numerical calculations use finite-precision arithmetic, so algebraically equivalent expressions can differ through rounding. The language and environment are developed by MathWorks.[ref]
For consistency with the supplied wavenumbers, use an FFT grid on a periodic interval: , , and similarly for . In MATLAB, replace the endpoint-inclusive coordinate definitions by x = (0:nx-1)*Lx/nx; and y = (0:ny-1)*Ly/ny;. The supplied linspace arrays instead have spacing while the Fourier multipliers assume . Including both copies of the periodic endpoint creates a grid inconsistency, separate from high-frequency aliasing.
A spatially uniform source changes only the zero Fourier mode. Its exact integrated increment over a time step beginning at is
The unnormalised two-dimensional discrete Fourier transform stores a constant field as in element (1,1).[ref] Keep the existing homogeneous exponential multiplication and add this source increment to that element:
for i = 1:nt
    t = (i-1)*dt;
    u_hat = u_hat .* exp(-alpha*K2*dt);
    dq = exp(-t/10) - exp(-(t+dt)/10);
    u_hat(1,1) = u_hat(1,1) + nx*ny*dq;
end
This replacement uses the original variable names and MATLAB indexing. The uniform forcing of a periodic heat equation is integrated exactly in time here. No source multiplier is applied to the other modes, and the zero-mode homogeneous multiplier equals one. Adding only dq to the unnormalised coefficient would underestimate the physical temperature increment by the factor .