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