Re: RE: Solving for Sum element
- To: mathgroup at smc.vnet.net
- Subject: [mg27292] [mg27292] Re: [mg27270] RE: [mg27260] Solving for Sum element
- From: "Mark Harder" <harderm at ucs.orst.edu>
- Date: Sun, 18 Feb 2001 02:52:26 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
David Park seems to have solved this problem, but to extend my understanding of how Mathematica does things, I worked out a different solution: solveElement[eqn_, k_] := ( sum1 = ReplacePart[eqn, k - 1, {1, 2, 3} ][[1]]; sum2 = ReplacePart[eqn, k + 1, {1, 2, 2} ][[1]]; Rule[Part[ReplacePart[eqn, k, {1, 1, 1} ], 1, 2], Part[eqn, 2] - (sum1 + sum2) ] ); This function *assumes* that eqn is of the form Sum[symbol[i], iterator]== r.h.s, and k is the index of the term to be solved for (it can be symbolic or numeric ). It breaks the equation expression down by parts, replaces the parts as needed, and returns the rule. All it does is manipulate the eqn expression passed to it and returns the manipulated expression -- no other assumptions about eqn are necessary, as far as i know so far. Doing it this way doesn't require any Hold[], but it is a little rigid about the form of eqn. Some results: (outputs are printed in input form) Symbolic sum, symbolic index: In[409]:= solveElement[eqn, k] Out[409]= a[k] -> x - Sum[a[i], {i, 1, k - 1}] - Sum[a[i], {i, k + 1, N}] Symbolic sum, numeric index: In[410]:= solveElement[eqn, 5] Out[410]= a[5] -> x - a[1] - a[2] - a[3] - a[4] - Sum[a[i], {i, 6, N}] One awkwardness is that you have to be clever about applying further rules in order to solve a sum with defined terms: In[427]:= eqn2 = Sum[a[i] , {i, 1, N} ] == 25. Out[427]= Sum[a[i], {i, 1, N}] == 25. In[447]:= solveElement[eqn2, 4] /. {N -> 6, a[4] -> x, a[i_] -> E^-i} Out[447]= x -> 24.4378 I'm sure there are simpler ways to do take this approach as well -- like just passing solveElement the r.h.s. of the equation, and setting Sum[a[i],{i,initial,final} ] to it inside the function. -mark harder -----Original Message----- From: David Park <djmp at earthlink.net> To: mathgroup at smc.vnet.net Subject: [mg27292] [mg27292] [mg27270] RE: [mg27260] Solving for Sum element >Ben, > >Here is one approach: > >Attributes[SumTermSolve] = {HoldFirst}; >SumTermSolve[sumequation_, index_] := > Module[{seqn = Hold[sumequation], i, t, start, end}, > seqn = ReleaseHold[seqn /. Sum -> sum]; > seqn = seqn /. sum[t_, {i_, start_, end_}] -> > sum[t, {i, start, index - 1}] + term[index] + > sum[t, {i, index + 1, end}]; > Solve[seqn, term[index]] /. sum -> Sum] > >f[i_] := i > >SumTermSolve[Sum[f[i], {i, 1, n}] == 0, 3] >{{term[3] -> -3 - 1/2*(-3 + n)*(4 + n)}} > >SumTermSolve[Sum[a[i], {i, 0, 6}] == x, 2] >{{term[2] -> x - a[0] - a[1] - a[3] - a[4] - a[5] - > a[6]}} > > >David Park >djmp at earthlink.net >http://home.earthlink.net/~djmp/ > > > >> From: Ben Jacobson [mailto:bjacobson at illumitech.com] To: mathgroup at smc.vnet.net >To: mathgroup at smc.vnet.net >> >> I want to solve for one element in an equation involving long sums. The >> actual equation is of course complicated, but a very simplified example >> would be >> >> Sum[a[i],{i,0,n}]==x >> >> where I want to solve for a[0]. The solution should be something like >> a[0]->x-Sum[a[i],{i,1,n}. >> >> Solve[Sum[a[i], {i, 0, n}] == x, a[0]] >> >> works fine if, for example, n=3, but it returns an empty result for >> symbolic n. Is there a way to tell Solve to assume that n>1 and to solve >> accordingly? The best I've been able to do so far is to explicitly remove >> the element I'm interested in from the sum: >> >> In: Solve[a[0] + Sum[a[i], {i, 1, n}] == x, a[0]] >> Out: {{a[0] -> x - Sum[a[i], {i, 1, n}]}} >> >> This works nicely, but becomes awkward when I try to solve for more >> elements and longer sums. Thanks. >> >> Ben Jacobson >> Illumitech Inc. >> > >