% Apply boundary conditions K(1,:) = 0; K(1,1) = 1; K(nx+1,:) = 0; K(nx+1, nx+1) = 1;
$$-\nabla^2u = f$$
% Compute the load vector F = zeros(nx+1, 1); for i = 1:nx+1 F(i) = f(i*k); end
% Solve the linear system u = K\F;
For 2D problems, such as the Poisson equation:
% Apply boundary conditions K(1,:) = 0; K(1,1) = 1; K(nx+1,:) = 0; K(nx+1, nx+1) = 1;
$$-\nabla^2u = f$$
% Compute the load vector F = zeros(nx+1, 1); for i = 1:nx+1 F(i) = f(i*k); end
% Solve the linear system u = K\F;
For 2D problems, such as the Poisson equation: