]> gitweb.michael.orlitzky.com - octave.git/blob - diffusion_matrix_sparse.m
Add diffusion_matrix_sparse() and its tests.
[octave.git] / diffusion_matrix_sparse.m
1 function K = diffusion_matrix_sparse(integerN)
2 ##
3 ## A sparse representation of the matrix K in the advection-diffusion
4 ## equation. See advection_matrix.m for details.
5 ##
6
7 if (integerN < 2)
8 K = NA;
9 return
10 end
11
12 ## The negative ones directly above the diagonal.
13 top = [ [zeros(integerN-1, 1), -speye(integerN-1)]; ...
14 zeros(1, integerN)];
15
16 ## The negative ones directly below the diagonal.
17 bottom = [ [zeros(1, integerN-1); ...
18 -speye(integerN-1) ], zeros(integerN, 1)];
19
20 ## Combine the top and bottom.
21 K = top + bottom + 2*speye(integerN);
22
23 ## Fill in the entries in the corner.
24 K(1, integerN) = -1;
25 K(integerN, 1) = -1;
26 end