In many cases only the last iterate is of interest, particularly when the fourth argument is omitted or the Simplify Basic commandBQ.NIL is used so that convergence is less of an issue.  ITERATE is identical to ITERATES, except that ITERATE returns only the last iterate.  ITERATE returns ? if the iteration does not converge to a single unique value (i.e. if the cycle length is not 1).


For example, to compute the nth power of x, define the function


       POWER(x, n) := ITERATE(a·x, a, 1, n)


Here the update formula  a <-- a·x  is being applied n times, starting the accumulator a with the initial value 1.  This iteration multiplies n copies of x, giving x^n.  Although Derive has a built-in ^ operator for raising an expression to a power, POWER is a good elementary application demonstrating the use of ITERATE.


When an iteration depends on two or more of the previous iterates, you can use a vector with ITERATE to store the previous iterates.  For example, the Fibonacci numbers are defined by the recurrence


       FIB(n) = 0                        if n = 0,
       FIB(n) = 1                        if n = 1, and
       FIB(n) = FIB(n-1) + FIB(n-2)        if n > 1.


where n is a nonnegative integer.  Use a two-element vector v to store the value of FIB(n-2) and FIB(n-1).  Then by letting n=2 the above recurrence specifies that the initial value of v is [0, 1].  Also it specifies that to update v, make its new first component its previous second component and make its new second component the sum of the two previous components.  This initialization and update are accomplished using ITERATE as follows:


FIB(n) := ITERATE([v SUB 2, v SUB 1 + v SUB 2], v, [0, 1], n)


If FIB is defined as shown and n is a nonnegative integer, FIB(n) will simplify to a vector whose first element is the nth Fibonacci number and whose second is the n+1th Fibonacci number.  For example, FIB(100) simplifies to


       [354224848179261915075, 573147844013817084101]


and 354224848179261915075 is the 100th Fibonacci number.


Instead of a variable, the second argument of ITERATE and ITERATES can be a vector of variables and the third argument a vector of values for those variables.  This can simplify definitions using these functions by eliminating the need to use subscripts.  For example, this simplifes the definition for FIB given earlier to


       FIB (n) := ITERATE([k, j+k], [j, k], [0, 1], n)


For additional examples of iteration, inspect the definitions of the functions


       FIXED_POINT and NEWTONS in the file EquationSolving.mth.S02QQ,


       IMP_DIF and PARA_DIF in the file DifferentiationApplications.mthAUKXVG, and


       PICARD, EULER, TAYLOR_ODE1, TAYLOR_ODES and RK in the file ODEApproximation.mth1NBVQQO.


Other Programming in DERIVEProgramming 

Created with the Personal Edition of HelpNDoc: Create cross-platform Qt Help files