Example: The cubic spline interpolant to noisy data

A cubic spline interpolant to noisy data is computed using cross-validation to estimate the smoothing parameter. The value of the spline at point 0.3010 is printed.

import com.imsl.math.*;
import com.imsl.stat.*;

public class CsSmoothEx1 {

    public static void main(String args[]) {
        int n = 300;
        double x[] = new double[n];
        double y[] = new double[n];
        for (int k = 0; k < n; k++) {
            x[k] = (3.0 * k) / (n - 1);
            y[k] = 1.0 / (0.1 + Math.pow(3.0 * (x[k] - 1.0), 4));
        }

        //	Seed the random number generator
        Random rn = new Random();
        rn.setSeed(1234579L);
        rn.setMultiplier(16807);

        //	Contaminate the data
        for (int i = 0; i < n; i++) {
            y[i] += 2.0 * rn.nextFloat() - 1.0;
        }

        //	Smooth the data
        CsSmooth cs = new CsSmooth(x, y);
        double csv = cs.value(0.3010);
        System.out.println("The computed cubic spline value at point .3010 is "
                + csv);
    }
}

Output

The computed cubic spline value at point .3010 is 0.10785822561423902
Link to Java source.