In this example, the dimension d
=10. The function is sampled at 200 random points, in the
cube, to which what noise in the range [-0.2,0.2] is added. The error is computed at 1000 random points, also from the
cube. The compute errors are less than the added noise.
import com.imsl.math.*;
import java.util.Random;
public class RadialBasisEx1 {
public static void main(String args[]) {
int nDim = 10;
// Sample, with noise, the function at 100 randomly choosen points
int nData = 200;
double xData[][] = new double[nData][nDim];
double fData[] = new double[nData];
Random rand = new Random(234567L);
for (int k = 0; k < nData; k++) {
for (int i = 0; i < nDim; i++) {
xData[k][i] = 2.0*rand.nextDouble() - 1.0;
}
// noisy sample
fData[k] = fcn(xData[k]) + 0.20*(2.0*rand.nextDouble()-1.0);
}
// Compute the radial basis approximation using 25 centers
int nCenters = 25;
RadialBasis rb = new RadialBasis(nDim, nCenters);
rb.update(xData, fData);
// Compute the error at a randomly selected set of points
int nTest = 1000;
double maxError = 0.0;
double aveError = 0.0;
double x[] = new double[nDim];
for (int k = 0; k < nTest; k++) {
for (int i = 0; i < nDim; i++) {
x[i] = 2.0*rand.nextDouble() - 1.0;
}
double error = Math.abs(fcn(x)-rb.value(x));
aveError += error;
maxError = Math.max(error, maxError);
double f = fcn(x);
}
aveError /= nTest;
System.out.println("average error is "+aveError);
System.out.println("maximum error is "+maxError);
}
// The function to approximate
static double fcn(double x[]) {
double sum = 0.0;
for (int k = 0; k < x.length; k++) {
sum += x[k]*x[k];
}
sum /= x.length;
return Math.exp(-sum);
}
}
average error is 0.02619296746295321 maximum error is 0.13197595135821727Link to Java source.