6
$\begingroup$

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>)]?

ADD

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.

ADD 2

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.

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

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}}       *)
$\endgroup$
1
  • $\begingroup$ thanks, I'm aware of NestWhile, but if I need to use simple RecurrenceTable, is it possible to add While in it? $\endgroup$
    – macros
    Apr 23 at 6:59

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.