3
$\begingroup$

As shown below, this is a overdetermined system. Could you teach me how to find the optimized solution in Mathematica? I know it could be solved by the method of least square, but how to realize it in Mathematica?

{E0 - 2 x + 2 y + 2 a - 2 c == 0.,
 E0 - 2 x + 2 y - 2 a + 2 c == 0.2, 
 E0 - 2 x - 2 y - 2 a - 4 b - 2 c == 0.032, 
 E0 + 2 x + 2 y - 2 a - 4 b - 2 c == 0.143, 
 E0 + 2 x - 2 y - 2 a + 2 c == 0.436, 
 E0 + 2 x - 2 y + 2 a - 2 c == 0.222, 
 E0 - 2 x - 2 y + 2 a + 4 b + 2 c == 0.275, 
 E0 + 2 x + 2 y + 2 a + 4 b + 2 c == 0.416}
$\endgroup$
4
  • 5
    $\begingroup$ Look up LeastSquares[]. Make sure you know how to formulate this in matrix-vector format. $\endgroup$ Commented Aug 12, 2015 at 9:26
  • $\begingroup$ Hi, @J.M. Thank you for your answer. $\endgroup$
    – Fang
    Commented Aug 12, 2015 at 15:17
  • 1
    $\begingroup$ alternatively NMinimize[Plus @@ (sys /. Equal[a_, b_] -> (a - b)^2), {E0, x, y, a, b, c}] $\endgroup$
    – chris
    Commented Aug 12, 2015 at 15:19
  • $\begingroup$ @chris Yes, thanks! $\endgroup$
    – Fang
    Commented Aug 12, 2015 at 15:33

1 Answer 1

6
$\begingroup$

As @Guess who it is. states in the comments, an overdetermined linear problem can be solved using Mathematica's LeastSquares[] functionality.

To input your above system of equations:

a = {{1, -2, 2, 2, 0, -2}, {1, -2, 2, -2, 0, 2}, 
    {1, -2, -2, -2, -4, -2}, {1, 2, 2, -2, -4, -2}, 
    {1, 2, -2, -2, 0, 2}, {1, 2, -2, 2, 0, -2}, 
    {1, -2, -2, 2, 4, 2}, {1, 2, 2, 2, 4, 2}};

b = {0, .2, 0.032, 0.142, 0.436, 0.222, 0.275, 0.416};

Now to solve:

sol = LeastSquares[a, b]

{0.215375, 0.0443125, -0.0129375, -0.0151042, 0.0215417, 0.0366458}

And to check how good the fit is:

ListPlot[Thread[{b, a.sol}], AxesLabel -> {"Actual", "Predicted"}, Epilog -> Line[{{0, 0}, {1, 1}}]]

enter image description here

Looks good. To explore further, we can use LinearModelFit[] to give us more detail about our model.

lm = LinearModelFit[{a, b}];

lm[{"FitResiduals", "RSquared"}]

{{0.002625, -0.004375, 0.008625, -0.006875, 0.002625, -0.004375, -0.006875, 0.008625}, 0.998342}

Also, you can use CoefficientArrays[] to build your coefficient matrix and vector.

{b0, a0} = Normal@CoefficientArrays[{e0 - 2 x + 2 y + 2 a - 2 c == 0, etc...}, {e0, x, y, a, b, c}]

And then:

LeastSquares[a0, -b0]
$\endgroup$
3
  • 4
    $\begingroup$ …and of course, one can use CoefficientArrays[] to form the matrix and vector from the equations. Still, for problems like these, it's best to skip the equations altogether and just input the matrix directly. $\endgroup$ Commented Aug 12, 2015 at 13:54
  • $\begingroup$ @J.M. Exactly. This one took exactly 20 seconds of keying to get the coefficient matrix input. Probably should put something in the answer about CoefficientArrays[] though... $\endgroup$
    – kale
    Commented Aug 12, 2015 at 13:58
  • $\begingroup$ Dear @kale and J.M. Thanks for your helpful tutorial and discussion for this kind of overdetermined system. With your help, I have reproduced the results which are in good agreement with others' reports. Great thanks again. $\endgroup$
    – Fang
    Commented Aug 12, 2015 at 15:30

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.