Solving Linear Systems
The LinearAlgebra package not only gives you tools to solve linear systems directly, but also allows you to examine each step of the process.
By default, Maple uses hardware floats, but in this worksheet, we will use software floats. (For information about hardware floats and using them effectively, see the LinearAlgebra Functions with NAG Support example worksheet.)
Solving with a Single Command - LU Method
The following Matrix and Vector represent a linear system M.x=V which we wish to solve for x:
To solve the linear system, you can use the LinearSolve command. Note that you can use separate Matrix and right-hand side Vector arguments, or simply use an augmented Matrix. To ensure that the method used is LU decomposition (the step-by-step method demonstrated in the next section), specify method='LU'.
Compute the residual to see how accurate the solution is:
Solving the Linear System One Step at a Time - LU Method
You can solve a linear system one step at time by using the LU Method.
As in the previous section, the following Matrix and Vector represent a linear system M.x=V where we want to solve for x:
First, do an LU decomposition of M. The output components are P (the permutation Matrix), L (a unit lower triangular Matrix), and U (an upper triangular Matrix).
Since this is a decomposition of M, P.L.U=M. The inverse of a permutation Matrix is always its transpose. To solve the system, M.x=V, start by multiplying both sides by the inverse (transpose) of P.
We now have L.U.x = Transpose(P).V, or L.U.x=V2 for short.
Since L is lower triangular, use ForwardSubstitute to find a Vector V3 such that L.V3 = V2:
Since L.V3=V2 and L.U.x=V2, we need to find a solution vector, x such U.x=V3. Since U is upper triangular, use BackwardSubstitute to do this:
Solving with a Single Command - QR Method
The following Matrix and Vector represent a linear system M.x=V where we want to solve for x:
To solve the linear system, you can use the LinearSolve command. To ensure that the method used is QR decomposition (the step-by-step method demonstrated in the next section), specify method='QR'.
Solving the Linear System One Step at a Time - QR Method
The QR method can be used to solve the system one step at a time.
First, do a QR decomposition of M. The output components are Q (an orthogonal Matrix) and R (an upper triangular Matrix).
Since this is a decomposition of M, Q.R=M. The inverse of an orthogonal Matrix is always its transpose (its Hermitian transpose if complex values are involved). To solve the system, M.x=V, start by multiplying both sides by the inverse (transpose) of Q.
We now have R.x = Transpose(Q).V, or R.x=V2 for short.
Since R is upper triangular use BackwardSubstitute to solve for x:
The Cholesky method uses a special form of LU decomposition. It works only for symmetric (Hermitian in the case of complex entries), positive definite Matrices. When you have a Matrix of this form, the Cholesky method is faster than the LU method because it does not involve a permutation Matrix, and it uses less storage because the upper triangular Matrix is the transpose (Hermitian transpose in the case of complex entries) of the lower triangular Matrix, and thus only one of them needs to be stored.
To try this method, you will need a symmetric, positive definite Matrix. One way to create such a Matrix is to create a lower triangular Matrix, then multiply it by its transpose. Here is an example, followed by verification that it is indeed symmetric and positive definite:
You will also need a right-hand side vector:
Solving with a Single Command
The system can be solved with a single call to LinearSolve. Since the Matrix has the properties required by the Cholesky method, that method will automatically be used. An explicit request for this method can also be made.
To solve the system one step at a time, start with a Cholesky decomposition:
Use ForwardSubstitute to solve L.V2 = V:
Use BackwardSubstitute to solve Transpose(L).x=V2:
Solving Triangular Systems
Triangular systems can be solved in one step. To solve lower triangular systems, use ForwardSubstitute. To solve upper triangular systems, use BackwardSubstitute. Cannot remember which "Substitute" command to use? As long as the shape of your Matrix is triangular[upper] or triangular[lower], LinearSolve will figure out which of ForwardSubstitute and BackSubstitute to use.
If your system is triangular, but stored with a rectangular shape, specify method=subs, and LinearSolve will use ForwardSubstitute or BackwardSubstitute (as appropriate) to solve the system:
Return to Index for Example Worksheets
Download Help Document