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.
using System;
using Imsl.Math;
public class RadialBasisEx1
{
public static void Main(String[] args)
{
int nDim = 10;
// Sample, with noise, the function at 100 randomly chosen points
int nData = 200;
double[,] xData = new double[nData,nDim];
double[] fData = new double[nData];
Imsl.Stat.Random rand = new Imsl.Stat.Random(234567);
double[] tmp = new double[nDim];
for (int k = 0; k < nData; k++)
{
for (int i = 0; i < nDim; i++)
{
tmp[i] = xData[k,i] = 2.0 * rand.NextDouble() - 1.0;
}
// noisy sample
fData[k] =
fcn(tmp) + 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.Eval(x));
aveError += error;
maxError = Math.Max(error, maxError);
double f = fcn(x);
}
aveError /= nTest;
Console.WriteLine("average error is " + aveError);
Console.WriteLine("maximum error is " + maxError);
}
// The function to approximate
internal 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.0419789795502543 maximum error is 0.171666811944547Link to C# source.