# How to do a 2d interpolation?

How can I do a 2D interpolation on a regular grid?
This works (1D):

using PyPlot, Interpolations

TSR = 2.0:1.0:10.0
TSR_fine = 2.0:0.01:10.0

CP1 = sin.(TSR)
plot(TSR, CP1)

itp_cubic1 = cubic_spline_interpolation(TSR, CP1)
Cp1(λ) =  itp_cubic1(λ)

plot(TSR_fine, Cp1.(TSR_fine))

Here creating the interpolation object fails:

using PyPlot, Interpolations

TSR = 2.0:0.25:10.0
Pitch = -5.0:0.5:20.0
TSR_fine = 2.0:0.01:10.0

CP = zeros(length(Pitch), length(TSR))
for (i, pitch) in pairs(Pitch), (j, tsr) in pairs(TSR)
println(i, ": ", pitch, ", ", j, ": ", tsr)
CP[i, j] = sin(tsr) + 0.25*cos(pitch)
end

plot_surface(TSR, Pitch, CP)
xlabel("TSR")
ylabel("Pitch angle")
zlabel("Cp")

itp_cubic = cubic_spline_interpolation(TSR, Pitch, CP)

It is not clear to me from the documentation which parameters the function cubic_spline_interpolation() expects.

itp_cubic = cubic_spline_interpolation((Pitch, TSR), CP)

?

1 Like

Good idea! This seams to work:

itp_cubic = cubic_spline_interpolation((Pitch, TSR), CP)

No such example in the documentation…

No such example in the documentation…

This is close to the multidimensional data example.

One important thing to remember is that julia is column-major,
so the first axis (x) is vertical.
The nice thing is that you just give the arguments in same order as in the for loop.

Other options GMT.jl gridit