Example 4
This example integrates a two-dimensional, tensor-product spline over the rectangle [0, x] by [0, y].
using System;
using Imsl.Math;
public class Spline2DInterpolateEx4
{
// Define function
private static double F(double x, double y)
{
return (x*x*x+y*y);
}
// The integral of F from 0 to x and 0 to y
private static double FI(double x, double y)
{
return (y*x*x*x*x/4.0 + x*y*y*y/3.0);
}
public static void Main(String[] args)
{
int nData = 11, outData = 2;
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]);
}
}
// Compute tensor-product interpolant
Spline2DInterpolate spline =
new Spline2DInterpolate(xData, yData, fData);
// Print results
Console.Out.WriteLine(" x y FI(x, y) Integral Error");
for (int i = 0; i < outData; i++)
{
x = (double) (1+i) / (double) (outData+1);
for (int j = 0; j < outData; j++)
{
y = (double) (1+j) / (double) (outData+1);
z = spline.Integral(0.0, x, 0.0, y);
Console.Out.WriteLine(x.ToString("0.0000") + " " +
y.ToString("0.0000") + " " +
FI(x, y).ToString("0.0000") + " " +
z.ToString("0.0000") + " " +
Math.Abs(FI(x,y)-z).ToString("0.0000"));
}
}
}
}
Output
x y FI(x, y) Integral Error
0.3333 0.3333 0.0051 0.0051 0.0000
0.3333 0.6667 0.0350 0.0350 0.0000
0.6667 0.3333 0.0247 0.0247 0.0000
0.6667 0.6667 0.0988 0.0988 0.0000
Link to C# source.