Nest
Examples
open all close allBasic Examples (2)
Scope (4)
Nesting a function can build a formula:
Nest[(1 + #) ^ 2&, x, 5]Nesting can return a single number:
Nest[Sqrt, 100.0, 4]The nested function can operate on a list:
With[{ϵ = 0.1}, Nest[Function[xy, {2ϵ, 0} + {{1 - 2ϵ, 0}, {0, 1 - 2ϵ}}.xy], {1.0, 1.0}, 5]]Apply the operator form to different starting expressions:
op = Nest[Sin, 3];{op[0], op[π / 4], op[π / 2]}Generalizations & Extensions (2)
Applications (8)
Nest[1 / (1 + #)&, x, 5]Nest[x ^ #&, x, 6]Growth of annually compounded capital in 10 years:
Nest[#(1 + 0.05)&, 1000, 10]Nest[(# + 2 / # ) / 2&, 1.0, 5]Nest[StringReplace[#, {"A" -> "BA", "B" -> "AB"}]&, "A", 5]Consecutive pairs of Fibonacci numbers:
Nest[{{1, 1}, {1, 0}}.#&, {0, 1}, 10]Functional composition for higher-order Newton iteration (for
):
Function[x, Evaluate[Together[Nest[Function[z, (z + 3 / z) / 2], x, 4]]]]FixedPointList[%, 1`50]Generate a bifurcation diagram for an iterated logistic map:
ListPlot[Table[Thread[{r, Nest[r # (1 - #)&, Range[0, 1, 0.01], 1000]}], {r, 0, 4, 0.01}]]Properties & Relations (6)
Nest gives the last element of NestList:
NestList[f, x, 4]Nest[f, x, 4]Nest is effectively Composition using the same function multiple times:
Nest[f, x, 3]Composition[f, f, f][x]Use RSolve to symbolically compute Nest operations:
f[x_] := (2x + 3/x + 4)RSolve[y[k + 1] == f[y[k]] && y[0] == 0, y, k]g = y /. First[%]Different length compositions or nests give the same result:
Table[g[k], {k, 1, 5}]Table[Nest[f, 0, k], {k, 1, 5}]FixedPoint automatically goes on until the result no longer changes:
Nest[Floor[# / 2]&, 20, 6]FixedPoint[Floor[# / 2]&, 20]NestWhile goes on while a condition is true:
NestWhile[If[EvenQ[#], # / 2, (3 # + 1) / 2]&, 29, # > 1&]Fold automatically inserts second arguments from a list:
Nest[f[#, a]&, x, 3]Fold[f, x, {a, a, a}]Neat Examples (4)
Nest[Framed, x, 6]Nest[{#, #}&, x, 3]Nest[Framed[Row[{#, #}]]&, "", 5]Nest[p[#][#]&, x, 4]Nest[Subsuperscript[#, #, #]&, x, 5]Nest[Join[#, Length[#] + Reverse[#]]&, {0}, 4]BaseForm[%, 2]See Also
NestList NestGraph NestWhile Fold Function FixedPoint Do Composition
Tech Notes
Related Guides
History
Introduced in 1988 (1.0) | Updated in 1996 (3.0) ▪ 2024 (14.1)
Text
Wolfram Research (1988), Nest, Wolfram Language function, https://reference.wolfram.com/language/ref/Nest.html (updated 2024).
CMS
Wolfram Language. 1988. "Nest." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/Nest.html.
APA
Wolfram Language. (1988). Nest. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Nest.html
BibTeX
@misc{reference.wolfram_2026_nest, author="Wolfram Research", title="{Nest}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/Nest.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_nest, organization={Wolfram Research}, title={Nest}, year={2024}, url={https://reference.wolfram.com/language/ref/Nest.html}, note=[Accessed: 13-June-2026]}