-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multigrid1D_Vcycle_GenMat.m
56 lines (45 loc) · 1.14 KB
/
Multigrid1D_Vcycle_GenMat.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function [A_list, R_list, max_level] = Multigrid1D_Vcycle_GenMat(A, direct_n, isTwo)
n = size(A, 1);
A_list = {};
R_list = {};
level = 1;
A_list(level) = {A};
% Levels are created by halving the original mesh until it reaches the
% limit for the direct solver
if isTwo
coarse_n = floor((n - 1) / 2);
% Construct full-weighted restriction operator
R = sparse(coarse_n, n);
for i = 1 : coarse_n
col = 2 * i - 1;
R(i, col) = 0.25;
R(i, col + 1) = 0.5;
R(i, col + 2) = 0.25;
end
R_list(level) = {R};
P = 2 * R';
A = R * A * P;
level = level + 1;
A_list(level) = {A};
n = coarse_n;
else
while (n > direct_n)
coarse_n = floor((n - 1) / 2);
% Construct full-weighted restriction operator
R = sparse(coarse_n, n);
for i = 1 : coarse_n
col = 2 * i - 1;
R(i, col) = 0.25;
R(i, col + 1) = 0.5;
R(i, col + 2) = 0.25;
end
R_list(level) = {R};
P = 2 * R';
A = R * A * P;
level = level + 1;
A_list(level) = {A};
n = coarse_n;
end
end
max_level = level;
end