/* Example: spline interpolation using spline/seval (spline.cpp) AG: February 2007 */ #include #include #include #include "spline.cpp" using namespace std; double spline (double[], double[], double[], double[], double[], int); double seval (double[], double[], double[], double[], double[], int, double); int main () { const int n = 11; int i; double x[n] = {-1.0,-0.8,-0.6,-0.4,-0.2,0.0,0.2,0.4,0.6,0.8,1.0}; double y[n] = {0.0385,0.0588,0.1,0.2,0.5,1.0,0.5,0.2,0.1,0.0588,0.0385}; double b[n], c[n], d[n]; double u, s; cout.setf(ios::fixed | ios::showpoint); cout.precision(5); // interpolation for point u u = -0.33; // print arrays of x and y for interpolation i=0; cout << " original array for interpolation " << endl; cout << setw(5) << "point" << setw(12) << "x" << setw(12) << "y" << endl; while (i <= n-1) { cout << setw(5) << i << setw(12) << x[i] << setw(12) << y[i] << endl; i = i+1; } // call spline to calculate spline coeficients spline (x, y, b, c, d, n); // print spline coefficients (for curiosity) cout << endl << " spline coefficients for a curious mind" << endl; cout << setw(5) << "point" << setw(12) << "b" << setw(12) << "c" << setw(12)<< "d" << endl; i = 0; while (i <= n-1) { cout << setw(5) << i << setw(12) << b[i] << setw(12) << c[i] << setw(12)<< d[i] << endl; i = i+1; } // call seval for spline interpolation using coefficients calculated by spline() s = seval(x, y, b, c, d, n, u); // screen output cout << endl << " result of interpolation for point x " << endl; cout << endl << " x = " << u << " y = " << s << endl; system ("pause"); return 0; } /* Test output original array for interpolation point x y 0 -1.00000 0.03850 1 -0.80000 0.05880 2 -0.60000 0.10000 3 -0.40000 0.20000 4 -0.20000 0.50000 5 0.00000 1.00000 6 0.20000 0.50000 7 0.40000 0.20000 8 0.60000 0.10000 9 0.80000 0.05880 10 1.00000 0.03850 spline coefficients for a curious mind point b c d 0 0.11201 -0.21048 0.78958 1 0.12257 0.26327 0.76938 2 0.32020 0.72490 0.87041 3 0.71461 1.24714 13.39898 4 2.82135 9.28653 -54.46633 5 -0.00000 -23.39327 54.46633 6 -2.82135 9.28653 -13.39898 7 -0.71461 1.24714 -0.87041 8 -0.32020 0.72490 -0.76938 9 -0.12257 0.26327 -0.78958 10 -0.11201 -0.21048 -0.78958 result of interpolation for point x x = -0.33000 y = 0.26073 */