Example: The Kochanek-Bartels cubic spline interpolant

This example interpolates to a set of points. At x = 3, the continuity and tension parameters are -1. At all other points, they are zero. Interpolated values are then printed.

import java.text.*;
import com.imsl.math.*;

public class CsTCBEx1 {

    public static void main(String args[]) {

        double[] xdata = {0., 1., 2., 3., 4., 5.};
        double[] ydata = {5., 2., 3., 5., 1., 2.};
        double[] continuity = {0., 0., 0., -1., 0., 0.};
        double[] tension = {0., 0., 0., -1., 0., 0.};

        CsTCB cs = new CsTCB(xdata, ydata);
        cs.setContinuity(continuity);
        cs.setTension(tension);
        cs.compute();
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(4);
        nf.setMinimumFractionDigits(4);
        System.out.println("      x    " + "   value   ");

        for (int k = 0; k < 11; k++) {
            double x = (double) k / 2.0;
            double y = cs.value(x);
            System.out.println("   " + nf.format(x) + "     " + nf.format(y));
        }
    }
}

Output

      x       value   
   0.0000     5.0000
   0.5000     3.4375
   1.0000     2.0000
   1.5000     2.1875
   2.0000     3.0000
   2.5000     3.6875
   3.0000     5.0000
   3.5000     2.1875
   4.0000     1.0000
   4.5000     1.2500
   5.0000     2.0000
Link to Java source.