3

I have 37 linear equations and 36 variables in the form of a matrix equation; A*X=B . The equations don't have an exact answer. I want to use Matlab least square method to find the answers with the least error. I am new to Matlab so any comments will help. Thank you

A. Donda
  • 8,381
  • 2
  • 20
  • 49
nasim
  • 725
  • 2
  • 8
  • 17

3 Answers3

9

If A is of full rank, i.e. the columns of A are linearly independent, the least-squares solution of an overdetermined system of linear equations

A * x = b

can be found by inverting the normal equations (see Linear Least Squares):

x = inv(A' * A) * A' * b

If A is not of full rank, A' * A is not invertible. Instead, one can use the pseudoinverse of A

x = pinv(A) * b

or Matlab's left-division operator

x = A \ b

Both give the same solution, but the left division is more computationally efficient.

The two latter computation methods can also deal with underdetermined systems of linear equations, but they give different solutions in that case: The pseudoinverse gives the solution where x has the smallest sum of squares, while the left-division operator gives a solution with as many 0 coefficients as possible.

A. Donda
  • 8,381
  • 2
  • 20
  • 49
5

The most general way to solve this is to use the pseudoinverse:

X = pinv(A) * B;
rlbond
  • 65,341
  • 56
  • 178
  • 228
  • How about if I want to apply a condition that x(i)>0 – nasim May 19 '15 at 20:12
  • 2
    You need the `lsqlin` function for that. I believe the syntax is `x = lsqlin(A,B,[],[],0,inf)`. – rlbond May 19 '15 at 20:15
  • 2
    This is numerically inferior to the using `mldivide` (backslash operator) in terms of computational complexity and numerical stability. – knedlsepp May 19 '15 at 20:15
  • 1
    `mldivide` and `pinv` are equivalent for an overdetermined problem like this question, but for an underdetermined problem `pinv` gives the "actual" least-squares solution (the least-energy solution). – rlbond May 19 '15 at 20:19
  • 2
    I agree with knedlsepp. Computing a matrix inverse (or pseudoinverse) is almost never a good idea if all you want to do is solve a linear system, both for efficiency and stability reasons. – cfh May 19 '15 at 20:33
3

You can calculate x by:

x = (A'*A)\A'*B

Tamás Szabó
  • 733
  • 6
  • 9
  • I am not sure if this is rigth – Ander Biguri May 19 '15 at 19:46
  • 1
    @AnderBiguri You're right. I've just corrected it - though your answer is more compact than mine. – Tamás Szabó May 19 '15 at 19:54
  • 1
    This expression is only correct when A has full column rank. `pinv` is more general. – rlbond May 19 '15 at 20:05
  • 2
    @TamásSzabó, if A is invertible, `(A'*A)*inv(A)` gives the same solution as `pinv` and the backslash. If A is not invertible, your equation using the backslash still works, but is unnecessarily complex. – A. Donda May 19 '15 at 20:06
  • 1
    @rlbond, it also works if A is not of full rank, because Tamás is using the backslash operator – A. Donda May 19 '15 at 20:06
  • How about if I want to apply a condition that x(i)>0 – nasim May 19 '15 at 20:11
  • 3
    @nasim you can't pose constraints on the solution when your problem is formulated as an equation. If your problem is an inequality, take a look at matlab's `linprog` function – Tamás Szabó May 19 '15 at 20:24
  • 1
    @TamásSzabó, I have to correct myself, again. I often get confused by which way the backslash operator goes. – A. Donda May 19 '15 at 20:31
  • 3
    @A.Donda - Just remember where the slash is learning towards. Wherever it's leaning, that's where the inverse is being taken.... so `A\b` with the ` \ ` leaning towards the `A` means `inv(A)*b` (assuming full rank) and doing `A/b` means `A*inv(b)` (also assuming full rank). I get confused all the time and I finally nailed it by just looking at the direction of where the slash is leaning. – rayryeng May 19 '15 at 21:37