Skip to content

Instantly share code, notes, and snippets.

@muspellsson
Created October 14, 2015 19:02
Show Gist options
  • Save muspellsson/9a7ec227feb92989d1ce to your computer and use it in GitHub Desktop.
Save muspellsson/9a7ec227feb92989d1ce to your computer and use it in GitHub Desktop.
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