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.


2 Answers 2


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}}
  • $\begingroup$ That's exactly what I've been looking for! Dzieki, Kuba! $\endgroup$
    – rythin
    May 13, 2014 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}
  • 2
    $\begingroup$ 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. $\endgroup$ Jun 18, 2014 at 13:14
  • $\begingroup$ @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. $\endgroup$
    – Peltio
    Jun 18, 2014 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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