Michael Thomas Flanagan's Java Scientific Library

CubicInterpolation Class:     Cubic Interpolation

     

Last update: 16 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 one dimensional array of data points, y = f(x), using the following cubic interpolation:

The coefficients ci are calculated for each two nearest neighbours in the entered line of data points and z is the normalized value of x for a unit line.

This method requires the derivative ∂y/∂x for each entered point. If the values of these are known they may be entered with the y and x data. If the values of these derivatives are not known 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 CubicSpline for a cubic spline interpolation.

import directive: import flanagan.interpolation.CubicInterpolation;
Constructor public CubicInterpolation(double[] x, double[] y, double[] dydx)
public CubicInterpolation(double[] x, double[] y, int numerDiffOption)
Interpolate public double interpolate(double xx)
Return the interpolated gradients public double[] getInterpolatedValues()
Grid point gradients public double[][] getGridDydx()
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 CubicInterpolation(double[] x, double[] y, double[] dydx)
public CubicInterpolation(double[] x, double[] y, int numerDiffOption)
Usage:                      CubicSInterpolation aa = new CubicInterpolation(x, y, dydx);
Creates an instance of CubicInterpolation with its internal data arrays initialised to copies of the values in the x, y and dydx1 arrays where y is the tabulated function y = f(x) and dydx is ∂y/∂x.

Usage:                      CubicInterpolation aa = new CubicInterpolation(x, y, numerDiffOption);
Creates an instance of CubicInterpolation with its internal data arrays initialised to copies of the values in the x and y arrays where y is the tabulated function y = f(x).

The derivatives ∂y/∂x1 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 cubic spline interpolation, i.e. as,

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



METHODS

INTERPOLATION
public double interpolate(double xx)
Usage:                      y1 = aa.interpolate(xx1);
Returns the interpolated value of y, y1, for a given value of xx1, using the y = f(x) 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 is outside the range of the values of x[] 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[][] getGridDydx()
Usage:                      grad1 = aa.getGridDydx();
This method returns the gradients, ∂y/∂x, 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:                      CubicInterpolation.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 h is calculated as:
 h = delta times (maximum value of x - minimum value of x)
The default value of delta is 1 x 10-3.
The values of f(x+h) and f(x-h), are calculated by interpolation of a cubic spline of the entered data. If h is greater than the mean separation of the relevant data points h is replaced by the minimimum separation of the data points. If delta is small this interpolation approximates to a cubic spline interpolation.
This method allows the value of delta to be reset. If delta is to be reset this static method must be called before CubicInterpolation is instantiated.



ROUNDING ERROR OPTIONS
public static void noRoundingErrorCheck()
Usage:                      CubicInterpolation.noRoundingErrorCheck();
Seveveral applications that call CubicInterpolation, e.g. plotting programs, may calculate an array of points to be fed to an instance of CubicInterpolation 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 CubicInterpolation 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:                      CubicInterpolation.potentialRoundingError(potentialRoundingError);
Seveveral applications that call CubicInterpolation, e.g. plotting programs, may calculate an array of points to be fed to an instance of CubicInterpolation 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 CubicInterpolation 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

CubicExampleOne

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

CubicExampleTwo

This simple program uses both CubicInterpolation and CubicSpline to demonstrate interpolation within a data set of the refractive indices of quartz tabulated as a function of both wavelength. This example program may be found on CubicExampleTwo.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