1

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?

Ozera
  • 125
  • 5

1 Answers1

2

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 *)
Jason B.
  • 68,381
  • 3
  • 139
  • 286
  • Ah yep! That is what I was wanting. Thanks! – Ozera Feb 25 '16 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. – Jason B. Feb 25 '16 at 14:04
  • 1
    @Ozera , go ahead and take the tour here, and see this thread for good pointers. – Jason B. Feb 25 '16 at 14:05
  • Thank you both for the information! Learning mathematica is an interesting experience indeed. – Ozera Feb 25 '16 at 14:09