Michael Thomas Flanagan's Java Scientific Library

BiCubicInterpolation Class:     Bicubic Interpolation

     

Last update: 11 January 2011                                                                                                                              Main Page of Michael Thomas Flanagan's Java Scientific Library

This class contains the constructor and methods for performing an interpolation within a two dimensional array of data points, y = f(x1,x2), using the following bicubic interpolation:

The coefficients cij are calculated for each nearest neighbour four point square in the entered grid of data points and z1 and z2 are the normalized values of x1 and x2 for a unit square.

This method requires the derivatives ∂y/∂x1, ∂y/∂x2 and 2y/∂x1∂x2 for each entered grid point. If the values of these are known they may be entered with the y, x1 and x2 data. If the values of these derivatives are not entered they are calculated by numerical differencing. Two numerical differencing methods are offered. The differencing may be performed using either the entered data points or using interpolated values close to the entered points after a cubic spline interpolation on the entered data points.

See BiCubicSpline for a bicubic spline interpolation.

import directive: import flanagan.interpolation.BiCubicInterpolation;
Constructor public BiCubicInterpolation(double[] x1, double[] x2, double[][] y, double[][] dydx1, double[][] dydx2,double[][] d2ydx1dx2)
public BiCubicInterpolation(double[] x1, double[] x2, double[][] y, int numerDiffOption)
Interpolate public double interpolate(double xx1, double xx2)
Return the interpolated gradients public double[] getInterpolatedValues()
Grid point gradients public double[][] getGridDydx1()
public double[][] getGridDydx2()
public double[][] getGridD2ydx1dx2()
Reset numerical differencing increment public static void resetDelta(double delta)
Rounding Error Options public static void noRoundingErrorCheck()
public static void potentialRoundingError(double potentialRoundingError)



CONSTRUCTOR

CONSTRUCTOR
public BiCubicInterpolation(double[] x1, double[] x2, double[][] y, double[][] dydx1, double[][] dydx2,double[][] d2ydx1dx2)
public BiCubicInterpolation(double[] x1, double[] x2, double[][] y, int numerDiffOption)
Usage:                      BiCubicSInterpolation aa = new BiCubicInterpolation(x1, x2, y, dydx1, dydx2, d2ydx1dx2);
Creates an instance of BiCubicInterpolation with its internal data arrays initialised to copies of the values in the x1, x2, y , dydx1, dydx2 and d2ydx1dx2 arrays where y is the tabulated function y = f(x1,x2), dydx1 is ∂y/∂x1, dydx2 is ∂y/∂x2 and d2ydx1dx2 is 2y/∂x1∂x2. The y, dydx1, dydx2 and d2ydx1dx2 elements are ordered as {{f(x1[0],x2[0], . . . f(x1[0],x2[m-1]} {f(x1[1],x2[0] . . . f(x1[1],x2[m-1]} ......{f(x1[n-1],x2[0], . . . f(x1[n-1],x2[m-1]} } etc. where m is the number of x1 values and n is the number of x2 values, i.e. as in the order of the following nested for loops if you were reading in or calculating the tabulated data:
for(int i=0; i<x1.length; i++){
       for(int j=0; j<x2.length; j++){
              y[i][j] = 'read statement' or 'calculation of f(x1,x2)';
       }
}
and similarly for dydx1, dydx2 and d2ydx1dx2.

Usage:                      BiCubicInterpolation aa = new BiCubicInterpolation(x1, x2, y, numerDiffOption);
Creates an instance of BiCubicInterpolation with its internal data arrays initialised to copies of the values in the x1, x2 and y arrays where y is the tabulated function y = f(x1,x2). The variables x1 and x2 are are one dimensional arrays of type double and y is a two dimensional array of type double. The y elements are ordered as {{f(x1[0],x2[0], . . . f(x1[0],x2[m-1]} {f(x1[1],x2[0] . . . f(x1[1],x2[m-1]} ......{f(x1[n-1],x2[0], . . . f(x1[n-1],x2[m-1]} } where m is the number of x1 values and n is the number of x2 values, i.e. as in the order of the following nested for loops if you were reading in or calculating the tabulated data:
for(int i=0; i<x1.length; i++){
       for(int j=0; j<x2.length; j++){
              y[i][j] = 'read statement' or 'calculation of f(x1,x2)';
       }
}
The derivatives ∂y/∂x1, ∂y/∂x2 and 2y/∂x1∂x2 are calculated by numerical differencing. Two numerical differencing options are available:
  1. numerDiffOption = 0
    The gradients are calculated using only the supplied data points, i.e. as,

  2. numerDiffOption = 1
    The gradients are calculated using a bicubic spline interpolation, i.e. as,

    where h1 and h1 are calculated as:
     h1 = delta times (maximum value of x1 - minimum value of x1)
     h2 = delta times (maximum value of x2 - minimum value of x2)
    The default value of delta is 1 x 10-3.
    The values of f(x1+h1, x2), f(x1-h1, x2), f(x1, x2+h1), f(x1, x2-h1), f(x1+h1, x2+h1), f(x1+h1, x2-h1), f(x1-h1, x2+h1) and f(x1-h1, x2-h1) are calculated by interpolation of a bicubic spline of the entered data. If h1 or h2 are greater than the mean separation of the relevant data points hx is replaced by the minimimum separation of the relevant data points.
    See below for details of how the value of delta may be reset.



METHODS

INTERPOLATION
public double interpolate(double xx1, double xx2)
Usage:                      y1 = aa.interpolate(xx1, xx2);
Returns the interpolated value of y, y1, for given values of xx1 and xx2, using the y = f(x1,x2) data entered via the constructor. This method may be called as often as required. The coefficients and derivatives needed for the interpolation are calculated and stored on instantiation so that they need not be recalculated on each call to this method. This method throws an IllegalArgumentException if xx1 or xx2 is outside the range of the values of x1[] or x2[] supplied to the constructor.



GET THE INTERPOLATED GRADIENTS
public double[] getInterpolatedValues()
Usage:                      values = aa.getInterpolatedValues();
Returns an array of six doubles which contains
for the last interpolation performed on calling the interpolate(xx1, xx2) method.



GET THE GRID POINT GRADIENTS
public double[][] getGridDydx1()
public double[][] getGridDydx2()
public double[][] getGridD2ydx1dx2()
Usage:                      grad1 = aa.getGridDydx1();
This method returns the gradients, ∂y/∂x1, for each grid point, that were either entered via the constructor or calculated by the program (see below).

Usage:                      grad2 = aa.getGridDydx2();
This method returns the gradients, ∂y/∂x2, for each grid point, that were either entered via the constructor or calculated by the program (see below).

Usage:                      grad2 = aa.getGridD2ydx1dx2();
This method returns the gradients, 2y/∂x1∂x2, for each grid point, that were either entered via the constructor or calculated by the program (see below).



RESET THE NUMERICAL DIFFERENCING FACTOR, delta
public static void resetDelta(double delta)
Usage:                      BicubicInterpolation.resetDelta();
If the gradients have not been entered via the constructer and the numerical differencing method using interpolation has been chosen the gradients are calculated as:

where h1 and h1 are calculated as:
 h1 = delta times (maximum value of x1 - minimum value of x1)
 h2 = delta times (maximum value of x2 - minimum value of x2)
The default value of delta is 1 x 10-3.
The values of f(x1+h1, x2), f(x1-h1, x2), f(x1, x2+h1), f(x1, x2-h1), f(x1+h1, x2+h1), f(x1+h1, x2-h1), f(x1-h1, x2+h1) and f(x1-h1, x2-h1) are calculated by interpolation of a bicubic spline of the entered data. If h1 or h2 are greater than the mean separation of the relevant data points hx is replaced by the minimimum separation of the relevant data points.
This method allows the value of delta to be reset. If delta is to be reset this static method must be called before BicubicInterpolation is instantiated.



ROUNDING ERROR OPTIONS
public static void noRoundingErrorCheck()
Usage:                      BiCubicSpline.noRoundingErrorCheck();
Seveveral applications that call BiCubicSpline, e.g. plotting programs, may calculate an array of points to be fed to an instance of cubic spline that should lie between the limits initially supplied to the instance but which, due to rounding errors in the calculation of the array, may have extreme values that lie outside the limits by the an amount equal to the rounding error, e.g. an array that should lie between limits of 0 and 4 may run from 0 to 4.0000000000000003. The default option of the BiCubicSpline class is to check for such violations of the order of a rounding error and round the extreme value to the limit thus preventing an out of range exception being thrown. This method allows this default option to be ignored so that an out of range exception is thrown if any value lies outside he range of the limis no matter how small the violation is.

public static void potentialRoundingError(double potentialRoundingError)
Usage:                      BiCubicSpline.potentialRoundingError(potentialRoundingError);
Seveveral applications that call BiCubicSpline, e.g. plotting programs, may calculate an array of points to be fed to an instance of cubic spline that should lie between the limits initially supplied to the instance but which, due to rounding errors in the calculation of the array, may have extreme values that lie outside the limits by the an amount equal to the rounding error, e.g. an array that should lie between limits of 0 and 4 may run from 0 to 4.0000000000000003. The default option of the BiCubicSpline class is to check for such violations of the order of a rounding error and round the extreme value to the limit thus preventing an out of range exception being thrown. The default calculculation of the potential rounding error is the multiplication of 5x10-15 by 10 raised to the exponent of the value lying outside the limits. This method allows the value of 5x10-15 to be reset to the user's choice, potentialRoundingError.



EXAMPLE PROGAMS

BiCubicExampleOne

This program uses calculated data and compares the interpolated y values obtained using This example program may be found on BiCubicExampleOne.java

BiCubicExampleTwo

This simple program uses both BiCubicInterpolation and BiCubicSpline to demonstrate interpolation within a data set of the refractive indices of water tabulated as a function of both wavelength and temperature. This example program may be found on BiCubicExampleTwo.java



OTHER CLASSES USED BY THIS CLASS

This class uses the following classes in this library:


This page was prepared by Dr Michael Thomas Flanagan