Skip to content

Commit 51d10e9

Browse files
committed
improve computation of epsilon in jacobian_fd
1 parent 9b51a54 commit 51d10e9

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/semidiscretization/semidiscretization.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,12 @@ function jacobian_fd(semi::AbstractSemidiscretization;
240240
# use second order finite difference to estimate Jacobian matrix
241241
for idx in eachindex(u0_ode)
242242
# determine size of fluctuation
243-
epsilon = sqrt(eps(typeof(u0_ode[idx])))
243+
# This is the approach used by FiniteDiff.jl to compute the
244+
# step size, which assures that the finite difference is accurate
245+
# for very small and very large absolute values `u0_ode[idx]`.
246+
absstep = sqrt(eps(typeof(u0_ode[idx])))
247+
relstep = absstep
248+
epsilon = max(relstep * abs(u0_ode[idx]), absstep)
244249

245250
# plus fluctuation
246251
u_ode[idx] = u0_ode[idx] + epsilon

0 commit comments

Comments
 (0)