# Replacement rule within a iteration with a new replacement needed at each step

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, i*c[i]], {i, 0, n}]
In:= funct
Out= c + c + 2 c + 3 c

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 with the first result will allow me to fix c. Then funct will depend on both c and c but as I know c, I will be able to fix directly c etc... . So the first step of the iteration is

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


This fixes

resulttab[] = 1


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

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


. The third step would then be

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


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.

• 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. – Simon Woods Mar 2 '17 at 20:10
• Agree with Simon Wood, we sure need the definition of Funct to be added to your query. – Jack LaVigne Mar 2 '17 at 21:56
• I edited the question with a definition of funct, and I did a one-parameter problem. – Ezareth Mar 3 '17 at 9:09

It seems that you want something like this:

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

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

sols = {};

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

sols

{c -> 1, c -> n1, c -> n1^2/2, c -> n1^3/3, c -> 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:

?c

Globalc

c=1

c=n1

c=n1^2/2

c=n1^3/3

c=n1^4/4


Here is a somewhat different approach that you may consider.

First built a list of all your equations using Table:

eqs=
Table[
funct[i] == c0[i],
{i, 0, 4}
]

{c == 1,
c + c == 1 + n1,
c + c + 2 c == 1 + n1 + n1^2,
c + c + 2 c + 3 c == 1 + n1 + n1^2 + n1^3,
c + c + 2 c + 3 c + 4 c == 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, c, c, c, c}


Finally Reduce using the option Backsubstitution -> True result = Reduce[eqs, vars, Backsubstitution -> True]

c == 1 && c == n1 && c == n1^2/2 && c == n1^3/3 && c == n1^4/4


Use ToRules if needed:

ToRules[result]
vars /. ToRules[result]

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

{1, n1, n1^2/2, n1^3/3, n1^4/4}

• @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. – Mr.Wizard Mar 9 '17 at 23:49
• @Ezareth I added a second method to my answer for your consideration. – Mr.Wizard Mar 9 '17 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_] :=
Append[sols,
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 -> 1, c -> n1, c -> n1^2/2, c -> n1^3/3, c -> n1^4/4}
`

Hope this helps.