// Spline.java // Functions for setting up and evaluating a cubic interpolatory spline. // AUTHORS: Lawrence Shampine, Richard Allen, Steven Pruess for // the text Fundamentals of Numerical Computing // DATE: February 27, 1996 // minimal change convertion to Java August 11, 2003 import java.text.*; public class Spline { int n, last_interval; double x[], f[], b[], c[], d[]; boolean uniform, debug; public Spline(double xx[], double ff[]) { // Calculate coefficients defining a smooth cubic interpolatory spline. // // Input parameters: // xx = vector of values of the independent variable ordered // so that x[i] < x[i+1] for all i. // ff = vector of values of the dependent variable. // class values constructed: // n = number of data points. // b = vector of S'(x[i]) values. // c = vector of S"(x[i])/2 values. // d = vector of S'''(x[i]+)/6 values (i < n). // x = xx // f = ff // Local variables: double fp1, fpn, h, p; final double zero = 0.0, two = 2.0, three = 3.0; DecimalFormat f1 = new DecimalFormat("00.00000"); boolean sorted = true; uniform = true; debug = false; last_interval = 0; n = xx.length; if(n<=3) { System.out.println("not enough points to build spline, n="+n); return; } if(n!=ff.length) { System.out.println("not same number of x and f(x)"); return; } x = new double[n]; f = new double[n]; b = new double[n]; c = new double[n]; d = new double[n]; for(int i=0; i1.0E-5) uniform = false; c[i] = (f[i+1]-f[i])/b[i]; d[i] = two*(b[i]+b[i-1]); } d[n-1] = two*b[n-2]; // Calculate estimates for the end slopes. Use polynomials // interpolating data nearest the end. fp1 = c[0]-b[0]*(c[1]-c[0])/(b[0]+b[1]); if(n>3) fp1 = fp1+b[0]*((b[0]+b[1])*(c[2]-c[1])/ (b[1]+b[2])-c[1]+c[0])/(x[3]-x[0]); fpn = c[n-2]+b[n-2]*(c[n-2]-c[n-3])/(b[n-3]+b[n-2]); if(n>3) fpn = fpn+b[n-2]*(c[n-2]-c[n-3]-(b[n-3]+ b[n-2])*(c[n-3]-c[n-4])/(b[n-3]+b[n-4]))/(x[n-1]-x[n-4]); // Calculate the right-hand-side and store it in c. c[n-1] = three*(fpn-c[n-2]); for(int i=n-2; i>0; i--) c[i] = three*(c[i]-c[i-1]); c[0] = three*(c[0]-fp1); // Solve the tridiagonal system. for(int k=1; k=0; k--) c[k] = (c[k]-b[k]*c[k+1])/d[k]; // Calculate the coefficients defining the spline. h = x[1]-x[0]; for(int i=0; ix[n-1]) { System.out.println("requested point above Spline region"); return 0.0; // should throw exception } if(t>x[n-2]) interval = n-2; else if (t >= x[last_interval]) for(int j=last_interval; j=x[j]; j++) interval = j; else for(int j=last_interval; t