The numerical solution of differential equations often results in what is known as a ‘sparse linear’ system. A matrix is ‘sparse’ if most of its entries are zero, and most of the MATLAB functions you have used so far have versions that are specially optimised for sparse matrices, which can speed up your code immensely. The details of this speeding-up will be discussed further in the later unit ‘Software engineering’, but we introduce sparse matrices here as they will be used in exercises later in this unit.
The following will give you an introduction to the uses of sparse matrices.
Create the matrix A
using the following commands:
A=2*eye(5000)-diag(ones(4999,1),1)-diag(ones(4999,1),-1);
Run the following commands to investigate the structure of A
:
A(1:5,1:5)
nnz(A)
spy(A)
spy(A(1:10,1:10))
What is the structure of A
?
A
is ‘tri-diagonal’, with 2 on the diagonal and −1 on the adjacent diagonals.
Therefore, A
has $5000+(2\times 4999)=14998$
non-zero entries, as found using nnz(A)
.
Calculate the inverse of A
.
The inverse of A
is not sparse.
This is true in general.
Ai=inv(A);
spy(abs(Ai)>1)
spy(abs(Ai)>5)
spy(abs(Ai)>10)
spy(abs(Ai)>100)
Calculate how long it takes to calculate $A^2$
.
You can use the commands tic
and toc
to record elapsed time in MATLAB.
tic; A*A; toc
Create the matrix B
using the following commands.
Note that A
and B
have the same non-zero entries:
B=spdiags(ones(5000,1)*[-1,2,-1],[-1,0,1],5000,5000);
The command spdiags
creates a sparse matrix with the entries from the vector (passed as the first variable) on the diagonal indicated by the second variable. These variables can be passed individually or in groups as in the above example. The final two variables represent the size of the matrix. Look at the MATLAB help files for more information on spdiags
.
Look at the structure of B
:
B(1:5,1:5)
full(B(1:5,1:5))
nnz(B)
Using the command whos
, look at how the matrices A
and B
are stored.
Which uses less memory?
>> whos A
Name Size Bytes Class Attributes
A 5000x5000 200000000 double
>> whos B
Name Size Bytes Class Attributes
B 5000x5000 279976 double sparse
So A
used 2000000 bytes, and B
only used 27976 bytes, saving about 98.6% of the space.
Calculate how long it takes to calculate $B^2$
:
tic; B*B; toc
Which is quicker, $A^2$
or $B^2$
?
>> tic; A*A; toc
Elapsed time is 0.768850 seconds.
>> tic; B*B; toc
Elapsed time is 0.013921 seconds.
Calculating $B^2$
should be faster, and the proportion that it is faster should increase with the size of the matrix.
Now compare the \
operator
b=ones(5000,1);
tic; x1=A\b; toc
tic; x2=B\b; toc
Which is quicker, A\b
or B\b
?
>> b=ones(5000,1);
tic; x1=A\b; toc
tic; x2=B\b; toc
Elapsed time is 0.268863 seconds.
Elapsed time is 0.000719 seconds.
Calculating B\b
should be faster, and the proportion that it is faster should increase with the size of the matrix.
Finally, you can also convert matrices from full to sparse using the following command
A=sparse(A);
Using the command whos
, check that A
is now sparse and uses the same memory as B
.
Now clear the workspace
clear