3
$\begingroup$

I want to iterate a function of two variables in NestWhileList. Obviously it will need a termination condition.

For example imagine I have this function:

f45[{x_, y_}] := {x + 1, y^2 + 1};
NestWhileList[f45, {0.1, 0.1}, "here I dont know"]

I put "here I dont know" where the termination test goes. The termination condition I want to use is:

  • When the norm of the last vector interated is less than 1, then the iteration will stop, something like this.

    NestWhileList[f45, {0.1, 0.1}, Norm[Last[f45]]] < 1]
    

If you help me, I will appreciate it,

By the way, I have seen many posts about NestWhileList, but only with functions of one variable and working with "#" arguments, I know about them, but I'm not expert with them.

$\endgroup$
  • 1
    $\begingroup$ Use something like: Norm[#] >= 1 & as your condition. Notice, however, that the Norm of your vectors is increasing with every iteration, not decreasing, and increasing very quickly at that. It will only take one iteration to get a vector whose norm is greater than 1. You will want to take a look at this tutorial on Pure Functions $\endgroup$ – MarcoB Jun 30 '16 at 0:08
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Jun 30 '16 at 0:39
  • $\begingroup$ Yes I tried that directly in the "test" but didn't work, it only gives me the first vector,and not because as you say it' s increasing quickly, the real function I want to to evaluate is more complicated and it has another initial conditions, I only put this as an example of the same body. $\endgroup$ – Carlos Díaz Jun 30 '16 at 0:47
4
$\begingroup$

Maybe this will make it clear:

condition[vector_] := Norm[vector] < 10

f45[{x_, y_}] := {x + 1, y^2 + 1};
NestWhileList[f45, {0.1, 0.1}, condition]

(*
==> {{0.1, 0.1}, {1.1, 1.01}, {2.1, 2.0201}, {3.1, 5.0808}, {4.1,
   26.8146}}
*)

Here I defined the condition as a separate function to show how the last argument of NestWhileList is constructed. As a simple example, I allow the list to be built as long as the norm of the vector is less than 10.

$\endgroup$
  • $\begingroup$ Thanks! now I know how NestWhileList tests. $\endgroup$ – Carlos Díaz Jun 30 '16 at 0:42
2
$\begingroup$

Jens has shown you how do it an nice, readily understood way. Here is the same code written in the way that becomes idiomatic as you gain experience.

f45[{x_, y_}] := {x + 1, y^2 + 1}
With[{max = 2}, NestWhileList[f45, {0.1, 0.1}, Norm[#] < max &]]

{{0.1, 0.1}, {1.1, 1.01}, {2.1, 2.0201}}

I also want to tell you that f45 is technically a function of one variable, not two, which makes your problem easy. You are taking advantage of Mathematica'a argument destructing, a valuable technique of which I strongly approve.

If you really had a function of two variables it would be a little more complicated.

f46[x_, y_] := {x + 1, y^2 + 1}
With[{max = 2}, NestWhileList[f46[#[[1]], #[[2]]] &, {0.1, 0.1}, Norm[#] < max &]]

{{0.1, 0.1}, {1.1, 1.01}, {2.1, 2.0201}}

$\endgroup$
  • $\begingroup$ Perhaps one could use f46[Sequence@@#]& in the second example. I personally would find it more readable. $\endgroup$ – MarcoB Jun 30 '16 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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