Intro 23. Iteration

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Dictionary

23. Iteration
The repetition of a process, or of a sequence of similar processes, is called iteration. Much iteration is implicit, as in`*/b` and`a*/b`,`` and`a*b` ;`` explicit iteration is provided by the power conjunction`` (`^:`),`` by agenda`` (`@.`)`` with self-reference, and by control structures:

       (cos=: 2&o.) ^: (i.6) b=: 1
    1 0.540302 0.857553 0.65429 0.79348 0.701369

       ]y=: cos^:_ b
    0.739085

       y=cos y
    1

The example`cos^:_` illustrates the fact that infinite iteration is meaningful (that is, terminates) if the process being applied converges.
Controlled iteration of a process`p` is provided by`p^:q` ,`` where the result of`q` determines the number of applications of`p` performed. The forms`$:@p^:q` and`p^:q^:_` are commonly useful.
If`f` is a continuous function, and if`f i` and`f j` differ in sign, then there must be a root`r` between`i` and`j` such that`f r` is zero; the list`b=:i,j` is said to bracket a root. A narrower bracket is provided by the mean of`b` together with that element of`b` whose result on applying`f` differs in sign from its result. Thus:

       f=: %: - 4:                 NB. A sample function

       root=: 3 : 0
    m=. +/ % #
    while. ~:/y.
    do.
    if. ~:/ * f ({.,m) y.
       do. y.=. ({.,m) y.
     else. y.=. ({:,m) y.
    end.
    end. m y.
    )

       b=: 1 32
       root b
    16

       f b,root b
    _3 1.65685 1.77636e_15

Exercises

23.1 | Use the function`root` to find the roots of various functions, such as`f=: 6&-@!`
23.2 | Experiment with the function`fn=: +/\` (which produces the figurate numbers when applied repeatedly to`i.n`),`` and explain the behaviour of the function`fn^:(?@3:)`

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Dictionary