/* This example of the use of both BiCubicInterpolation and BiCubicSpline demonstrates interpolation within a calculated data set y = x + 2.x^2 - 3.x^3 allowing a comparison of the use of gradients calculated by numerical differentiation Michael Thomas Flanagan Created: May 2003 Updated: 1 May 2005, 15 January 2011 */ import flanagan.interpolation.*; public class CubicExampleOne{ public static void main(String arg[]){ // Array of x double[] x = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0}; double[] y = new double[6]; // Array of y double[] dydx = new double[6]; // Array of gradients, dy/dx double xx = 0.0; // x value at interpolation point double y1 = 0.0; // interpolated y value using BiCubicInterpolation with user supplied // analytically calculated gradients double y2 = 0.0; // interpolated y value using BiCubicInterpolation with // numerical differencing using supplied data points double y3 = 0.0; // interpolated y value using BiCubicInterpolation with // numerical differencing using interpolation double y4 = 0.0; // interpolated y value using BiCubicSpline double yt = 0.0; // true y value // Calculate the data for(int i=0; i<6; i++){ y[i] = x[i] + 2.0*x[i]*x[i] - 3.0*x[i]*x[i]*x[i]; dydx[i] = 1.0 + 4.0*x[i] - 9.0*x[i]*x[i]; } // Create an instance of CubicInterpolation with supplied analytically determined derivatives CubicInterpolation ci1 = new CubicInterpolation(x, y, dydx); // Create an instance of CubicInterpolation with calculated derivatives // using supplied data points CubicInterpolation ci2 = new CubicInterpolation(x, y, 0); // Create an instance of CubicInterpolation with calculated derivatives // using numerical differencing and interpolation CubicInterpolation ci3 = new CubicInterpolation(x, y, 1); // Create an instance of CubicSpline CubicSpline cs = new CubicSpline(x, y); // interpolation xx = 0.5; for(int i=0; i<5; i++){ yt = xx + 2.0*xx*xx - 3.0*xx*xx*xx; System.out.println("Interpolation at x = " + xx); System.out.println("The true y value is " + yt); y1 = ci1.interpolate(xx); System.out.println(" CubicInterpolation with supplied analytical gradients:"); System.out.println(" The interpolated y value is " + y1); y2 = ci2.interpolate(xx); System.out.println(" CubicInterpolation with numerical differencing of supplied data points:"); System.out.println(" The interpolated y value is " + y2); y3 = ci3.interpolate(xx); System.out.println(" CubicInterpolation with numerical differencing using interpolation:"); System.out.println(" The interpolated y value is " + y3); y4 = cs.interpolate(xx); System.out.println(" CubicSpline interpolation:"); System.out.println(" The interpolated y value is " + y4); System.out.println(" "); xx += 1.0; } } }