2
$\begingroup$

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.

$\endgroup$

2 Answers 2

4
$\begingroup$

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}}
$\endgroup$
1
  • $\begingroup$ That's exactly what I've been looking for! Dzieki, Kuba! $\endgroup$
    – rythin
    May 13, 2014 at 20:06
2
$\begingroup$

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}
$\endgroup$
2
  • 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 that you have read and understand our privacy policy and code of conduct.

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