# Derivative using Interpolations.jl failing

**URL:** <https://discourse.julialang.org/t/derivative-using-interpolations-jl-failing/71759>\
**Category:** General Usage\
**Tags:** interpolations\
**Created:** [November 19, 2021, 5:36am UTC](https://discourse.julialang.org/t/derivative-using-interpolations-jl-failing/71759 "2021-11-19T05:36:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vtomar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vtomar/32/18304_2.png) [@vtomar](https://discourse.julialang.org/u/vtomar)\
**Post date:** [November 19, 2021, 5:36am UTC](https://discourse.julialang.org/t/derivative-using-interpolations-jl-failing/71759/1 "2021-11-19T05:36:46Z")

</div>

I was trying to compute gradient of the points in the y-variable but it errors out:

```julia
using Interpolations
x = 1:6;
y = [1.0,2.0,3.0, 5.0, 10.0, 7.0];

itp = interpolate((x,), y, Gridded(Linear()));
only.(Interpolations.gradient.(Ref(itp), y))

```

Error:

```julia
ERROR: BoundsError: attempt to access 6-element interpolate((::Vector{Int64},), ::Vector{Float64}, Gridded(Linear())) with element type Float64 at index [10.0]
Stacktrace:
  [1] throw_boundserror(A::Interpolations.GriddedInterpolation{Float64, 1, Float64, Gridded{Linear{Throw{OnGrid}}}, Tuple{Vector{Int64}}}, I::Tuple{Float64})
    @ Base ./abstractarray.jl:651
  [2] gradient
    @ ~/.julia/packages/Interpolations/3gTQB/src/gridded/indexing.jl:20 [inlined]
  [3] _broadcast_getindex_evalf
    @ ./broadcast.jl:648 [inlined]
  [4] _broadcast_getindex
    @ ./broadcast.jl:621 [inlined]
  [5] getindex
    @ ./broadcast.jl:575 [inlined]
  [6] macro expansion
    @ ./broadcast.jl:984 [inlined]
  [7] macro expansion
    @ ./simdloop.jl:77 [inlined]
  [8] copyto!
    @ ./broadcast.jl:983 [inlined]
  [9] copyto!
    @ ./broadcast.jl:936 [inlined]
 [10] copy
    @ ./broadcast.jl:908 [inlined]
 [11] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(Interpolations.gradient), Tuple{Base.RefValue{Interpolations.GriddedInterpolation{Float64, 1, Float64, Gridded{Linear{Throw{OnGrid}}}, Tuple{Vector{Int64}}}}, Vector{Float64}}})
    @ Base.Broadcast ./broadcast.jl:883
 [12] top-level scope

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 19, 2021, 6:29am UTC](https://discourse.julialang.org/t/derivative-using-interpolations-jl-failing/71759/2 "2021-11-19T06:29:01Z")

</div>

You are trying to evaluate an interpolation on a grid ranging from 1 to 6 at the value 10 - ie you are extrapolating rather than interpolating. You need to specify an extrapolation when constructing the interpolant, see the example on the first page of the docs here

[https://juliamath.github.io/Interpolations.jl/stable/#Example-Usage](https://juliamath.github.io/Interpolations.jl/stable/#Example-Usage)
