# Simple numerical differentiation

**URL:** <https://discourse.julialang.org/t/simple-numerical-differentiation/52254>\
**Category:** New to Julia\
**Tags:** question, forwarddiff, finitediff\
**Created:** [December 23, 2020, 2:55am UTC](https://discourse.julialang.org/t/simple-numerical-differentiation/52254 "2020-12-23T02:55:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishihama](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@ishihama](https://discourse.julialang.org/u/ishihama)\
**Post date:** [December 23, 2020, 2:55am UTC](https://discourse.julialang.org/t/simple-numerical-differentiation/52254/1 "2020-12-23T02:55:31Z")

</div>

Hello there, I am wondering how to implement numerical differentiation in Julia simply. My MWE looks like,

```julia
using ForwardDiff
p = [1, 2, 3]
f(x::Vector) = p[1] .+ p[2] .* x .+ p[3] .* x .^ 2
x=1:10
g = x -> ForwardDiff.gradient(f, x);
g(x)

```

The result looks like `Array{Array{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f),Float64},Float64,11},1},1}`.

I wish to know a simple differentiation option, not gradient. Note that my question is not  
related to analytical differentiation where `SymPy` or Mathematica should be used  
despite simple polynomial used in my MWE. I would like to numerically differentiate  
the function `f`. Searching this topic would result in many ODE how-to’s, not the case  
for me. Any idea? Thanks in advance…

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [December 23, 2020, 4:02am UTC](https://discourse.julialang.org/t/simple-numerical-differentiation/52254/2 "2020-12-23T04:02:38Z")

</div>

Try [https://github.com/JuliaDiff/FiniteDiff.jl](https://github.com/JuliaDiff/FiniteDiff.jl)

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [December 23, 2020, 4:51am UTC](https://discourse.julialang.org/t/simple-numerical-differentiation/52254/3 "2020-12-23T04:51:20Z")

</div>

It’s slightly unclear what you’re trying to do in your example. If `g` is a function that takes a single argument (not a vector), you might use ForwardDiff to evaluate that on each element of `1:10` like this:

```julia
using ForwardDiff
const p = [1,2,3]
f(x) = p[1] + p[2]*x + p[3]*x^2
g(x) = ForwardDiff.gradient(f, x)
map(g, 1:10)

```

using a finite difference package (here using [FiniteDifferences.jl](https://github.com/JuliaDiff/FiniteDifferences.jl)), it might look like this:

```julia
using FiniteDifferences
const fd = central_fdm(5,1) 
g(x) = fd(f, x) # using f from above snippet
map(g, 1:10)

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [December 23, 2020, 10:16am UTC](https://discourse.julialang.org/t/simple-numerical-differentiation/52254/4 "2020-12-23T10:16:34Z")

</div>

You might be looking for `ForwardDiff.gradient(sum∘f, 1:10)`. In general `gradient(h, y)` expects that `h(y)` is a number, and `y` is a vector (or some other array). (`ForwardDiff.derivative` is for real → real functions, and `ForwardDiff.jacobian` for vector → vector.)

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [December 23, 2020, 10:21am UTC](https://discourse.julialang.org/t/simple-numerical-differentiation/52254/5 "2020-12-23T10:21:11Z")

</div>

Note that what `ForwardDiff` package offers is not numerical differentiation. There is now a trio of approaches to differentiation:

- symbolic: `Sympy`, …
- numerical: `FiniteDifferences`, `FiniteDiff`, …
- [algorithmic](https://en.wikipedia.org/wiki/Automatic_differentiation): `ForwardDiff`, `Yota`, `Zygote`, `ReverseDiff`,…

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 23, 2020, 10:26am UTC](https://discourse.julialang.org/t/simple-numerical-differentiation/52254/6 "2020-12-23T10:26:10Z")

</div>

```julia
using FiniteDiff
p = [1, 2, 3]
f(x::Vector) = p[1] .+ p[2] .* x .+ p[3] .* x .^ 2
x=1:10
g = x -> FiniteDiff.finite_difference_gradient(f, x);
g(x)

```
