# How do I solve for a variable inside a sum but independent of the summation index?

How would I go about solving the following for c?

Solve[0 == Sum[(t[i]*m[i] - c*t[i]^2)/s[i]^2, {i, 1, n}], c, Reals]


I get the error

Solve::nsmet : This system cannot be solved with the methods available to Solve

but it is fairly straight forward to get that

c = Sum[m[i]*t[i]/s[i]^2, {i, 1, n}] / Sum[t[i]^2/s[i]^2, {i, 1, n}]


I need to repeat this for a similar equation that is not quite so straight forward to do on paper. I intended to use Mathematica to check my result but since I cannot verify the one I am confident in, I may be out of luck. I am new to Mathematica so I would not be surprised if the problem is my ignorance.

Edit: A friend pointed out that N was a defined function in Mathematica (also mentioned in a comment below). I replaced that with n with the same overall outcome.

• I think you will fairly consistently find that Mathematica is not particularly helpful with "abstract" things, like sums where you don't give it integer upper and lower bounds or vectors and matricies where you don't give it specific integer dimensions or even functions where you don't actually define what the functions are.
– Bill
Oct 14 '15 at 0:23
• For simple problems such as yours, you might try Summa.m, you need to patch it for use with the latest versions of mma, as shown here mathematica.stackexchange.com/questions/31426/… . Please note, you won't be able to use solve directly, but you might find the simplification and BringOut functions useful. (Usage and help are available on the Mathsource). Oct 14 '15 at 1:41
• The fact that the summation limit and the coefficients aren't numeric makes Mathematica give up, especially since you're asking for a solution over the Reals (which can't be done for the generic coefficients). But if I choose a numeric upper limit for the sum, and remove Reals, it works: Table[c/.First@Solve[0==Sum[(t[i]*m[i]-c*t[i]^2)/s[i]^2,{i,1,n}],c],{n,1,4}]. So you can at least check your manual calculations by inserting specific n. And: never use capitals like N as variable names. Type ?N.
– Jens
Oct 14 '15 at 2:38

If the variable to be solved for appears in the Sum in polynomial form, then the following works.

solvsum[s_, z_] := Module[{coef, in = s[[2]], cf = CoefficientList[s[[1]], z]},
Solve[0 == Sum[coef[i] z^(i - 1), {i, Length[cf]}], z] /.
Table[coef[j] -> Sum[cf[[j]], Evaluate[in]], {j, Length[cf]}]]


For the Sum in the question,

s1 = Sum[(t[i]*m[i] - c*t[i]^2)/s[i]^2, {i, 1, n}];
solvsum[s1, c]
(* {{c -> -(Sum[(m[i]*t[i])/s[i]^2, {i, 1, n}]/Sum[-(t[i]^2/s[i]^2), {i, 1, n}])}} *)

• This works great for the example case. Unfortunately, the more difficult sums do not strictly have the variable in polynomial form. I am fairly confident there is no analytic solution though. Oct 15 '15 at 0:53
• @stvn66 Thanks for accepting the answer. It probably can be generalized to solve with a Sum many expressions that Solve can handle without a Sum. Best wishes. Oct 15 '15 at 1:32

If your goal is to check the work you do on paper you may want to go this route :

First define

c[n_Integer] :=
Sum[m[i]*t[i]/s[i]^2, {i, 1, n}]/Sum[t[i]^2/s[i]^2, {i, 1, n}];

ShouldBeZero[n_Integer] :=
Sum[(t[i]*m[i] - c[n]*t[i]^2)/s[i]^2, {i, 1, n}] // FullSimplify


then check a few values :

Table[ShouldBeZero[n], {n, 1, 20}]
(* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} *)