For more information including compatibility, examples and test cases, see https://github.com/petercrlane/r7rs-libs

0.1. Minimize: (import (slib minimize))

The Golden Section Search algorithm can find minima of functions where derivatives are expensive to compute or unavailable.

golden-section-search takes four arguments:

  1. the function to minimize (of one argument)

  2. the lowest value of x to search from

  3. the highest value of x to search to

  4. a stopping condition for the search, which is one of

    1. a positive number defines a target tolerance

    2. a negative number is negated to define the number of iterations

    3. a function of (x0 x1 a b fa fb count) ends the search by returning #t

The function returns a dotted pair: the value of x for the minimum and f(x)

sash[r7rs]> (golden-section-search square -10 10 -100)  ; <1>
(4.258043576850981e-21 . 1.8130935102361897e-41)        ; <2>
  1. Find the minimum of the square value in range (-10, 10) for 100 iterations

  2. The minimum is x=0 and (square x)=0

The following example minimizes its function to within the specified tolerance:

sash[r7rs]> (golden-section-search (lambda (x) (+ (* x x x) (* -2 x) -5)) 0 1 (/ 10000)))
(0.8164883855245578 . -6.0886621077391165)