## Matlab Tutorial 5: Linear Equations

What happens if we do not have the same number of equations as unknown variables? See first example. Assume that A is mxn matrix, where m>n. It means that we have more equations than unknowns. The system is over-determined meaning: probably it dose not exist an exact solution. This is a very realistic case in real life.

Suppose we have 58 measurements of current and voltage over a resistor, and we wish to determine the resistance using OhmÂ´s law. In theory these measurements would ideally lie on a straight line on a U-I plot, but in practice there are likely to be some deviations from the measurements to the straight line. We would like to find a quadratic deviation sum as small as possible. This would give us the best solution according to the least square method. In matlab this is achieved by:

```b= A= 8 3 -2 4 -5 5 8 -6 -17 9 -2 7```

Solve the following example where we have 3 equations, but only 2 unknowns.

x + y = 2
x + 2y = 0
x + 3y =-1

Our coefficient matrix is A and the measurement vector is b: AX=b

`>> X=inv(A)*b`

What do x and y become according to the least square method? If we have fewer equations than unknowns, than we have a under-determined system. It means that we have infinitely many solutions, but Matlab only presents one of these solutions without any warning of all the others. Solve the equation system below:

x + y + z = 2
x + 2y + 3z = 3

What solution is presented? In the Example 2 below we solve an over determined equation system without any knowledge about more advanced commands. Instead we rely on our mathematical background. First specify the nomal equations!

Example 2

We have the following output from an experiment: y=[0 1.1 1.93 3.3] and the corresponding input is x=[ 0 1 2 3]. Let us now look for a function that adapts as good as possible to the data set. Try to find a model that suits our data. First we make a plot of the data. See Figure 1a.

In all real-life measurements we always have the presence of disturbances and noise. These will of course have some impact on our measurement and give us some error. It will happen no matter how good we are in modeling. We will not find a perfect match between our model and the measured data.

Figure 1a

In Figure 1a it seems that a suitable function could be a straight line. y=a1x + a2 , where a1 and a2 must be determined. If we use our y- and x-values we get the following:

3.3 = a1 *3 + a2
1.93 = a1 *2 + a2
1.1 = a1 *1 + a2
0 = a1 *0 + a2

We now have 4 equations and 2 unknown. Now let us look for a best possible solution (least square) that fits our data, where the sum (a1xi + a2 – yi )2 should be as small as possible. Index i goes from i=0 to 3. We rewrite the 4 equations above to a matrix equation Y=X*a, where

`>> X=A\b`

and

a= a1 .
a2

We need to solve the vector a from the matrix equation, but in order to do so X must be inverted, which can be achieved only if X is quadratic. The matrix equation is written like:

XT*Y= XT*X*a
XT*X is a quadratic matrix, and we will try to invert it. If the inverse exists:
a= (XT*X)-1* XT*Y

the vector a that we get contains the values a1 and a2. This is the best solution according to the least square method.

Pages: 1 2 3 4 5