# Writing an iteration using two functions [closed]

I have been given two functions with an initial condition. One function becomes the variable of the other. I need to run the program for 10 iterations.

d = 100 (initial condition)
x = (300*d)/(d + 100)


Next

d1 = 200 - x


d1 should become the variable of the function x instead of d.

Again

 x2 = (300*d1)/(d1 + 100)
d2 = 200 - x2
x3 = (300*d2)/(d2 + 100)
d3 = 200 - x3


and repeat the process until 10 iterations have been made.

How can I write a program to carry out this process?

f[y_, x_] := {200 - x, 300 y/(y + 100)}
ic = {100, 150}
nf[n_] := NestList[f @@ # &, ic, n]
TableForm[nf[10], TableHeadings -> {Range[0, 10], {"d", "x"}}]


• Thank you very much. it helps a lot. I really needed to display it as a table too. Thank you once again. – prasanthi Mar 31 '17 at 21:44
• My goodness: Isn't that my solution (below)? – David G. Stork Mar 31 '17 at 22:14
• @DavidG.Stork I am sorry I did not see your solutions. I agree that they use the same strategy. However your use of null rather than the value of the initial x renders your output difficult to interpret. This, I agree, is a trivial difference but I can only express that it was unintentional. I suggest that you raise with OP. I will completely accept change in vote etc. – ubpdqn Apr 1 '17 at 0:15

Let's do two simple pre-computations.

With[{d = 200 - x}, (300 d)/(d + 100)]


(300 (200 - x))/(300 - x)

and

With[{d = 100}, (300 d)/(d + 100)]


150

Then the iteration can be written as

NestList[300 (200 - #)/(300 - #) &, 150, 10]


{150, 100, 150, 100, 150, 100, 150, 100, 150, 100, 150}

f1 = Function[d, 300*d/(d + 100)]  (* your first transformation *)
f2 = Function[x, 200 - x]  (* your second transformation *)
f = f2@*f1  (* your composed transformation *)
(* using NestList *)
NestList[f, 100, 10]
(* using RecurrenceTable *)
RecurrenceTable[{d[n + 1] == f[d[n]], d[0] == 100}, d, {n, 0, 10}]


One could compose the two component function, but another trick is to compute {f1[x], f2[f1[x]}, then take the second component and feed it back as the new x:

Flatten@NestList[
{temp = 300 #[[2]]/(#[[2]] + 100), 200 - temp} &,
{Null, 100}, 10]


(*

{Null, 100, 150, 50, 100, 100, 150, 50, 100, 100, 150, 50, 100, 100, 150, 50, 100, 100, 150, 50, 100, 100}

*)

I note that I get a different sequence than @corey979.

• Yes sequence is little bit different. But thanks a lot for the help. – prasanthi Mar 31 '17 at 21:52