Linear Systems
In MATLAB you can solve a linear system A x = b using the backslash operator \. However you need to be aware that the MATLAB's backslash does much, much more.
Square, nonsingular systems
If A is an n by n non-singular matrix (that is the determinant of A is non-zero) then the system of linear equations A x = b has a unique solution x = A-1 b in Rn for any right-hand-side vector b in Rn.
A non-singular linear system can be efficiently solved using the backslash \ operator. This uses Gaussian elimination, based on reducing the matrix to row-echelon form using row operations, as in first year mathematics. However all these details are hidden from you.
In general this is far more efficient than explicitly calculating the inverse A-1.
Create the matrix A.
Check A is non-singular using the det function.
Define the right-hand-side column vector of all 1s.
Solve the linear system using Gaussian elimination.
Solve the linear system by explicitly calculating the inverse using the inv function.
Inconsistent linear systems
If the coefficient matrix A is singular, that is det(A) = 0, then you will get a warning message. However MATLAB will still calculate a "solution" which will probably involve Inf or NaN.
Create the matrix A. The vector b was defined in the previous example.
Solving the linear system using \ gives
Warning: Matrix is singular to working precision.
and x = [-Inf; Inf]
Checking the determinant show that A is singular.
Non-square linear systems
When the coefficient matrix A is not square, for example if it has more rows (equations) than columns (variables) then MATLAB calculates the least squares solution.
If you have not seen least squares solutions (yet) then skip the rest of this section, but remember that MATLAB may calculate it, even if you did not (explicitly) ask it to!
When A is not square, it is a good idea to calculate the residual vector r = b - Ax. Then x is a solution to Ax = b if and only if r = 0 (the zero vector).
Create the matrix A by columns, transposing the row vector 1:3 to get a column vector.
Create the right-had-side column vector b.
Solving the linear system using \ gives
x = [1; 2] and no warnings or error messages.
The residual r is not zero, so x does not satisfy Ax = b.
Warnings
- Use help mldivide (matrix left division) to get more information on the backslash \ operator. However this may be very confusing at this stage.
- MATLAB does a lot of analysis to see if the coefficient matrix A has any special structure that can be exploited to solve the system more efficiently.
Self-test Exercise
Create the column vector t with elements 0, 1, 2. Create the matrix A with elements Aij = (ti)j-1, i,j = 1,2,3, and column vector y with elements 3, 2, 3. Solve the linear system Ax = y. Calculate the determinant to check A is non-singular, and the residual r = y - Ax to check x does solve Ax = y.This finds the quadratic passing through (ti, yi) for i = 1,2,3.
Answer:
t = [0:2]'
A = [ones(size(t)) t t.^2]
y = [3; 2; 3]
x = A \ y
det(A)
r = y - A*x
Use the mouse to select the text between the word "Answer" and here to see the answer.
Summary
A linear system Ax = b with a square non-singular coefficient matrix A can be efficiently solved using x = A \ b.
If the the coefficient matrix A is not square then this gives the "least squares solution" which does not necessarily satisfy Ax = b.