# How to interpolate a scalar field?

**URL:** <https://discourse.julialang.org/t/how-to-interpolate-a-scalar-field/124499>\
**Category:** General Usage\
**Tags:** fit, interpolations\
**Created:** [January 7, 2025, 1:36pm UTC](https://discourse.julialang.org/t/how-to-interpolate-a-scalar-field/124499 "2025-01-07T13:36:39Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [January 7, 2025, 3:18pm UTC](https://discourse.julialang.org/t/how-to-interpolate-a-scalar-field/124499/2 "2025-01-07T15:18:20Z")

</div>

> [@Valery](#):
>
> For instance, getting the coefficients of the x^n, y^m, x^n y^m would be fine, where n and m are integers.

If you want to fit a multivariate polynomial, it’s just linear equations, which are trivial to set up and solve in Julia. For example, if you want to fit to z = a\_1 + a\_2 x + a\_3 y + a\_4 x y + a\_5 x^2 + a\_6 y^2 you could do a least-square fit with:

```julia
a = [x.^0 x y x.*y x.^2 y.^2] \ z

```

(The matrix `[...]` here is a generalization of the [Vandermonde matrix](https://en.wikipedia.org/wiki/Vandermonde_matrix) that appears in univariate polynomial fitting: each column corresponds to one basis function.) If the number of basis functions (columns) equals `length(z)`, then this becomes interpolation, i.e. it will exactly go through the given points.

You can also do an equivalent fit more easily and generally with FastChebInterp.jl:

```julia
using FastChebInterp, StaticArrays
c = chebregression(SVector.(x, y), z, (2,2))

```

which represents the polynomial (here of degree 2 in both x and y) in the basis of Chebyshev polynomials, and you can then just evaluate it at any given point with `c([x,y])` or `c(SVector(x,y))`.

However, you have to be careful with polynomial fitting if you go to high degree compared to the number of data points (unless the function is smooth and the points are carefully selected, e.g. using Chebyshev points via `chebpoints` and `chebinterp`), since high-degree polynomials can go crazy in between the fitted points if you are not careful.

Other options include radial basis functions (e.g. Surrogates.jl or ScatteredInterpolation.jl, both of which implement several algorithms), splines (e.g. Dierckx.jl can do splines from irregular 2d points), dividing your points into a triangular mesh (e.g. Delaunay triangulation) and doing bi-linear interpolation within each triangle, and more.

> [@Valery](#):
>
> I have read several post about interpolation in Julia, but they seem to focus purely on linear interpolation of function (and not field).

See e.g. [2D Interpolation on an irregular grid](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-interpolate-a-scalar-field/124499)._
