-
Notifications
You must be signed in to change notification settings - Fork 0
/
newton.8th
50 lines (39 loc) · 955 Bytes
/
newton.8th
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
\ -*- 8th -*-
\ : File: \texttt{newton.8th} \\
\ : Copyright (c) 2020, Moritz Frisch\\
\ : All rights reserved.\\
\ : This file may be used according to the BSD 3-Clause License\\
\ : see LICENSE for details\\
\ : Find a root using Newton's method\\
needs tools
needs numerics
with: nm
with: n
\ : Test function: $e^x-2$, has a root at $\ln 2\sim 0.69$.
: f-test \ n -- n
dup cos swap - ;
\ : Derivative: $e^x$.
: f'-test \ n -- n
sin neg 1 - ;
: nwt-criterion? \ x1 n -- x1 n
row @ 3 a:@ 0~ swap
2 a:@ 0~ nip or ;
: nwt-algorithm \ x0 n -- x1 n
>r 0 item!
dup dup dup f 2 item!
swap f' / - 1 item!
2dup - abs 3 item! drop nip r> ;
: newton \ n -- n
["x0","x1","f(x0)","|x1-x0|"] main ;
num-debug on
\ 3 max-iterations !
' f-test w:is f
' f'-test w:is f'
' nwt-algorithm w:is algorithm
' nwt-criterion? w:is criterion?
\ 10 print-lines !
200 big-floats
10 man!
2 print-lines !
F1. newton .s f. cr
;with ;with bye