# Differentiation without explicit function (np.gradient)

**URL:** <https://discourse.julialang.org/t/differentiation-without-explicit-function-np-gradient/57784>\
**Category:** New to Julia\
**Tags:** differentiation, gradient\
**Created:** [March 23, 2021, 1:06pm UTC](https://discourse.julialang.org/t/differentiation-without-explicit-function-np-gradient/57784 "2021-03-23T13:06:03Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 23, 2021, 1:27pm UTC](https://discourse.julialang.org/t/differentiation-without-explicit-function-np-gradient/57784/2 "2021-03-23T13:27:25Z")

</div>

One nice option is to use Interpolations.jl to create a piecewise linear interpolation and then ask for its derivatives:

```julia
julia> using Interpolations

julia> x = [1, 2, 4];

julia> y = [1, 2, 3];

julia> itp = interpolate((x,), y, Gridded(Linear()));

```

You can now use `itp` to interpolate values:

```julia
julia> itp(3.0)
2.5

```

and compute derivatives:

```julia
julia> Interpolations.gradient(itp, 3.0)
1-element StaticArrays.SArray{Tuple{1},Float64,1,1} with indices SOneTo(1):
 0.5

```

Note that this gives you a gradient vector, but you can get the scalar derivative by taking its only element:

```julia
julia> only(Interpolations.gradient(itp, 3.0))
0.5

```

and you can use broadcasting to get multiple derivatives at different x values:

```julia
julia> Interpolations.gradient.(Ref(itp), [1.0, 2.0, 3.0])
3-element Array{StaticArrays.SArray{Tuple{1},Float64,1,1},1}:
 [1.0]
 [1.0]
 [0.5]

```

or as scalars:

```julia
julia> only.(Interpolations.gradient.(Ref(itp), [1.0, 2.0, 3.0]))
3-element Array{Float64,1}:
 1.0
 1.0
 0.5

```

---

_[View the full topic](https://discourse.julialang.org/t/differentiation-without-explicit-function-np-gradient/57784)._
