This afternoon I was looking around for a cubic interpolation routine to do some data analysis. The numerical recipes one works fine but I also found a nice library from the website of Tino Kluge which works (so far anyway) very nicely indeed. It is as simple as including a header file, defining a set of (sorted) [latex]x_i[/latex] and their corresponding [latex]y_i[/latex] (for example using the vector class from the STL), declaring an instance of that class and calling the classes routine “set_points”. Below is the example provided on the authors website.

#include <cstdio> #include <cstdlib> #include <vector> #include "spline.h" int main(int argc, char** argv) { std::vector<double> X(5), Y(5); X[0]=0.1; X[1]=0.4; X[2]=1.2; X[3]=1.8; X[4]=2.0; Y[0]=0.1; Y[1]=0.7; Y[2]=0.6; Y[3]=1.1; Y[4]=0.9; tk::spline s; s.set_points(X,Y); // currently it is required that X is already sorted double x=1.5; printf("spline at %f is %f\n", x, s(x)); return EXIT_SUCCESS;

The header is available from the authors website at the following link along with a some example programs and the explanation of the mathematics (see here). Many thanks to the author for providing it under the GNU GPLv2 licence.

Math symbols are rendered by MathJax which requires JavaScript.