Example: The cubic spline interpolant to noisy data with supplied weights
A cubic spline interpolant to noisy data is computed using supplied weights and smoothing parameter. The value of the spline at point 0.3010 is printed.
using System;
using Imsl.Math;
using Imsl.Stat;
public class CsSmoothC2Ex1
{
public static void Main(String[] args)
{
// Set up a grid
int n = 300;
double[] x = new double[n];
double[] y = new double[n];
for (int k = 0; k < n; k++)
{
x[k] = 3.0 * ((double) (k) / (double) (n - 1));
y[k] = 1.0 / (.1 + System.Math.Pow(3.0 * (x[k] - 1.0), 4));
}
// Seed the random number generator
Imsl.Stat.Random rn = new Imsl.Stat.Random(1234579);
rn.Multiplier = 16807;
// Contaminate the data
for (int i = 0; i < n; i++)
{
y[i] = y[i] + 2.0 * (float) rn.NextDouble() - 1.0;
}
// Set the weights
double sdev = 1.0 / System.Math.Sqrt(3.0);
double[] weights = new double[n];
for (int i = 0; i < n; i++)
{
weights[i] = sdev;
}
// Set the smoothing parameter
double smpar = (double) n;
// Smooth the data
CsSmoothC2 cs = new CsSmoothC2(x, y, weights, smpar);
double csv = cs.Eval(0.3010);
Console.Out.WriteLine("The computed cubic spline value at " +
"point .3010 is " + csv);
}
}
Output
The computed cubic spline value at point .3010 is 0.0335028881575695
Link to C# source.