-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_matrix.m
39 lines (33 loc) · 1022 Bytes
/
build_matrix.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function A = build_matrix(N)
% Define constants based on grid spacing
h = 1 / (N + 1);
a = 4 / h^2;
b = -1 / h^2;
% Total number of unknowns in the grid
M = N^2;
% Initialize sparse matrix
A = sparse(M, M);
% Loop over each grid point (i, j)
for i = 1:N
for j = 1:N
% Convert (i, j) to linear index
idx = (i - 1) * N + j;
% Set the main diagonal entry
A(idx, idx) = a;
% Set the horizontal neighbors if they exist
if i > 1 % left neighbor
A(idx, idx - N) = b;
end
if i < N % right neighbor
A(idx, idx + N) = b;
end
% Set the vertical neighbors if they exist
if j > 1 % bottom neighbor
A(idx, idx - 1) = b;
end
if j < N % top neighbor
A(idx, idx + 1) = b;
end
end
end
end