3
$\begingroup$

Suppose I have the following functions:

a[x_] := x
b[x_] := x (a[x]) - a[x] + 1
c[x_] := x (b[x]) - b[x] + 1
d[x_] := x (c[x]) - c[x] + 1

as you can see each function uses previous function to calculate the output. I wonder how can I write a generalised version of this so I can tell the function the number of iteration and it returns the outputs?

Update: I made the following:

f[x_] := x
Fold[f[#2][#] &, x (f[x]) - f[x] + 1, Range[5]] // ExpandAll

Which indeed works, yet I dont know how to expand the results as a list, so far it returns:

5[4[3[2[1[1 - x + x^2]]]]]
$\endgroup$

1 Answer 1

4
$\begingroup$

The following should work:

f[0, x_] := x
f[i_, x_] := x (f[i - 1, x]) - f[i - 1, x] + 1

The idea is to add a second parameter to the function to specify the "level" of the recursion. Then we simply need a base case (here given by f[0, x_] := x), and we are done. You can verify that this yields the same result as your original functions:

d[x] == f[3, x]
(* True *)

An alternative is to use RSolve to solve for an explicit formula for f[i, x]:

g[i_, x_] = f[i] /. First@RSolve[{f[0] == x, f[i] == x (f[i - 1]) - f[i - 1] + 1}, 
 f[i], i]
(* (-1 + (-1 + x)^i - 2 (-1 + x)^i x + (-1 + x)^i x^2)/(-2 + x) *)

Again, we can verify that this is correct:

f[5, x] == g[5, x] // FullSimplify
(* True *)
$\endgroup$

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.