Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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.

share|improve this question
up vote 4 down vote accepted

I think this is what you are after:

 #.# &[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}}
share|improve this answer
That's exactly what I've been looking for! Dzieki, Kuba! – rythin May 13 '14 at 20:06

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:

   {0.99076, 2.00755}
share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.