Example 4
This example integrates a two-dimensional, tensor-product spline over the rectangle [0, x] by [0, y].
import java.text.*;
import com.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
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(4);
nf.setMinimumFractionDigits(4);
System.out.println(" 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);
System.out.println(nf.format(x) + " " + nf.format(y)
+ " " + nf.format(FI(x, y)) + " "
+ nf.format(z) + " "
+ nf.format(Math.abs(FI(x, y) - z)));
}
}
}
}
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 Java source.