Beispiel #1
0
    private static void test01(int prob, int grd, int m)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    VANDERMONDE_APPROX_2D_TEST01 tests VANDERMONDE_APPROX_2D_MATRIX.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the problem number.
    //
    //    Input, int GRD, the grid number.
    //    (Can't use GRID as the name because that's also a plotting function.)
    //
    //    Input, int M, the total polynomial degree.
    //
    {
        Console.WriteLine("");
        Console.WriteLine("TEST01:");
        Console.WriteLine("  Approximate data from TEST_INTERP_2D problem #" + prob + "");
        Console.WriteLine("  Use grid from TEST_INTERP_2D with index #" + grd + "");
        Console.WriteLine("  Using polynomial approximant of total degree " + m + "");

        int nd = Data_2D.g00_size(grd);

        Console.WriteLine("  Number of data points = " + nd + "");

        double[] xd = new double[nd];
        double[] yd = new double[nd];
        Data_2D.g00_xy(grd, nd, ref xd, ref yd);

        double[] zd = new double[nd];
        Data_2D.f00_f0(prob, nd, xd, yd, ref zd);

        switch (nd)
        {
        case < 10:
            typeMethods.r8vec3_print(nd, xd, yd, zd, "  X, Y, Z data:");
            break;
        }

        //
        //  Compute the Vandermonde matrix.
        //
        int tm = typeMethods.triangle_num(m + 1);

        double[] a = VandermondeMatrix.vandermonde_approx_2d_matrix(nd, m, tm, xd, yd);
        //
        //  Solve linear system.
        //
        double[] c = QRSolve.qr_solve(nd, tm, a, zd);
        //
        //  #1:  Does approximant match function at data points?
        //
        int ni = nd;

        double[] xi = typeMethods.r8vec_copy_new(ni, xd);
        double[] yi = typeMethods.r8vec_copy_new(ni, yd);
        double[] zi = Polynomial.r8poly_values_2d(m, c, ni, xi, yi);

        double app_error = typeMethods.r8vec_norm_affine(ni, zi, zd) / ni;

        Console.WriteLine("");
        Console.WriteLine("  L2 data approximation error = " + app_error + "");
    }
Beispiel #2
0
    private static void test01(int prob, int g, double p)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests SHEPARD_INTERP_2D.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the problem number.
    //
    //    Input, int G, the grid number.
    //
    //    Input, double P, the power used in the distance weighting.
    //
    {
        const bool debug = false;

        Console.WriteLine("");
        Console.WriteLine("TEST01:");
        Console.WriteLine("  Interpolate data from TEST_INTERP_2D problem #" + prob + "");
        Console.WriteLine("  using grid #" + g + "");
        Console.WriteLine("  using Shepard interpolation with P = " + p + "");

        int nd = Data_2D.g00_size(g);

        Console.WriteLine("  Number of data points = " + nd + "");

        double[] xd = new double[nd];
        double[] yd = new double[nd];
        Data_2D.g00_xy(g, nd, ref xd, ref yd);

        double[] zd = new double[nd];
        Data_2D.f00_f0(prob, nd, xd, yd, ref zd);

        switch (debug)
        {
        case true:
            typeMethods.r8vec3_print(nd, xd, yd, zd, "  X, Y, Z data:");
            break;
        }

        //
        //  #1:  Does interpolant match function at interpolation points?
        //
        int ni = nd;

        double[] xi = typeMethods.r8vec_copy_new(ni, xd);
        double[] yi = typeMethods.r8vec_copy_new(ni, yd);

        double[] zi = Shepard.shepard_interp_2d(nd, xd, yd, zd, p, ni, xi, yi);

        double int_error = typeMethods.r8vec_norm_affine(ni, zi, zd) / ni;

        Console.WriteLine("");
        Console.WriteLine("  L2 interpolation error averaged per interpolant node = " + int_error + "");
    }
Beispiel #3
0
    private static void test01(int prob, int g,
                               Func <int, double[], double, double[], double[]> phi, string phi_name)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    RBF_INTERP_2D_TEST01 tests RBF_INTERP_2D.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    06 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the index of the problem.
    //
    //    Input, int G, the index of the grid.
    //
    //    Input, void PHI ( int n, double r[], double r0, double v[] ), the
    //    radial basis function.
    //
    //    Input, string PHI_NAME, the name of the radial basis function.
    //
    {
        const bool debug = false;
        int        i;

        Console.WriteLine("");
        Console.WriteLine("RBF_INTERP_2D_TEST01:");
        Console.WriteLine("  Interpolate data from TEST_INTERP_2D problem #" + prob + "");
        Console.WriteLine("  using grid #" + g + "");
        Console.WriteLine("  using radial basis function \"" + phi_name + "\".");

        int nd = Data_2D.g00_size(g);

        Console.WriteLine("  Number of data points = " + nd + "");

        double[] xd = new double[nd];
        double[] yd = new double[nd];
        Data_2D.g00_xy(g, nd, ref xd, ref yd);

        double[] zd = new double[nd];
        Data_2D.f00_f0(prob, nd, xd, yd, ref zd);

        switch (debug)
        {
        case true:
            typeMethods.r8vec3_print(nd, xd, yd, zd, "  X, Y, Z data:");
            break;
        }

        const int m = 2;

        double[] xyd = new double[2 * nd];

        for (i = 0; i < nd; i++)
        {
            xyd[0 + i * 2] = xd[i];
            xyd[1 + i * 2] = yd[i];
        }

        double xmax   = typeMethods.r8vec_max(nd, xd);
        double xmin   = typeMethods.r8vec_min(nd, xd);
        double ymax   = typeMethods.r8vec_max(nd, yd);
        double ymin   = typeMethods.r8vec_min(nd, yd);
        double volume = (xmax - xmin) * (ymax - ymin);

        const double e  = 1.0 / m;
        double       r0 = Math.Pow(volume / nd, e);

        Console.WriteLine("  Setting R0 = " + r0 + "");

        double[] w = RadialBasisFunctions.rbf_weight(m, nd, xyd, r0, phi, zd);
        //
        //  #1:  Does interpolant match function at interpolation points?
        //
        double[] xyi = typeMethods.r8mat_copy_new(2, nd, xyd);

        double[] zi = RadialBasisFunctions.rbf_interp(m, nd, xyd, r0, phi, w, nd, xyi);

        double int_error = typeMethods.r8vec_norm_affine(nd, zi, zd) / nd;

        Console.WriteLine("");
        Console.WriteLine("  L2 interpolation error averaged per interpolant node = "
                          + int_error + "");
    }
Beispiel #4
0
    private static void test03(int prob)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST03 tests PWL_INTERP_2D_SCATTERED_VALUE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    25 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int       element_num = 0;
        int       i;
        int       j;
        const int ni = 25;

        double[] xi  = new double[25];
        double[] xyi = new double[2 * 25];
        double[] yi  = new double[25];

        const int g  = 2;
        int       nd = Data_2D.g00_size(g);

        Console.WriteLine("");
        Console.WriteLine("TEST03");
        Console.WriteLine("  PWL_INTERP_2D_SCATTERED_VALUE evaluates a");
        Console.WriteLine("  piecewise linear interpolant to scattered data.");
        Console.WriteLine("  Here, we use grid number " + g + "");
        Console.WriteLine("  with " + nd + " scattered points in the unit square");
        Console.WriteLine("  on problem " + prob + "");
        //
        //  Get the data points and evaluate the function there.
        //
        double[] xd = new double[nd];
        double[] yd = new double[nd];

        Data_2D.g00_xy(g, nd, ref xd, ref yd);

        double[] zd = new double[nd];
        Data_2D.f00_f0(prob, nd, xd, yd, ref zd);

        double[] xyd = new double[2 * nd];

        for (i = 0; i < nd; i++)
        {
            xyd[0 + i * 2] = xd[i];
            xyd[1 + i * 2] = yd[i];
        }

        //
        //  Set up the Delaunay triangulation.
        //
        int[] element_neighbor = new int[3 * 2 * nd];
        int[] triangle         = new int[3 * 2 * nd];

        Delauney.r8tris2(nd, ref xyd, ref element_num, ref triangle, ref element_neighbor);

        for (j = 0; j < element_num; j++)
        {
            for (i = 0; i < 3; i++)
            {
                switch (element_neighbor[i + j * 3])
                {
                case > 0:
                    element_neighbor[i + j * 3] -= 1;
                    break;
                }
            }
        }

        //
        //  Define the interpolation points.
        //
        int k = 0;

        for (i = 1; i <= 5; i++)
        {
            for (j = 1; j <= 5; j++)
            {
                xyi[0 + k * 2] = (2 * i - 1) / 10.0;
                xyi[1 + k * 2] = (2 * j - 1) / 10.0;
                k += 1;
            }
        }

        for (k = 0; k < ni; k++)
        {
            xi[k] = xyi[0 + k * 2];
            yi[k] = xyi[1 + k * 2];
        }

        double[] ze = new double[ni];
        Data_2D.f00_f0(prob, ni, xi, yi, ref ze);
        //
        //  Evaluate the interpolant.
        //
        double[] zi = Interp2D.pwl_interp_2d_scattered_value(nd, xyd, zd, element_num,
                                                             triangle, element_neighbor, ni, xyi);

        double rms = 0.0;

        for (k = 0; k < ni; k++)
        {
            rms += Math.Pow(zi[k] - ze[k], 2);
        }

        rms = Math.Sqrt(rms / ni);

        Console.WriteLine("");
        Console.WriteLine("  RMS error is " + rms + "");

        Console.WriteLine("");
        Console.WriteLine("     K      Xi(K)       Yi(K)       Zi(K)       Z(X,Y)");
        Console.WriteLine("");

        for (k = 0; k < ni; k++)
        {
            Console.WriteLine("  " + k.ToString().PadLeft(4)
                              + "  " + xyi[0 + k * 2].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + xyi[1 + k * 2].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + zi[k].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                              + "  " + ze[k].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }
    }