# RecurrenceTable and While

Good day.

Let's take an example from Help on RecurrenceTable:

RecurrenceTable[{x[n + 1] == 0.7 x[n] + y[n], y[n + 1] == -0.7989995 + x[n]^2, x[0] == 0.142857,
y[0] == 0.33}, {x, y}, {n, 1, 2500}]


How to add here a While condition so that iterations stop after x[n] reached some threshold value, smth like While[x[n] > -1, <continue recurrence>)]?

I need this because in my real code for some initial conditions there will be overflow, so MMA issues warnings, and I think it is better to stop iterations at some threshold.

Simple approach, but I think not efficient in terms of time and memory, would be this:

RecurrenceTable[{x[n + 1] == If[x[n] < -1, -10, 0.7 x[n] + y[n]],
y[n + 1] == If[x[n] < -1, -10., -0.7989995 + x[n]^2],
x[0] == 0.142857, y[0] == 0.33}, {x, y}, {n, 0, 10}] /.{-10., -10.}->Nothing


Of course, here we have unneeded iterations.

• Like a WhenEvent for recurrence tables? Great idea! – Roman Apr 23 at 6:52
• @Roman, there are NestWhile, FoldWhile, but I need smth like RecurrenceTableWhile, not sure this is possible. – macros Apr 23 at 6:57
• Did you try WhenEvent? – Rom38 Apr 23 at 7:00
• @Rom38, no I have no idea how to add it here. – macros Apr 23 at 7:00

For an explicit recurrence relation like yours, an iterative approach with NestWhileList works:

NestWhileList[{0.7 #[[1]] + #[[2]], -0.7989995 + #[[1]]^2} &,
{0.142857, 0.33},
#[[1]] > -1 &]

(*    {{0.142857, 0.33},
{0.43, -0.778591},
{-0.477591, -0.6141},
{-0.948414, -0.570906},
{-1.2348, 0.100489}}       *)

• thanks, I'm aware of NestWhile, but if I need to use simple RecurrenceTable, is it possible to add While in it? – macros Apr 23 at 6:59