1
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Feb 25, 2016 at 13:54
  • $\begingroup$ oh okay. Thank you! $\endgroup$
    – Ozera
    Feb 25, 2016 at 13:58

1 Answer 1

2
$\begingroup$

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 *)
$\endgroup$
4
  • $\begingroup$ Ah yep! That is what I was wanting. Thanks! $\endgroup$
    – Ozera
    Feb 25, 2016 at 14:00
  • $\begingroup$ 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. $\endgroup$
    – Jason B.
    Feb 25, 2016 at 14:04
  • 1
    $\begingroup$ @Ozera , go ahead and take the tour(mathematica.stackexchange.com/tour) here, and see this thread for good pointers. $\endgroup$
    – Jason B.
    Feb 25, 2016 at 14:05
  • $\begingroup$ Thank you both for the information! Learning mathematica is an interesting experience indeed. $\endgroup$
    – Ozera
    Feb 25, 2016 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.