-
Notifications
You must be signed in to change notification settings - Fork 0
/
getL4v.m
64 lines (55 loc) · 1.45 KB
/
getL4v.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
57
58
59
60
61
62
63
64
% Copyright 2020 The MathWorks, Inc.
function L4v = getL4v(nx,ny,dx,dy,mask)
% Note: Boundary condition effects x derivative only
% x-derivatives
% vb = (v0+v1)/2 => v0 = 2*vb - v1;
% Lv_1 = (v2 - 2*v1 + v0)/dx^2
% = (v2 - 2*v1 + 2*vb - u1)/dx^2
% = (v2 - 3*v1)/dx^2 + 2*vb/dx^2
%
% y-derivatives
% Lv_1 = (v2 - 2*v1 + vb)/dy^2
% = (v2 - 2*v1)/dy^2 + vb/dy^2
matrixSize = [nx,ny-1];
% 3
%152
% 4
coefx = 1/dx^2;
coefy = 1/dy^2;
i = zeros(nx*ny*5,1);
j = zeros(nx*ny*5,1);
v = zeros(nx*ny*5,1);
index1 = 1;
index = 1;
for jj=1:ny-1
for ii=1:nx
idx = [mask(ii+2,jj+1),mask(ii,jj+1),mask(ii+1,jj+2),mask(ii+1,jj),mask(ii+1,jj+1)];
stencils = [ii+1,jj
ii-1,jj
ii,jj+1
ii,jj-1
ii,jj];
stencils = stencils(idx,:);
tmpx = 2 + sum(~idx(1:2));
tmpy = 2;
coeffs = [coefx
coefx
coefy
coefy
-tmpx*coefx-tmpy*coefy];
coeffs = coeffs(idx);
linearIdx = sub2ind(matrixSize, stencils(:,1), stencils(:,2));
i(index1:index1+length(linearIdx)-1) = index;
j(index1:index1+length(linearIdx)-1) = linearIdx;
v(index1:index1+length(linearIdx)-1) = coeffs;
index1 = index1 + length(linearIdx);
index = index + 1;
end
end
i(i==0) = [];
j(j==0) = [];
v(v==0) = [];
L4v = sparse(i,j,v,nx*(ny-1),nx*(ny-1));
L4v(:,~any(L4v,1)) = [];
L4v(~any(L4v,2),:) = [];
end