2

I would like to find the best fit of two variables for a number of approximate equations, such as:

1x+2y+2=7.04
2x+y+4=7.98
1.5x+1.5y-2=2.53
1.8x+3y-1=6.77

That should come up with a result: x = ~1, y = ~2 (exact value for whatever the best fit is).

I can't find any inspiration in either

I don't expect anyone to do it for me, but perhaps someone could push me in a correct direction?

Thanks in advance, r.

Kuba
  • 136,707
  • 13
  • 279
  • 740
rythin
  • 185
  • 5

2 Answers2

4

I think this is what you are after:

NMinimize[
 #.# &[Subtract @@@ {
    1 x + 2 y + 2 == 7.04,
    2 x + y + 4 == 7.98,
    1.5 x + 1.5 y - 2 == 2.53,
    1.8 x + 3 y - 1 == 6.77
    }],
 {x, y}]
{0.00360363, {x -> 0.99076, y -> 2.00755}}
Kuba
  • 136,707
  • 13
  • 279
  • 740
2

I tend to avoid black boxed solutions, whenever it's possible. Here's how to build the least square solver from scratch:

LeastSquareSolve[A_, Ym_] := Inverse[Transpose[A].A].Transpose[A].Ym

And to use it you just have to recast your equations in matrix form (a one-liner can be built for that, should you need it - there's one on Thomas Bahder's "Mathematica for Scientists and Engineers" wonderful book)

A = {{1, 2}, {2, 1}, {1.5, 1.5}, {1.8, 3}}
Ym = {{7.04 - 2}, {7.98 - 4}, {2.53 + 2}, {6.77 + 1}}

Now, its calling time:

LeastSquareSolve[A, Ym]
    {0.99076, 2.00755}

BTW, there's even a builtin function that does just that: PseudoInverse, so that your solution can be found - once you have entered your A and Ym matrices, as:

PseudoInverse[A].Ym
   {0.99076, 2.00755}
Peltio
  • 5,516
  • 3
  • 27
  • 27
  • 2
    Better to use the built-in function LeastSquares than to explicitly form the pseudoinverse. Using the SVD method (which is what PseudoInverse does) is a lot better than finding it with an explicit Inverse as in your first example, but it can still be numerically unstable. – Oleksandr R. Jun 18 '14 at 13:14
  • @OleksandrR., good point. I was not even aware there was a LeastSquare built-in function (MMA is growing larger and larger, the next manual will be a dictionary). The problem with what I call black-boxed approach is that one is tempted to overlook this sort of pitfalls, trusting the system to do the thinking. I did it with the black-box Inverse and PseudoInverseit seems :-)... But I guess that even LeastSquares and NMinimize are hiding pitfalls. – Peltio Jun 18 '14 at 13:27