Matlab Tutorial 5: Linear Equations
In this Matlab tutorial we will deal with linear equations, the least square method, condition numbers over & under-determined equations.
Let’s start with an example.
Example 1
We have an equation system with three unknown variables and three equations. What will be the solution to the system below?
3w-2y+4z=8
5w+8y-6z=-5
9w-2y+7z=-17
Assume a matrix A containing the coefficients multiplied with x, y and z, and a vector with the numbers on the right-hand side of the equations. We can thus rewrite our equations as:
AX=b , where X contains the unknown (w, y and z), A and b are shown below.
b= A= 8 3 -2 4 -5 5 8 -6 -17 9 -2 7 |
How will we find the solution?
>> X=inv(A)*b |
or
>> X=A\b |
both gives a correct answer. The last method produces a Gaussian elimination if A is a quadratic matrix. In our case X becomes:
X= -36.7778 71.2778 65.2222 |
This is no exact solution, only an approximation. Try also a specific command:
>> lsqr(A,b) |
The condition number of an equation can be examined. It means in short, that you can investigate the sensitivity of a linear equation system to disturbances in A or b. The condition number is always >1. The greater the sensitivity, the greater the number.
Calculate the sensitivity in our system.
>> cond(A) |
The condition number partly indicates what kind of trust one should put in a solution given from lsqr.
>> cond(A\b) or cond(lsqr(A,b)) |
As you have seen there are many different commands to use for solving equation systems. Wee will look at a few. They use the same argument as lsqr. Try the following: pcg, qmr, symmlq, minres . Some of them are successful and one or two will fail. try to figure out which one of these methods produces a correct answer?
Round-off errors can be magnified and we can lose accuracy. For a badly conditioned matrix A, A*A^-1 is not equal to the identity matrix. Return to the matrix A and change the element A(3,3). See below!
A = 3.0000 -2.0000 4.0000 5.0000 8.0000 -6.0000 9.0000 -2.0000 7.5600 |
What will the solution become and what happens to the condition number? The right-hand side is the same as previous. The equation system above has been applied to a quadratic matrix A (nxn) , where b is a column vector with n elements.