Matrix form of equations
Systems of linear equations
Linear algebra is largely concerned with representing, manipulating and solving large systems of linear equations. Consider the following 2 linear equations:
where the values and are to be found, and and are given constants. We know that we can use linear combinations of these two equations to solve this sytem for and , like so:
The Matrix
We can also write these linear equations using a matrix. Matrices are structures that allow us to more easily manipulate linear systems. While not particularly useful for just 2 equations, using a matrix representation allows us to generalise to, say equations and unknowns, or to solve large systems of equations on a computer.
Consider the original system:
We rewrite this, in the form of a matrix as:
Think about how this form relates to the original linear system.
Geometry of linear equations
Consider the following system of equations
That is,
Plotting these two linear equations on a graph shows graphically the solution to this equation given by
Now lets consider two different system of equations represented by the matrix:
and
The first gives the plot on the left, while the second, which has a different vector of constants on the RHS, gives the plot on the right. You can see that depending on the constants, the system of equations can have an infinite number of solutions, or no solutions at all.
The matrix in this case is singular, and therefore does not have an inverse. Looking at the equations again, you can see that the two rows of the matrix are multiples of the other, and thus there is only one independent row. That is, the rank of the matrix is one.
Singular matrices
The rank of an matrix is the number of linearly independent rows in (rows not combinations of other rows).
When then
The matrix is said to be 'rank deficient'
The system has fewer equations than unknowns
The matrix is said to be singular
The matrix is said to be underdetermined
has no inverse
The determinant of is 0
The equation has non-trivial solutions ()
The determinant
One way of solving a system of equations represented by is to calculate the inverse of A, giving the solution as . This can be done by calculating what is known as the determinant of .
If
then the determinant of A is:
The inverse of can be found using the determinant:
Calculating the inverse of a matrix using its determinant can be very costly for larger matrices, therefore other algorithms are used (e.g. Gaussian Elimination, which is introduced in the next section)
If , A is said to be singular (have no inverse). Graphically, this is represented by the parallel or non-intersecting lines in the figure above.
Using Python to calculate the inverse
To find for
you can using numpy like so:
import numpy as np A = np.array([[3, 0, 2], [3, 0, -3], [0, 1, 1]]) np.linalg.inv(A)
Output:
array([[ 0.2 , 0.13333333, 0. ], [-0.2 , 0.2 , 1. ], [ 0.2 , -0.2 , -0. ]])
It doesn't always work. Consider the following system
A = np.array([[1, 1, 1], [2, 5, 2], [7, 10, 7]]) np.linalg.inv(A)
Output:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<__array_function__ internals>", line 6, in inv File "/home/mrobins/git/scientific-computing/env/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 546, in inv ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj) File "/home/mrobins/git/scientific-computing/env/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 88, in _raise_linalgerror_singular raise LinAlgError("Singular matrix") numpy.linalg.LinAlgError: Singular matrix
Other Reading
Linear algebra by Ward Cheney
Linear algebra and its applications by David C. Lay.
Strang, G. (2016). Introduction to linear algebra (Fifth ed.). Wellesley.
Linear algebra and its applications by Gilbert Strang
lots of supplimentary material via MIT course page here: https://github.com/mitmath/1806/blob/master/summaries.md
LA from an ODE perspective: Kapitula, T. (2015). Ordinary Differential Equations and Linear Algebra. Society for Industrial and Applied Mathematics.
Problems
Intersection of planes
Describe the intersection of the three planes , and (all in four-dimensional space). Is it a line or a point or a fourth equation that leaves us with no solution. an empty set? What is the intersection if the fourth plane is included? Find a fourth equation that leaves us with no solution.
Python: Intersection of planes
Sketch or plot in Python these three lines and decide if the equations are solvable: 3 by 2 system , , and . What happens if all right-hand sides are zero? Is there any nonzero choice of right- hand sides that allows the three lines to intersect at the same point?
Upper triangular matrix
Write a Python function that takes in a upper triangular matrix
represented as an ndarray
, and a rhs vector , and solves the equation .
i.e. the function will solve the following triangular system for :
Generalise this function to a triangular matrix input.