Skip to content

Commit d338f4a

Browse files
Merge pull request #16 from eriktaubeneck/cleanup
changed epsilon choice in finite difference to match literature
2 parents 306f9c3 + bfb189a commit d338f4a

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/finite_difference.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ function finite_difference{T <: Number}(f::Function,
2525
x::T,
2626
dtype::Symbol = :central)
2727
if dtype == :forward
28-
epsilon = sqrt(eps(max(one(T), abs(x))))
28+
epsilon = sqrt(eps(T))*max(one(T),abs(x))
2929
xplusdx = x + epsilon
3030
# use machine-representable numbers
3131
return (f(xplusdx) - f(x)) / epsilon
3232
elseif dtype == :central
33-
epsilon = cbrt(eps(max(one(T), abs(x))))
33+
epsilon = cbrt(eps(T))*max(one(T), abs(x))
3434
xplusdx, xminusdx = x + epsilon, x - epsilon
3535
# Is it better to do 2 * epsilon or xplusdx - xminusdx?
3636
return (f(xplusdx) - f(xminusdx)) / (2 * epsilon)

0 commit comments

Comments
 (0)