-
Notifications
You must be signed in to change notification settings - Fork 2
/
MatrixOfCoeff.m
55 lines (47 loc) · 1.03 KB
/
MatrixOfCoeff.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
%Number of unknowns, M points in (x) and N points in (y)
clc
clear
M = 5;
N = 4;
%Size of the Matrix
size = (N-1)*(M-2);
%Coefficients of the matrix
c = 1;
dt = 1;
dx = 3;
A = 2*c*dt;
B = -4*c*dt;
C = dx^2;
D = -dx^2;
%Intial Zero square matrix with size (size x size)
Matrix = zeros(size);
%Main for loop
for i = 1:size
%Coefficients of (i,j)
Matrix(i,i) = B;
if i+(N-1) <= size
%Coefficients of (i+1,j)
Matrix(i,i+(N-1)) = A;
end
if i-(N-1) >= 1
%Coefficients of (i-1,j)
Matrix(i,i-(N-1)) = A;
end
if i >= 2
%Coefficients of (i,j-1)
if mod(i-1,(N-1)) == 0
Matrix(i,i-1) = 0;
else
Matrix(i,i-1) = C;
end
end
if i<= size -1
%Coefficients of (i,j+1)
if mod(i,(N-1)) == 0
Matrix(i,i+1) = 0;
else
Matrix(i,i+1) = D;
end
end
end
Matrix