# Interpolation of function with multiple variables

**URL:** <https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640>\
**Category:** New to Julia\
**Tags:** interpolations, interpolationsjl\
**Created:** [July 30, 2024, 3:58pm UTC](https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640 "2024-07-30T15:58:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dilaraabdel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilaraabdel/32/210999_2.png) [@dilaraabdel](https://discourse.julialang.org/u/dilaraabdel)\
**Post date:** [July 30, 2024, 3:58pm UTC](https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640/1 "2024-07-30T15:58:46Z")

</div>

Dear all,

I am trying to interpolate a function f: R^2 → R with Interpolations.jl.

But as far as I understand [the documentation](http://juliamath.github.io/Interpolations.jl/stable/iterate/#Multiple-Dimensions) this is only possible for functions from R^n to R^n.

Are you aware of a way to build an interpolation?

Thank you so much!

---

<div class="post-metadata">

**Author:** ![JoshuaLampert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshualampert/32/205964_2.png) [@JoshuaLampert](https://discourse.julialang.org/u/JoshuaLampert)\
**Post date:** [July 30, 2024, 4:52pm UTC](https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640/2 "2024-07-30T16:52:00Z")

</div>

Hi! There are multiple packages than can do multivariate interpolations. You can have a look at the following thread: [3D interpolation in Julia](https://discourse.julialang.org/t/3d-interpolation-in-julia/117035). To name some you might want to look at: Dierckx.jl, GridInterpolations.jl, GMT.jl, DIVAnd.jl, RadialBasisFunctions.jl, ScatteredInterpolation.jl, KernelInterpolation.jl. Which of these is suited the best for your use case depends on how your data looks like, especially the location of your data points in \mathbb{R}^2 (e.g., grid data or scattered data). If you have any questions about one (or multiple) of these packages, feel free to ask.

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [July 30, 2024, 4:55pm UTC](https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640/3 "2024-07-30T16:55:40Z")

</div>

If your data is scattered, NaturalNeighbours.jl may be of interest.

See also [Other Interpolation Packages · Interpolations.jl](https://docs.juliahub.com/General/Interpolations/stable/other_packages/)

---

<div class="post-metadata">

**Author:** ![JoshuaLampert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshualampert/32/205964_2.png) [@JoshuaLampert](https://discourse.julialang.org/u/JoshuaLampert)\
**Post date:** [July 30, 2024, 5:00pm UTC](https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640/5 "2024-07-30T17:00:42Z")

</div>

Yes, but I think the person asking the question wants to interpolate 2D data.

---

<div class="post-metadata">

**Author:** ![jgr](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@jgr](https://discourse.julialang.org/u/jgr)\
**Post date:** [July 30, 2024, 5:15pm UTC](https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640/6 "2024-07-30T17:15:05Z")

</div>

Sorry, I posted a similar question and I thought you were replying to that…

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 30, 2024, 5:18pm UTC](https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640/7 "2024-07-30T17:18:37Z")

</div>

Please check this simple example:

```julia
# R² -> R
using Interpolations
f(x, y) = 2x - 5y
x = y = -10:5.0:10
v = f.(x, y')
itp = interpolate((x,y), v, Gridded(Linear()))
itp(1.0, 2.0) ≈ f(1.0, 2.0) # true (= -8.0)

```

---

<div class="post-metadata">

**Author:** ![dilaraabdel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilaraabdel/32/210999_2.png) [@dilaraabdel](https://discourse.julialang.org/u/dilaraabdel)\
**Post date:** [July 31, 2024, 6:56am UTC](https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640/8 "2024-07-31T06:56:59Z")

</div>

Dear @JoshuaLampert, @rafael.guerra thank you for your answers! Maybe this minimal example helps:

```julia
# x coord; dim = n x 1
X = [0.0 1.0 3.0 0.0 1.0 3.0 0.0 1.0 3.0] 
# y coord; dim = n x 1
Y = [0.0 0.0 0.0 1.0 1.0 1.0 3.0 3.0 3.0] 
# PDE sol; dim = n x 1
sol = [0.5 1.0 1.5 1.0 0.5 1.0 1.5. 2.0 0.5] 

```

(X[i], Y[i]) is a node in a grid and sol[i] is the solution at (X[i], Y[i]).  
I want to build now an interpolation of the vector sol on the whole domain.

AFAIU for the answers and the other threads, I need a a n x n matrix for sol?

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [July 31, 2024, 7:06am UTC](https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640/9 "2024-07-31T07:06:18Z")

</div>

If your data is from a grid, you will get the most efficiency from providing `sol` as a matrix that conforms to the grid. Do you have data on the entire grid, or do you need a scattered interpolant?

If you do actually want to interpolate with data like from your example, you can do e.g.

```julia
julia> using NaturalNeighbours
julia> X = [0.0 1.0 3.0 0.0 1.0 3.0 0.0 1.0 3.0] |> vec;
julia> Y = [0.0 0.0 0.0 1.0 1.0 1.0 3.0 3.0 3.0] |> vec;
julia> sol = [0.5 1.0 1.5 1.0 0.5 1.0 1.5 2.0 0.5] |> vec;
julia> itp = interpolate(X, Y, sol); # also see ?interpolate for some keywords
julia> itp(0.2, 0.3)
0.69
julia> itp([0.5, 0.2, 0.3], [0.1, 0.2, 0.9]; method = Laplace()) # see ?interpolate
3-element Vector{Float64}:
 0.75
 0.66
 0.8300000000000001

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 31, 2024, 7:21am UTC](https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640/10 "2024-07-31T07:21:06Z")

</div>

For your data example, you could reshape the input as follows:

```julia
using Interpolations

X = [0.0 1.0 3.0 0.0 1.0 3.0 0.0 1.0 3.0]
Y = [0.0 0.0 0.0 1.0 1.0 1.0 3.0 3.0 3.0]
sol = [0.5 1.0 1.5 1.0 0.5 1.0 1.5 2.0 0.5]

x, y = unique(X), unique(Y)
v = reshape(sol, length(x),length(y))
itp = interpolate((x,y), v, Gridded(Linear()))
itp(0.5, 2.0) # 1.25

```

_NB:_  
_The x axis is along the vertical columns of `v` and the y-axis along the rows._

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 31, 2024, 11:26am UTC](https://discourse.julialang.org/t/interpolation-of-function-with-multiple-variables/117640/11 "2024-07-31T11:26:18Z")

</div>

> [@dilaraabdel](#):
>
> `# PDE sol; dim = n x 1`

> [@dilaraabdel](#):
>
> I want to build now an interpolation of the vector sol on the whole domain.

If you are trying to interpolate a smooth function f(x,y), e.g. the solution of a PDE as a function of some parameters, and you have the freedom to _choose_ the x,y points, then you will probably be much better off choosing the x,y points to lie on a Chebyshev grid and then use Chebyshev interpolation, which converges exponentially fast for smooth functions. See [FastChebInterp.jl](https://github.com/JuliaMath/FastChebInterp.jl).

On the other hand if your x,y points come from how you solve the PDE, e.g. a finite-difference grid, then you should choose your interpolation based on what computational method you used. e.g. if you used 2nd-order centered differences then you should just use bilinear interpolation, and if you used finite elements you should use the FEM basis functions.
