I would like to know you to plot a recurrence relation using foreach, which requires updating a variable inside the loop. To make this concrete without copying over a bunch of code, please consider this existing example, which uses copy/paste rather than foreach. Can this example be changed to update a variable in a foreach loop, without introducing a definition at document scope. If not, why not?
As another concrete illustration, this answer uses foreach but introduces a global definition (see the line with \newcommand{\x}{.1}). I don't want to introduce any definitions at document scope. Surely this is some way to use the TikZ registers instead? If not, can you say why not?
Abstractly, the problem involves an initial condition, a sequence produced by function iteration, and the plotting of coordinates produced from each adjacent pair in the sequence. For example, given x0 and then x1=f(x0), plot a point at (x0,x1). Then with x2=f(x1), plot a point at (x1,x2). And so on, as many times as requested.

pgfplotshas the command\pgfplotsforeachungroupedfor that purpose. – Nov 07 '20 at 20:18