1 ! Copyright (c) 2008 Reginald Keith Ford II.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.function-tools math.points ;
6 ! Secant method of approximating roots
10 : secant-solution ( x1 x2 function -- solution )
11 [ eval ] curry bi@ linear-solution ;
13 : secant-step ( x1 x2 func -- x2 x3 func )
14 [ secant-solution ] 2keep swapd ;
16 : secant-precision ( -- n ) 15 ; inline
20 : secant-method ( left right function -- x )
21 secant-precision [ secant-step ] times drop + 2 / ;
23 ! : close-enough? ( a b -- t/f ) - abs tiny-amount < ;
25 ! : secant-method2 ( left right function -- x )
27 ! [ drop average ] [ secant-step secant-method ] if ;