0

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]

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) 
VASUDEVAN
  • 101
  • 2
  • 6
  • 3
    There are many algorithms you could use. http://en.wikipedia.org/wiki/Multivariate_interpolation#2_dimensions Do you have a preference? – Mark Byers Jan 07 '10 at 22:53
  • no .. i have this implemented in python i have to implement it in c++ this is a sample i provided from my database my Y varies from 0 - 300 in steps of 10 my X from 1 - 20 and corresponding 20 values of data on Z axis Which one you suggest or how to go about it – VASUDEVAN Jan 07 '10 at 22:58
  • Show us your python implementation, and where you're stuck turning it into C++. – Bill Jan 07 '10 at 23:12
  • 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) – VASUDEVAN Jan 07 '10 at 23:38
  • THis is my python code I am starter at c++ so interpolate.bisplrep how to get it in C++ is the question – VASUDEVAN Jan 07 '10 at 23:41
  • Put the pythin code into the question so we can read it. – Martin York Jan 07 '10 at 23:49
  • Also, select the code and press the *01010* button to mark it as code and make it readable. – Georg Fritzsche Jan 07 '10 at 23:54

2 Answers2

1

Just do the interpolation twice. First interpolate with Y to select the two Z tables. Then interpolate with X to pick the Z value.

Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
1

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.

greyfade
  • 24,948
  • 7
  • 64
  • 80
  • see my list of Akima Spline implementations at http://stackoverflow.com/questions/3502769/akima-interpolation-of-an-array-of-doubles/4637884#4637884 – Handcraftsman Jan 09 '11 at 17:19