I am trying to do a For iteration where at each step, the code solve an equation and use the result for the next step. Nevertheless, I am struggling with the replacement rules that I need to use.

I worked out the three first steps by hand. I edited the question to do it an only one parameter problem. Here resulttab is the table in which I want to keep my results, while C[k] are the coefficients I am searching for. I am going to iterate on k. My function Funct is a polynomial in C[k]. This is not the real form since it involve a complete evaluation of another function into it but for the purpose of my question, I think that the following definition of funct is suitable.

funct[n_] := Sum[If[i == 0, c[0], i*c[i]], {i, 0, n}]
In[231]:= funct[3]
Out[231]= c[0] + c[1] + 2 c[2] + 3 c[3]

c0[k_] := Sum[n1^i, {i, 0, k}]

Now, at each step in n, I will compare the result with a symbolic expression that I have, called C0[1,1,0]. Those results are polynomials in $n_1$, where $n_1$ is constant. Therefore, comparing funct[1] with the first result will allow me to fix c[1]. Then funct[2] will depend on both c[1] and c[2] but as I know c[1], I will be able to fix directly c[2] etc... . So the first step of the iteration is

resulttab[[1]] = c[0] /. (Solve[funct[0] == c0[0], c[0]][[1]])

This fixes

resulttab[[1]] = 1

resulttab and c have different indices since I have a symbolic c[0] while resulttab indexation starts at 1. The second step in the process is then

 resulttab[[2]] = c[1] /. (Solve[(funct[1] /. c[0] -> resulttab[[1]]) == c0[ 1], 

. The third step would then be

resulttab[[3]] = c[2] /. (Solve[(funct[2] /. {c[1] -> resulttab[[2]], 
     c[0] -> resulttab[[1]]}) == c0[2], c[2]][[1]])

Now the problem I have is the following. At each step I am going to need an extra replacement rule of the form c[j]->resulttab[[j+1]] in order to automatize the resolution. In the end, the goal would be to have a function where I give the order up to which I want to go and that do this process up to the specified order.

Is there a convenient way to do it? Also using another method since mine seems somehow not very optimized.

  • $\begingroup$ Since the first two parameters in C and SC are staying constant, your example would be easier to read if you removed them. Provide a simple one-parameter version for people to work on and make it complete (ie include a definition for Funct). Also it's advisable to use lower-case letters for symbols, to avoid potential conflict with built-ins. For example C is a built-in. $\endgroup$ Mar 2, 2017 at 20:10
  • $\begingroup$ Agree with Simon Wood, we sure need the definition of Funct to be added to your query. $\endgroup$ Mar 2, 2017 at 21:56
  • $\begingroup$ I edited the question with a definition of funct, and I did a one-parameter problem. $\endgroup$
    – Ezareth
    Mar 3, 2017 at 9:09

2 Answers 2


It seems that you want something like this:

funct[n_] := Sum[If[i == 0, c[0], i*c[i]], {i, 0, n}]

c0[k_] := Sum[n1^i, {i, 0, k}]

sols = {};

 AppendTo[sols, Solve[(funct[i] /. sols) == c0[i], c[i]][[1, 1]]],
 {i, 0, 4}

{c[0] -> 1, c[1] -> n1, c[2] -> n1^2/2, c[3] -> n1^3/3, c[4] -> n1^4/4}

If you permit direct assignments to c[x] this could also be written without sols:

Table[Set @@ Solve[funct[i] == c0[i], c[i]][[1, 1]], {i, 0, 4}]
{1, n1, n1^2/2, n1^3/3, n1^4/4}

After which these definitions exist:







Here is a somewhat different approach that you may consider.

First built a list of all your equations using Table:

   funct[i] == c0[i],
   {i, 0, 4}
{c[0] == 1,
 c[0] + c[1] == 1 + n1,
 c[0] + c[1] + 2 c[2] == 1 + n1 + n1^2,
 c[0] + c[1] + 2 c[2] + 3 c[3] == 1 + n1 + n1^2 + n1^3, 
 c[0] + c[1] + 2 c[2] + 3 c[3] + 4 c[4] == 1 + n1 + n1^2 + n1^3 + n1^4}

Then make a list of the variables you wish to solve for using Array:

vars = Array[c, 5, 0]
{c[0], c[1], c[2], c[3], c[4]}

Finally Reduce using the option Backsubstitution -> True

enter image description here

result = Reduce[eqs, vars, Backsubstitution -> True]
c[0] == 1 && c[1] == n1 && c[2] == n1^2/2 && c[3] == n1^3/3 && c[4] == n1^4/4

Use ToRules if needed:

vars /. ToRules[result]
{c[0] -> 1, c[1] -> n1, c[2] -> n1^2/2, c[3] -> n1^3/3, c[4] -> n1^4/4}

{1, n1, n1^2/2, n1^3/3, n1^4/4}
  • $\begingroup$ @Ezareth You are welcome. If this answer is fully satisfactory please consider Accepting it. If it is not let me know how it might be improved. $\endgroup$
    – Mr.Wizard
    Mar 9, 2017 at 23:49
  • $\begingroup$ @Ezareth I added a second method to my answer for your consideration. $\endgroup$
    – Mr.Wizard
    Mar 9, 2017 at 23:59

If you write a function that takes and returns the solutions of the previous steps then you can use Fold.

findSol[sols_, n_] :=
  c[n] ->
   (c[n] /. First@Solve[(funct[n] /. sols) == c0[n], c[n]])

findSol takes the list of solutions, sols, performs the Solve, and returns the previous solutions and the one just solved.

Next use findSol in Fold over the indices you want to solve for.

res = Fold[findSol, {}, Range[0, 4]]
{c[0] -> 1, c[1] -> n1, c[2] -> n1^2/2, c[3] -> n1^3/3, c[4] -> n1^4/4}

Hope this helps.


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.