3
$\begingroup$

I cannot get mathematica to solve:

enter image description here

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}]]
$\endgroup$
2
  • $\begingroup$ Is the variable x a list of values? If yes then you should use x[[k]] instead of using a subscript. $\endgroup$
    – Spawn1701D
    Commented May 18, 2013 at 16:33
  • $\begingroup$ I added my version of the code to the question to make it easier to reproduce. $\endgroup$
    – Jens
    Commented May 18, 2013 at 21:41

1 Answer 1

5
$\begingroup$

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

output

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).

$\endgroup$
2
  • $\begingroup$ any ideas for a workaround? $\endgroup$ Commented May 19, 2013 at 2:09
  • $\begingroup$ 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. $\endgroup$
    – Jens
    Commented May 19, 2013 at 3:11

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.