Example 2

A tensor-product spline interpolant to a function
f(x,y) = x^{3} + y^2
is computed. The values of the interpolant and the error on a 4 x 4 grid are displayed. Notice that the first interpolant with order = 3 does not reproduce the cubic data, while the second interpolant with order = 6 does reproduce the data.
using System;
using Imsl.Math;

public class Spline2DInterpolateEx2
{
	
	private static double F(double x, double y)
	{
		return (x * x * x + y * y);
	}
	
	public static void  Main(String[] args)
	{
		int nData = 7;
		int outData = 4;
		double[,] fData = new double[nData,nData];
		double[] xData = new double[nData];
		double[] yData = new double[nData];
		double x, y, z;
		
		// Set up grid
		for (int i = 0; i < nData; i++)
		{
			xData[i] = yData[i] = (double) i / ((double) (nData - 1));
		}
		
		for (int i = 0; i < nData; i++)
		{
			for (int j = 0; j < nData; j++)
			{
				fData[i,j] = F(xData[i], yData[j]);
			}
		}
		
		
		for (int order = 3; order < 7; order += 3)
		{
			// Compute tensor-product interpolant
			Spline2DInterpolate spline = new Spline2DInterpolate(xData, yData,
            fData, order, order);
			
			// Print results
			Console.Out.WriteLine("\nThe order of the spline is " + order);
			Console.Out.WriteLine("   x        y     F(x, y)   " 
			+ "Interpolant   Error");
			
			for (int i = 0; i < outData; i++)
			{
				x = (double) i / (double) (outData);
				
				for (int j = 0; j < outData; j++)
				{
					y = (double) j / (double) (outData);
					z = spline.Value(x, y);
					
					Console.Out.WriteLine(x.ToString("0.0000")+"   "
						+y.ToString("0.0000")+"   "+F(x,y).ToString("0.0000")
						+"       "+z.ToString("0.0000")+"     "
						+Math.Abs(F(x, y) - z).ToString("0.0000"));
				}
			}
		}
	}
}

Output


The order of the spline is 3
   x        y     F(x, y)   Interpolant   Error
0.0000   0.0000   0.0000       0.0000     0.0000
0.0000   0.2500   0.0625       0.0625     0.0000
0.0000   0.5000   0.2500       0.2500     0.0000
0.0000   0.7500   0.5625       0.5625     0.0000
0.2500   0.0000   0.0156       0.0158     0.0002
0.2500   0.2500   0.0781       0.0783     0.0002
0.2500   0.5000   0.2656       0.2658     0.0002
0.2500   0.7500   0.5781       0.5783     0.0002
0.5000   0.0000   0.1250       0.1250     0.0000
0.5000   0.2500   0.1875       0.1875     0.0000
0.5000   0.5000   0.3750       0.3750     0.0000
0.5000   0.7500   0.6875       0.6875     0.0000
0.7500   0.0000   0.4219       0.4217     0.0002
0.7500   0.2500   0.4844       0.4842     0.0002
0.7500   0.5000   0.6719       0.6717     0.0002
0.7500   0.7500   0.9844       0.9842     0.0002

The order of the spline is 6
   x        y     F(x, y)   Interpolant   Error
0.0000   0.0000   0.0000       0.0000     0.0000
0.0000   0.2500   0.0625       0.0625     0.0000
0.0000   0.5000   0.2500       0.2500     0.0000
0.0000   0.7500   0.5625       0.5625     0.0000
0.2500   0.0000   0.0156       0.0156     0.0000
0.2500   0.2500   0.0781       0.0781     0.0000
0.2500   0.5000   0.2656       0.2656     0.0000
0.2500   0.7500   0.5781       0.5781     0.0000
0.5000   0.0000   0.1250       0.1250     0.0000
0.5000   0.2500   0.1875       0.1875     0.0000
0.5000   0.5000   0.3750       0.3750     0.0000
0.5000   0.7500   0.6875       0.6875     0.0000
0.7500   0.0000   0.4219       0.4219     0.0000
0.7500   0.2500   0.4844       0.4844     0.0000
0.7500   0.5000   0.6719       0.6719     0.0000
0.7500   0.7500   0.9844       0.9844     0.0000

Link to C# source.