integrating a stateful with rk()





What is the proper method in Maxima to integrate a stateful function with rk()?

Below is a greatly simplified example to illustrate the problem. %i4 gives the wrong answer, and %i5 crashes.


Thank you.


------------------ begin example -----------------------------

Maxima 5.27.0 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.8 (a.k.a. GCL)
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.

(%i1) load("dynamics")$

(%i2) r:0$

(%i3) f(x):=(if r<5 then r:r+1, r)$

(%i4) rk(f(x),x,0,[t,0,1,0.2]);

(%o4) [[0, 0], [0.2, 0.2], [0.4, 0.4], [0.6, 0.6], [0.8, 0.8], [1.0, 1.0]]

(%i5) rk('(f(x)),x,0,[t,0,1,0.2]);

Expecting a number when the initial state is replaced in the equations, but instead found:
?r
#0: rk(odes=f(x),state=x,initial=0,domain=[t,0,1,0.2])
?-- an error. To debug this try: debugmode(true);

-------------------- end --------------------------------------