# Cannot solve system of linear equation sums?

I cannot get mathematica to solve:

The problematic code:

With[{n = 7},
Solve[((2*
Sum[((Subscript[y,
k] - (a*Subscript[x, k]^3 + b*Subscript[x, k]^2 +
c*Subscript[x, k] + d)))*(-Subscript[x, k]^3), {k, 1,
n}]) == 0) && ((2*
Sum[(Subscript[y,
k] - (a*Subscript[x, k]^3 + b*Subscript[x, k]^2 +
c*Subscript[x, k] + d))*(-Subscript[x, k]^2), {k, 1,
n}]) == 0) && ((2*
Sum[(Subscript[y,
k] - (a*Subscript[x, k]^3 + b*Subscript[x, k]^2 +
c*Subscript[x, k] + d))*(-Subscript[x, k]), {k, 1,
n}]) == 0) && ((2*
Sum[(Subscript[y,
k] - (a*Subscript[x, k]^3 + b*Subscript[x, k]^2 +
c*Subscript[x, k] + d))*(-1), {k, 1, n}]) == 0), {a, b,
c, d}]
]

I tried for a variable n, but that gave me 'could not solve with methods available to Solve', so then via a recommendation here I used n=6, but this still doesn't seem to work :/

If I put $x_{k}$ and $y_{k}$ instead as $x[[k]]$ and $y[[k]]$ and give a list however, then it is able to solve in under a second, as shown in this example:

x = RandomReal[{-10, 10}, 6];
y = RandomReal[{-10, 10}, 6];
With[{n = 6},
Solve[And @@
Table[Sum[(y[[k]] - (a x[[k]]^3 + b x[[k]]^2 + c x[[k]] +
d)) (-x[[k]]^j), {k, 1, n}] == 0, {j, 0, 3}], {a, b, c, d}]]

(*
==> {{a -> -0.0970828, b -> 0.771442, c -> 0.895965,
d -> -3.73369}}
*)

Here is a version of this code that doesn't work:

Clear[x, y]

With[{n = 6},
Solve[And @@
Table[Sum[(Subscript[y,
k] - (a Subscript[x, k]^3 + b Subscript[x, k]^2 +
c Subscript[x, k] + d)) (-Subscript[x, k]^j), {k, 1, n}] ==
0, {j, 0, 3}], {a, b, c, d}]]
-
Is the variable x a list of values? If yes then you should use x[[k]] instead of using a subscript. – Spawn1701D May 18 '13 at 16:33
I added my version of the code to the question to make it easier to reproduce. – Jens May 18 '13 at 21:41

I tried your example in version 8 and had no problems solving it. To save typing, I shortened it as follows:

With[{n = 6},
Solve[
And @@ Table[
Sum[(Subscript[y,
k] - (a Subscript[x, k]^3 + b Subscript[x, k]^2 +
c Subscript[x, k] + d)) (-Subscript[x, k]^j), {k, 1, n}] ==
0, {j, 0, 3}],
{a, b, c, d}
]
]

Then I confirmed that it works identically for this input where I eliminated all the subscripted variables:

With[{n = 6},
Solve[And @@
Table[Sum[(y[k] - (a x[k]^3 + b x[k]^2 + c x[k] + d)) (-x[k]^
j), {k, 1, n}] == 0, {j, 0, 3}], {a, b, c, d}]]

So the use of Subscript is perfectly fine here, and makes no difference in version 8.

However, in version 9 neither of the above produce a solution before I had to abort the calculation. So this is probably a bug, and it will take a little more thought to find a work-around.

One can write the equations explicitly as a linear system by calculating the matrix containing the sums of powers of $x_k$ (which can be summed independently of the unknown variables). I tried to do this with the original lengthy code in the question, and then used LinearSolve:

With[{n = 7},
equations = {
((2*Sum[((Subscript[y,
k] - (a*Subscript[x, k]^3 + b*Subscript[x, k]^2 +
c*Subscript[x, k] + d)))*(-Subscript[x, k]^3), {k, 1,
n}])), ((2*
Sum[(Subscript[y,
k] - (a*Subscript[x, k]^3 + b*Subscript[x, k]^2 +
c*Subscript[x, k] + d))*(-Subscript[x, k]^2), {k, 1,
n}])), ((2*
Sum[(Subscript[y,
k] - (a*Subscript[x, k]^3 + b*Subscript[x, k]^2 +
c*Subscript[x, k] + d))*(-Subscript[x, k]), {k, 1,
n}])), ((2*
Sum[(Subscript[y,
k] - (a*Subscript[x, k]^3 + b*Subscript[x, k]^2 +
c*Subscript[x, k] + d))*(-1), {k, 1, n}]))
}
];

Clear[a, b, c, d];
m = D[equations, {{a, b, c, d}}];
vec = -equations /. Thread[{a, b, c, d} -> {0, 0, 0, 0}];
LinearSolve[m, vec]

Here, I converted the system of equations to a matrix of linear coefficients (m) and a vector vec. Again, this hangs in version 9 but produces a solution in version 8. So the work-around for now seems to be: use version 8 (as I do by default, too).

-
any ideas for a workaround? – Eiyrioü von Kauyf May 19 '13 at 2:09
I just tried my LinearSolve idea and again can't get it to work in version 9 while it works in 8. See my edit. – Jens May 19 '13 at 3:11