# Find Max Value An Interpolation Spline

(Pierre Gouffran) #1

I have two array; double [ ] X and double [ ] Y. Using this unsorted and different arrays, I obtained a cubic spline array like:

CubicSpline newdatafunc = CubicSpline.Interpolate(X, Y);

Now, I want to find max interpolate value in this series; even if it is bigger than all of the Y array elements.

Note: Also, I try to create chart using this cubicspline interpolate series. How can I convert to datas, like [ ] newX and [ ]newY which is meaning included interpolated values.

(Pierre Gouffran) #2

Is there any solution?

(Peter Vanderwaart) #3

The programming is easy, however cubic splines wiggle in unexpected ways and there may be multiple extrema. If you get strange answers, or program crashes, plot the spline to see whatâ€™s going on.

``````private void btnSplineMin_Click(object sender, EventArgs e)
{
// spline data
double[] X = { 0.0, 6.0, 10.0, 12.0, 50.0, 75.0 };
double[] Y = { 100.0, 65.0, 33.0, 2.0, 12.0, 45.0 };
CubicSpline sp = CubicSpline.InterpolateNatural(X, Y);

var obj = ObjectiveFunction.ScalarValue(sp.Interpolate);
var r1 = GoldenSectionMinimizer.Minimum(obj, X.Minimum(), X.Maximum());

Console.WriteLine("Iterations: " + r1.Iterations.ToString());
Console.WriteLine("Minimizing Point: " + r1.MinimizingPoint.ToString());
Console.WriteLine("Minimum Value: " + sp.Interpolate(r1.MinimizingPoint).ToString());

// To maximize, multiply objective function by -1.
Y = new double[] { 1.0, 3.0, 4.0, 4.0, 3.0, 1.0 };
sp = CubicSpline.InterpolateNatural(X, Y);

var f0 = new Func<double, double>((z) => -1.0 * sp.Interpolate(z));
obj = ObjectiveFunction.ScalarValue(f0);
r1 = GoldenSectionMinimizer.Minimum(obj, X.Minimum(), X.Maximum());

Console.WriteLine("Iterations: " + r1.Iterations.ToString());
Console.WriteLine("Maximizing Point: " + r1.MinimizingPoint.ToString());
Console.WriteLine("Maximum Value: " + (-1.0 * f0(r1.MinimizingPoint)).ToString());
}``````