What is the dx used in finite difference approximation in Calculus.jl?

I tried to find what the value of \Delta x that is used in Calculus.jl for finite difference approximation (i.e. for a forward scheme \frac{f(x+\Delta x)-f(x)}{\Delta x }), without luck. Is there a way to retreive it myself, so that I can write my own customized FD approximation, or is it varied based on application?

1 Like

It depends on the rule:
https://github.com/johnmyleswhite/Calculus.jl/blob/master/src/finite_difference.jl
For example, the forward rule use sqrt(eps(eltype(x))) * max(one(eltype(x)), abs(x)).

You may also want to check out:
https://github.com/JuliaDiffEq/DiffEqDiffTools.jl
Complex step finite differencing is really cool!

1 Like

Thanks! Is there a way to specify what rule one would like to use in Calculus.jl? My experience is that if you want to get the gradient, then the central scheme is employed anyhow.

It takes a optional parameter dtype.

Here’s a basic example.

julia> using Calculus

julia> g(x) =  sin(x[1]) + cos(x[2])
g (generic function with 1 method)

julia> Calculus.finite_difference(g,[0,0])
2-element Array{Float64,1}:
 1.0
 0.0

julia> Calculus.finite_difference(g,[0,0],:central)
2-element Array{Float64,1}:
 1.0
 0.0

julia> Calculus.finite_difference(g,[0,0],:forward)
2-element Array{Float64,1}:
  1.0
 -7.45058e-9

You are right - the default method is central. I’d encourage you to look at the src file posted, it’s pretty easy to understand and modify for your needs.

Also the command methods(your_function_here) is often one of the first analysis tools I use when trying to understand a method.

1 Like