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

**URL:** <https://discourse.julialang.org/t/what-is-the-dx-used-in-finite-difference-approximation-in-calculus-jl/5773>\
**Category:** Numerics\
**Tags:** differentiation\
**Created:** [September 7, 2017, 11:33pm UTC](https://discourse.julialang.org/t/what-is-the-dx-used-in-finite-difference-approximation-in-calculus-jl/5773 "2017-09-07T23:33:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vgdev](https://avatars.discourse-cdn.com/v4/letter/v/47e85d/32.png) [@vgdev](https://discourse.julialang.org/u/vgdev)\
**Post date:** [September 7, 2017, 11:33pm UTC](https://discourse.julialang.org/t/what-is-the-dx-used-in-finite-difference-approximation-in-calculus-jl/5773/1 "2017-09-07T23:33:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [September 8, 2017, 12:09am UTC](https://discourse.julialang.org/t/what-is-the-dx-used-in-finite-difference-approximation-in-calculus-jl/5773/2 "2017-09-08T00:09:43Z")

</div>

It depends on the rule:  
[https://github.com/johnmyleswhite/Calculus.jl/blob/master/src/finite\_difference.jl](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](https://github.com/JuliaDiffEq/DiffEqDiffTools.jl)  
Complex step finite differencing is really cool!

---

<div class="post-metadata">

**Author:** ![vgdev](https://avatars.discourse-cdn.com/v4/letter/v/47e85d/32.png) [@vgdev](https://discourse.julialang.org/u/vgdev)\
**Post date:** [September 8, 2017, 8:56am UTC](https://discourse.julialang.org/t/what-is-the-dx-used-in-finite-difference-approximation-in-calculus-jl/5773/3 "2017-09-08T08:56:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ohsonice](https://avatars.discourse-cdn.com/v4/letter/o/d6d6ee/32.png) [@ohsonice](https://discourse.julialang.org/u/ohsonice)\
**Post date:** [September 8, 2017, 4:29pm UTC](https://discourse.julialang.org/t/what-is-the-dx-used-in-finite-difference-approximation-in-calculus-jl/5773/4 "2017-09-08T16:29:58Z")

</div>

It takes a optional parameter `dtype`.

Here’s a basic example.

```julia
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.
