# Single variable function minimizer The function must be continuous unimodal, taking a floating point argument and returning a floating point value. The method is binary search based on the derivative at the splitting point. Convergence is judged by the change in function value. First, words defined elsewhere. ~~~ {{ :d:another-name (as-) d:create &class:word reclass d:last d:xt swap d:xt fetch swap store ; ---reveal--- :d:aka (s-)_Also-Known-As,_make_alias_of_the_last_defined_word [ d:last ] dip d:another-name ; 'aka d:aka :d:alias (ss-)_make_alias_s2_of_s1 [ d:lookup ] dip d:another-name ; 'alias d:aka }} 'var-n 'var! (n-) alias 'lt? 'n:_X) @L @H + #2 / !X ; :x1y (-) @X n:inc e:to-f @F call X1y e:! ; ---reveal--- :minimize (a-_nn-x)_(L_H_F_->_X) (minimize_floating_point_function_F_over_[L,H] (initialize ( !F f:to-e !H f:to-e !L ) ( L Ly @F e:call.vv ) ( H Hy @F e:call.vv ) x y ( #0 !Itr ) [ trace x1y ( @Y @X1y n:_X) @L @H + 2 / !X ; rather than :x (-)_(L_H_->_X) ( L e:@ ) ( H e:@ ) f:+ .2. f:/ X e:! ; Doing ``` :f (-_x-y) ; .0 .5 &f minimize . nl ``` with the first definition gives # low mid high 0 0.000000 1.249991 5.000009 1 0.000000 0.312492 1.249991 2 0.000000 0.078120 0.312492 3 0.000000 0.019530 0.078120 4 0.000000 0.004882 0.019530 5 0.000000 0.001220 0.004882 6 0.000000 0.000305 0.001220 7 0.000000 0.000076 0.000305 8 0.000000 0.000019 0.000076 9 0.000000 0.000005 0.000019 10 0.000000 0.000001 0.000005 final x = 0.000000 whereas with the second definition gives # low mid high 0 0.000000 2.500004 5.000009 1 0.000000 1.249991 2.500004 2 0.000000 0.625001 1.249991 3 0.000000 0.312503 0.625001 4 0.000000 0.156254 0.312503 5 0.000000 0.078126 0.156254 6 0.000000 0.039062 0.078126 7 0.000000 0.019530 0.039062 8 0.000000 0.009765 0.019530 9 0.000000 0.004883 0.009765 10 0.000000 0.002441 0.004883 11 0.000000 0.001221 0.002441 12 0.000000 0.000611 0.001221 13 0.000000 0.000305 0.000611 14 0.000000 0.000153 0.000305 15 0.000000 0.000076 0.000153 16 0.000000 0.000038 0.000076 17 0.000000 0.000019 0.000038 18 0.000000 0.000009 0.000019 final x = 0.000005