Solve overdetermined set using Mathematica?

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}

• Look up LeastSquares[]. Make sure you know how to formulate this in matrix-vector format. Commented Aug 12, 2015 at 9:26
– Fang
Commented Aug 12, 2015 at 15:17
• alternatively NMinimize[Plus @@ (sys /. Equal[a_, b_] -> (a - b)^2), {E0, x, y, a, b, c}] Commented Aug 12, 2015 at 15:19
• @chris Yes, thanks!
– Fang
Commented Aug 12, 2015 at 15:33

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}}]]


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]

• …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. Commented Aug 12, 2015 at 13:54
• @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...
– kale
Commented Aug 12, 2015 at 13:58
• 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.
– Fang
Commented Aug 12, 2015 at 15:30