# Using the Nest function -- Confusion on my output

I have the following code and output where the original function is $f(x,a) = a^2 - x^2$.

f[x_, a_] = a - x^2
ref := Nest[Function[{u, v}, f[u, v]][x, y], x, 2]
ref
(-x^2 + y)[(-x^2 + y)[x]]


I want the second iteration of this function. i.e, $f(f(x)) = a^2 - (a^2 - x^2)^2$. Why am I not getting this?

• You need to use SetDelayed (:=) when you make the function assignement. ref on the other hand should be Set (=) since it will compute the same thing each time ref is called anyhow. Feb 25, 2016 at 13:54
• oh okay. Thank you! Feb 25, 2016 at 13:58

## 1 Answer

In your code, you give a 2-argument function as the first argument to Nest, but then only one expression. From the documentation, I only see examples of Nest being given a single argument.

Here I use a single-argument function, since that seems to match the output you want,

f[x_, a_] = a^2 - x^2;
Nest[Function[x, f[x, a]], x, 2]
(* a^2 - (a^2 - x^2)^2 *)

• Ah yep! That is what I was wanting. Thanks! Feb 25, 2016 at 14:00
• There were a couple more syntax problems with your input as well, like when you write Function[{u, v}, f[u, v]] that is a function that will take arguments, but when you put [x,y] after it, it's no longer a function since you are going to evaluate it already with x and y as the arguments. Look at the syntax here and in the help file. Marius's point about defining f with a := is a valid one, but it is often okay to use = instead, you just have to see where it will trip you up. Feb 25, 2016 at 14:04
• @Ozera , go ahead and take the tour(mathematica.stackexchange.com/tour) here, and see this thread for good pointers. Feb 25, 2016 at 14:05
• Thank you both for the information! Learning mathematica is an interesting experience indeed. Feb 25, 2016 at 14:09