2-dimensional interpolation
I want to find a value of z at y = 12 and x = 3.5, given the below example data. How can I do this in C++?
y = 10
x = [1,2, 3,4, 5,6]
z = [2.3, 3.4, 5.6, 7.8, 9.6, 11.2]
y = 20
x = [1,2, 3,4, 5,6]
z = [4.3, 5.4, 7.6, 9.8, 11.6, 13.2]
y = 30
x = [1,2, 3,4, 5,6]
z = [6.3, 7.4, 8.6, 10.8, 13.6, 15.2]
开发者_Python百科
My current Python code:
import scipy
import math
import numpy
from scipy import interpolate
x = [1, 2, 3, 4, 5, 6]
y = [10, 20, 30]
Y = numpy.array([[i]*len(x) for i in y])
X = numpy.array([x for i in y])
Z = numpy.array([[2.3, 3.4, 5.6, 7.8, 9.6, 11.2],
[4.3, 5.4, 7.6, 9.8, 11.6, 13.2],
[6.3, 7.4, 8.6, 10.8, 13.6, 15.2]])
tck = interpolate.bisplrep(X, Y, Z)
print interpolate.bisplev(3.5, 15, tck)
Just do the interpolation twice. First interpolate with Y to select the two Z tables. Then interpolate with X to pick the Z value.
I would use Akima's Spline, which is very well-tested, very fast, and produces extremely good results.
Unfortunately, it's in Fortran-66 (and messy at that), so you'll need to either translate it to C or a more modern variant of Fortran. I've gotten some help in getting it all to work, so I'd suggest reading through my threads on Usenet.
精彩评论