Group Abstract Group Abstract

Message Boards Message Boards

Iterating the function n times

GROUPS:
How can I  iterate the equation ax^2+b of n time?
POSTED BY: Henri Isomyyr�
Answer
10 months ago
I think NestList is what you want. 
POSTED BY: Bruce Miller
Answer
10 months ago
Thank you for your help!

I tried that Nestlist-function but I am not sure that this gives me what I want... or mayby I dont know how to use it. But let say that I want to iterate x^2+b of 2 times

NestList[x, x^2 + a, 2]
{a + x^2, x[a + x^2], x[x[a + x^2]]}

its give me something like that. I would like to see 

b + b^2 + 2 b x^2 + x^4

Do I use wrong NestList-function or is there some other method to calculate this kind of iteration?

Thank you.
POSTED BY: Henri Isomyyr�
Answer
10 months ago
You probably want this:
Nest[#^2 + b &, x, 2] // Expand

(* b + b^2 + 2 b x^2 + x^4 *)
Firstly note the order of arguments; function, initial argument for the function, and nesting depth. Secondly, Not the construction used for function; you have to make it clear what is the argument; it's marked by # in Mathematica, and & ends the Function. This is equivalent to more conventional form, where you name your function f and its' argument x:
f[x_] := x^2 + b
Nest[f, x, 2] // Expand

(* b + b^2 + 2 b x^2 + x^4 *)
Every convention above has a specific meaning; often you can write something that looks almost the same, but results are quite different. Eagerly head for the help center and tutorials; they're really good.

The last part in the above puzzle is the // Expand, which expands positive integer producs in the top level of the result of Nest. Without it, you get a mathematically identical result, but in a different form:
Nest[#^2 + b &, x, 2]

(* b + (b + x^2)^2 *)
Just to check, you can actually nest the function "manually", and this also results the symbolic result, assuming you haven't defined x earlier in the session:
f[x_] := x^2 + b
f[f[x]]

(* b + (b + x^2)^2 *)
POSTED BY: Jari Kirma
Answer
10 months ago