In this example, the parameter of the Gaussian radial basis function
. The function is sampled at 100 random points and the error is computed at 10000 random points.
import com.imsl.math.*;
import com.imsl.stat.Random;
public class RadialBasisEx4 {
public static void main(String args[]) {
int nDim = 2;
// Sample, with noise, the function at 100 randomly choosen points
int nData = 100;
double xData[][] = new double[nData][nDim];
double fData[] = new double[nData];
Random rand = new Random(123457);
rand.setMultiplier(16807);
double noise[] = new double[nData * nDim];
for (int k = 0; k < nData; k++) {
for (int i = 0; i < nDim; i++) {
noise[k * 2 + i] = 1.0d - 2.0d * (double) rand.nextDouble();
xData[k][i] = 3 * noise[k * 2 + i];
}
// noisy sample
fData[k] = fcn(xData[k]) + noise[k * 2] / 10;
}
// Compute the radial basis approximation using 100 centers
int nCenters = 100;
RadialBasis rb = new RadialBasis(nDim, nCenters);
rb.setRadialFunction(new RadialBasis.Gaussian(.1));
rb.update(xData, fData);
// Compute the error at a randomly selected set of points
int nTest = 10000;
double maxError = 0.0;
double aveError = 0.0;
double maxMagnitude = 0.0;
double x[][] = new double[nTest][nDim];
noise = new double[nTest * nDim];
for (int i = 0; i < nTest; i++) {
for (int j = 0; j < nDim; j++) {
noise[i * 2 + j] = 1.0d - 2.0d * rand.nextDouble();
x[i][j] = 3 * noise[i * 2 + j];
}
double error = Math.abs(fcn(x[i]) - rb.value(x[i]));
maxMagnitude = Math.max(Math.abs(fcn(x[i])), maxMagnitude);
aveError += error;
maxError = Math.max(error, maxError);
}
aveError /= nTest;
System.out.println("Average normalized error is " + aveError / maxMagnitude);
System.out.println("Maximum normalized error is " + maxError / maxMagnitude);
}
// The function to approximate
static double fcn(double x[]) {
return Math.exp((x[1]) / 2.0) * Math.sin(x[0]) - Math.cos((x[1]) / 2.0);
}
}
Average normalized error is 0.01095458960263398 Maximum normalized error is 0.023010989446143673Link to Java source.