Created
October 14, 2015 19:02
-
-
Save muspellsson/9a7ec227feb92989d1ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REBOL [] | |
math: context [ | |
eps: 1E-15 | |
steps: 0 | |
math-init: func [] [ | |
steps: 0 | |
] | |
derive: func [ | |
f [function!] | |
x [decimal!] | |
/local d1 d2 d3 d4 | |
] [ | |
d1: negate f x + (2 * eps) | |
d2: 8 * (f x + eps) | |
d3: -8 * (f x - eps) | |
d4: f x - (2 * eps) | |
(d1 + d2 + d3 + d4) / (12 * eps) | |
] | |
newton: func [ | |
f [function!] | |
guess [decimal!] | |
/local x | |
] [ | |
math-init | |
x: guess | |
until [ | |
steps: steps + 1 | |
x: x - ((f x) / (derive :f x)) | |
(abs f x) < eps | |
] | |
x | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment