]> gitweb.factorcode.org Git - factor.git/blob - extra/math/secant-method/secant-method.factor
factor: trim using lists
[factor.git] / extra / math / secant-method / secant-method.factor
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 ;
4 IN: math.secant-method
5
6 ! Secant method of approximating roots
7
8 <PRIVATE
9
10 : secant-solution ( x1 x2 function -- solution )
11     [ eval ] curry bi@ linear-solution ;
12
13 : secant-step ( x1 x2 func -- x2 x3 func )
14     [ secant-solution ] 2keep swapd ;
15
16 : secant-precision ( -- n ) 15 ; inline
17
18 PRIVATE>
19
20 : secant-method ( left right function -- x )
21     secant-precision [ secant-step ] times drop + 2 / ;
22
23 ! : close-enough? ( a b -- t/f ) - abs tiny-amount < ;
24
25 ! : secant-method2 ( left right function -- x )
26     ! 2over close-enough?
27     ! [ drop average ] [ secant-step secant-method ] if  ;