Esempio n. 1
0
    private static void Main()
/******************************************************************************/
//
//  Purpose:
//
//    MAIN is the main program for SHEPARD_INTERP_1D_TEST.
//
//  Discussion:
//
//    SHEPARD_INTERP_1D_TEST tests the SHEPARD_INTERP_1D library.
//
//  Licensing:
//
//    This code is distributed under the GNU LGPL license.
//
//  Modified:
//
//    03 July 2015
//
//  Author:
//
//    John Burkardt
//
    {
        int p_num = 5;

        double[] p_test =
        {
            0.0, 1.0, 2.0, 4.0, 8.0
        }

        ;

        Console.WriteLine("");
        Console.WriteLine("SHEPARD_INTERP_1D_TEST:");
        Console.WriteLine("  Test the SHEPARD_INTERP_1D library.");
        Console.WriteLine("  The R8LIB library is needed.");
        Console.WriteLine("  This test needs the TEST_INTERP library as well.");

        shepard_basis_1d_test();

        shepard_value_1d_test();

        int prob_num = TestInterp.p00_prob_num();

        for (int prob = 1; prob <= prob_num; prob++)
        {
            for (int j = 0; j < p_num; j++)
            {
                double p = p_test[j];
                test01(prob, p);
            }
        }

        Console.WriteLine("");
        Console.WriteLine("SHEPARD_INTERP_1D_TEST:");
        Console.WriteLine("  Normal end of execution.");
        Console.WriteLine("");
    }
Esempio n. 2
0
    private static void Main()
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MAIN is the main program for VANDERMONDE_INTERP_1D_TEST.
    //
    //  Discussion:
    //
    //    VANDERMONDE_INTERP_1D_TEST tests the VANDERMONDE_INTERP_1D library.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    03 July 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int prob;

        Console.WriteLine("");
        Console.WriteLine("VANDERMONDE_INTERP_1D_TEST:");
        Console.WriteLine("  Test the VANDERMONDE_INTERP_1D library.");
        Console.WriteLine("  The QR_SOLVE library is needed.");
        Console.WriteLine("  The R8LIB library is needed.");
        Console.WriteLine("  This test needs the CONDITION library.");
        Console.WriteLine("  This test needs the TEST_INTERP library.");

        vandermonde_coef_1d_test();

        vandermonde_matrix_1d_test();

        vandermonde_value_1d_test();

        int prob_num = TestInterp.p00_prob_num();

        for (prob = 1; prob <= prob_num; prob++)
        {
            test01(prob);
        }

        for (prob = 1; prob <= prob_num; prob++)
        {
            test02(prob);
        }

        Console.WriteLine("");
        Console.WriteLine("VANDERMONDE_INTERP_1D_TEST:");
        Console.WriteLine("  Normal end of execution.");
        Console.WriteLine("");
    }
Esempio n. 3
0
    private static void nearest_interp_1d_test01(int prob, int ni)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    NEAREST_INTERP_1D_TEST01 tests NEAREST_INTERP_1D.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 September 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the index of the problem.
    //
    //    Input, int NI, the number of interpolation points.
    //
    {
        int j;

        Console.WriteLine("");
        Console.WriteLine("NEAREST_INTERP_1D_TEST01");
        Console.WriteLine("  Sample the nearest neighbor interpolant for problem # " + prob + "");

        int nd = TestInterp.p00_data_num(prob);

        double[] d = TestInterp.p00_data(prob, 2, nd);

        double[] xd = new double[nd];
        double[] yd = new double[nd];

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

        double xd_min = typeMethods.r8vec_min(nd, xd);
        double xd_max = typeMethods.r8vec_max(nd, xd);

        double[] xi = typeMethods.r8vec_linspace_new(ni, xd_min, xd_max);
        double[] yi = Nearest1D.nearest_interp_1d(nd, xd, yd, ni, xi);

        string title = "X, Y for problem " + prob;

        typeMethods.r8vec2_print(ni, xi, yi, title);
    }
Esempio n. 4
0
    private static void Main()
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MAIN is the main program for PWL_INTERP_1D_TEST.
    //
    //  Discusion:
    //
    //    PWL_INTERP_1D_TEST tests the PWL_INTERP_1D library.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    01 July 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int prob;

        Console.WriteLine("");
        Console.WriteLine("PWL_INTERP_1D_TEST:");
        Console.WriteLine("  Test the PWL_INTERP_1D library.");
        Console.WriteLine("  The R8LIB library is needed.");
        Console.WriteLine("  The test needs the TEST_INTERP library.");

        pwl_basis_1d_test();

        pwl_value_1d_test();

        int prob_num = TestInterp.p00_prob_num();

        for (prob = 1; prob <= prob_num; prob++)
        {
            pwl_interp_1d_test01(prob);
        }

        Console.WriteLine("");
        Console.WriteLine("PWL_INTERP_1D_TEST:");
        Console.WriteLine("  Normal end of execution.");
        Console.WriteLine("");
    }
Esempio n. 5
0
    private static void pwl_interp_1d_test01(int prob)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    PWL_INTERP_1D_TEST01 tests PWL_INTERP_1D.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    31 May 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the problem index.
    //
    {
        List <string> command_unit = new();
        List <string> data_unit    = new();
        int           i;
        List <string> interp_unit = new();
        int           j;

        Console.WriteLine("");
        Console.WriteLine("PWL_INTERP_1D_TEST01:");
        Console.WriteLine("  PWL_INTERP_1D evaluates the piecewise linear interpolant.");
        Console.WriteLine("  Interpolate data from TEST_INTERP problem #" + prob + "");

        int nd = TestInterp.p00_data_num(prob);

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

        double[] xy = TestInterp.p00_data(prob, 2, nd);

        typeMethods.r8mat_transpose_print(2, nd, xy, "  Data array:");

        double[] xd = new double[nd];
        double[] yd = new double[nd];

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

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

        double[] xi = new double[ni];
        for (i = 0; i < ni; i++)
        {
            xi[i] = xd[i];
        }

        double[] yi = Interp1D.pwl_value_1d(nd, xd, yd, ni, xi);

        double interp_error = typeMethods.r8vec_diff_norm(ni, yi, yd) / ni;

        Console.WriteLine("");
        Console.WriteLine("  L2 interpolation error averaged per interpolant node = " + interp_error + "");
        //
        //  Create data file.
        //
        string data_filename = "data" + prob + ".txt";

        for (j = 0; j < nd; j++)
        {
            data_unit.Add("  " + xd[j]
                          + "  " + yd[j] + "");
        }

        File.WriteAllLines(data_filename, data_unit);

        Console.WriteLine("");
        Console.WriteLine("  Created graphics data file \"" + data_filename + "\".");
        //
        //  Create interp file.
        //

        ni = 501;
        double xmin = typeMethods.r8vec_min(nd, xd);
        double xmax = typeMethods.r8vec_max(nd, xd);

        xi = typeMethods.r8vec_linspace_new(ni, xmin, xmax);
        yi = Interp1D.pwl_value_1d(nd, xd, yd, ni, xi);

        string interp_filename = "interp" + prob + ".txt";

        for (j = 0; j < ni; j++)
        {
            interp_unit.Add("  " + xi[j]
                            + "  " + yi[j] + "");
        }

        File.WriteAllLines(interp_filename, interp_unit);

        Console.WriteLine("  Created graphics interp file \"" + interp_filename + "\".");
        //
        //  Plot the data and the interpolant.
        //
        string command_filename = "commands" + prob + ".txt";

        string output_filename = "plot" + prob + ".png";

        command_unit.Add("# " + command_filename + "");
        command_unit.Add("#");
        command_unit.Add("# Usage:");
        command_unit.Add("#  gnuplot < " + command_filename + "");
        command_unit.Add("#");
        command_unit.Add("set term png");
        command_unit.Add("set output '" + output_filename + "'");
        command_unit.Add("set xlabel '<---X--->'");
        command_unit.Add("set ylabel '<---Y--->'");
        command_unit.Add("set title 'Data versus Nearest Neighbor Interpolant'");
        command_unit.Add("set grid");
        command_unit.Add("set style data lines");
        command_unit.Add("plot '" + data_filename
                         + "' using 1:2 with points pt 7 ps 2 lc rgb 'blue',\\");
        command_unit.Add("     '" + interp_filename
                         + "' using 1:2 lw 3 linecolor rgb 'red'");

        File.WriteAllLines(command_filename, command_unit);

        Console.WriteLine("  Created graphics command file \"" + command_filename + "\".");
    }
Esempio n. 6
0
    private static void test01(int prob, int m)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests VANDERMONDE_APPROX_1D_MATRIX.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    10 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the problem number.
    //
    //    Input, int M, the polynomial degree.
    //
    {
        const bool debug = false;
        int        i;

        Console.WriteLine("");
        Console.WriteLine("TEST01:");
        Console.WriteLine("  Approximate data from TEST_INTERP problem #" + prob + "");

        int nd = TestInterp.p00_data_num(prob);

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

        double[] xy = TestInterp.p00_data(prob, 2, nd);

        switch (debug)
        {
        case true:
            typeMethods.r8mat_transpose_print(2, nd, xy, "  Data array:");
            break;
        }

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

        //
        //  Compute the Vandermonde matrix.
        //
        Console.WriteLine("  Using polynomial approximant of degree " + m + "");

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

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

        double app_error = typeMethods.r8vec_norm_affine(ni, yi, yd) / ni;

        Console.WriteLine("");
        Console.WriteLine("  L2 data approximation error = " + app_error + "");

        //
        //  #2: Compare estimated curve length to piecewise linear (minimal) curve length.
        //  Assume data is sorted, and normalize X and Y dimensions by (XMAX-XMIN) and
        //  (YMAX-YMIN).
        //
        double xmin = typeMethods.r8vec_min(nd, xd);
        double xmax = typeMethods.r8vec_max(nd, xd);
        double ymin = typeMethods.r8vec_min(nd, yd);
        double ymax = typeMethods.r8vec_max(nd, yd);

        ni = 501;
        xi = typeMethods.r8vec_linspace_new(ni, xmin, xmax);

        yi = Polynomial.r8poly_values(m, c, ni, xi);

        double ld = 0.0;

        for (i = 0; i < nd - 1; i++)
        {
            ld += Math.Sqrt(Math.Pow((xd[i + 1] - xd[i]) / (xmax - xmin), 2)
                            + Math.Pow((yd[i + 1] - yd[i]) / (ymax - ymin), 2));
        }

        double li = 0.0;

        for (i = 0; i < ni - 1; i++)
        {
            li += Math.Sqrt(Math.Pow((xi[i + 1] - xi[i]) / (xmax - xmin), 2)
                            + Math.Pow((yi[i + 1] - yi[i]) / (ymax - ymin), 2));
        }

        Console.WriteLine("");
        Console.WriteLine("  Normalized length of piecewise linear interpolant = " + ld + "");
        Console.WriteLine("  Normalized length of polynomial interpolant       = " + li + "");
    }
Esempio n. 7
0
    private static void test02(int prob)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST02 tests VANDERMONDE_INTERP_1D_MATRIX.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 June 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the problem index.
    //
    {
        List <string> command_unit = new();
        List <string> data_unit    = new();
        int           i;
        List <string> interp_unit = new();
        int           j;

        Console.WriteLine("");
        Console.WriteLine("TEST02:");
        Console.WriteLine("  VANDERMONDE_INTERP_1D_MATRIX sets the Vandermonde linear system");
        Console.WriteLine("  for the interpolating polynomial.");
        Console.WriteLine("  Interpolate data from TEST_INTERP problem #" + prob + "");

        int nd = TestInterp.p00_data_num(prob);

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

        double[] xy = TestInterp.p00_data(prob, 2, nd);

        typeMethods.r8mat_transpose_print(2, nd, xy, "  Data array:");

        double[] xd = new double[nd];
        double[] yd = new double[nd];

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

        //
        //  Compute Vandermonde matrix and get condition number.
        //
        double[] ad = Vandermonde.vandermonde_matrix_1d(nd, xd);
        //
        //  Solve linear system.
        //
        double[] cd = QRSolve.qr_solve(nd, nd, ad, yd);
        //
        //  Create data file.
        //
        string data_filename = "data" + prob + ".txt";

        for (j = 0; j < nd; j++)
        {
            data_unit.Add("  " + xd[j]
                          + "  " + yd[j] + "");
        }

        File.WriteAllLines(data_filename, data_unit);
        Console.WriteLine("");
        Console.WriteLine("  Created graphics data file \"" + data_filename + "\".");
        //
        //  Create interp file.
        //
        int    ni   = 501;
        double xmin = typeMethods.r8vec_min(nd, xd);
        double xmax = typeMethods.r8vec_max(nd, xd);

        double[] xi = typeMethods.r8vec_linspace_new(ni, xmin, xmax);
        double[] yi = Vandermonde.vandermonde_value_1d(nd, cd, ni, xi);

        string interp_filename = "interp" + prob + ".txt";

        for (j = 0; j < ni; j++)
        {
            interp_unit.Add("  " + xi[j]
                            + "  " + yi[j] + "");
        }

        File.WriteAllLines(interp_filename, interp_unit);
        Console.WriteLine("  Created graphics interp file \"" + interp_filename + "\".");
        //
        //  Plot the data and the interpolant.
        //
        string command_filename = "commands" + prob + ".txt";

        string output_filename = "plot" + prob + ".png";

        command_unit.Add("# " + command_filename + "");
        command_unit.Add("#");
        command_unit.Add("# Usage:");
        command_unit.Add("#  gnuplot < " + command_filename + "");
        command_unit.Add("#");
        command_unit.Add("set term png");
        command_unit.Add("set output '" + output_filename + "'");
        command_unit.Add("set xlabel '<---X--->'");
        command_unit.Add("set ylabel '<---Y--->'");
        command_unit.Add("set title 'Data versus Vandermonde polynomial interpolant'");
        command_unit.Add("set grid");
        command_unit.Add("set style data lines");
        command_unit.Add("plot '" + data_filename
                         + "' using 1:2 with points pt 7 ps 2 lc rgb 'blue',\\");
        command_unit.Add("     '" + interp_filename
                         + "' using 1:2 lw 3 linecolor rgb 'red'");

        File.WriteAllLines(command_filename, command_unit);
        Console.WriteLine("  Created graphics command file \"" + command_filename + "\".");
    }
Esempio n. 8
0
    private static void test01(int prob)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests VANDERMONDE_INTERP_1D.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    07 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const bool debug = false;
        int        i;

        Console.WriteLine("");
        Console.WriteLine("TEST01:");
        Console.WriteLine("  Interpolate data from TEST_INTERP problem #" + prob + "");

        int nd = TestInterp.p00_data_num(prob);

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

        double[] xy = TestInterp.p00_data(prob, 2, nd);

        switch (debug)
        {
        case true:
            typeMethods.r8mat_transpose_print(2, nd, xy, "  Data array:");
            break;
        }

        double[] xd = new double[nd];
        double[] yd = new double[nd];

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

        //
        //  Compute Vandermonde matrix and get condition number.
        //
        double[] ad = Vandermonde.vandermonde_matrix_1d(nd, xd);

        double condition = Matrix.condition_hager(nd, ad);

        Console.WriteLine("");
        Console.WriteLine("  Condition of Vandermonde matrix is " + condition + "");
        //
        //  Solve linear system.
        //
        double[] cd = QRSolve.qr_solve(nd, nd, ad, yd);
        //
        //  #1:  Does interpolant match function at interpolation points?
        //
        int ni = nd;

        double[] xi = typeMethods.r8vec_copy_new(ni, xd);
        double[] yi = Vandermonde.vandermonde_value_1d(nd, cd, ni, xi);

        double int_error = typeMethods.r8vec_norm_affine(ni, yi, yd) / ni;

        Console.WriteLine("");
        Console.WriteLine("  L2 interpolation error averaged per interpolant node = " + int_error + "");

        //
        //  #2: Compare estimated curve length to piecewise linear (minimal) curve length.
        //  Assume data is sorted, and normalize X and Y dimensions by (XMAX-XMIN) and
        //  (YMAX-YMIN).
        //
        double xmin = typeMethods.r8vec_min(nd, xd);
        double xmax = typeMethods.r8vec_max(nd, xd);
        double ymin = typeMethods.r8vec_min(nd, yd);
        double ymax = typeMethods.r8vec_max(nd, yd);

        ni = 501;
        xi = typeMethods.r8vec_linspace_new(ni, xmin, xmax);
        yi = Vandermonde.vandermonde_value_1d(nd, cd, ni, xi);

        double ld = 0.0;

        for (i = 0; i < nd - 1; i++)
        {
            ld += Math.Sqrt(Math.Pow((xd[i + 1] - xd[i]) / (xmax - xmin), 2)
                            + Math.Pow((yd[i + 1] - yd[i]) / (ymax - ymin), 2));
        }

        double li = 0.0;

        for (i = 0; i < ni - 1; i++)
        {
            li += Math.Sqrt(Math.Pow((xi[i + 1] - xi[i]) / (xmax - xmin), 2)
                            + Math.Pow((yi[i + 1] - yi[i]) / (ymax - ymin), 2));
        }

        Console.WriteLine("");
        Console.WriteLine("  Normalized length of piecewise linear interpolant = " + ld + "");
        Console.WriteLine("  Normalized length of polynomial interpolant       = " + li + "");
    }
Esempio n. 9
0
    private static void test01(int prob, double p)
//****************************************************************************80
//
//  Purpose:
//
//    TEST01 tests SHEPARD_VALUE_1D.
//
//  Licensing:
//
//    This code is distributed under the GNU LGPL license.
//
//  Modified:
//
//    01 October 2012
//
//  Author:
//
//    John Burkardt
//
    {
        int i;

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

        int dim_num = TestInterp.p00_dim_num(prob);

        int nd = TestInterp.p00_data_num(prob);

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

        double[] xy = TestInterp.p00_data(prob, dim_num, nd);

        switch (p)
        {
        case 0.0:
            typeMethods.r8mat_transpose_print(2, nd, xy, "  Data array:");
            break;
        }

        double[] xd = new double[nd];
        double[] yd = new double[nd];

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

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

        double[] xi = new double[ni];
        for (i = 0; i < ni; i++)
        {
            xi[i] = xd[i];
        }

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

        double int_error = typeMethods.r8vec_norm_affine(nd, yi, yd) / ni;

        Console.WriteLine("");
        Console.WriteLine("  L2 interpolation error averaged per interpolant node = "
                          + int_error + "");

//
//  #2: Compare estimated curve length to piecewise linear (minimal) curve length.
//  Assume data is sorted, and normalize X and Y dimensions by (XMAX-XMIN) and
//  (YMAX-YMIN).
//
        double xmin = typeMethods.r8vec_min(nd, xd);
        double xmax = typeMethods.r8vec_max(nd, xd);
        double ymin = typeMethods.r8vec_min(nd, yd);
        double ymax = typeMethods.r8vec_max(nd, yd);

        ni = 501;
        xi = typeMethods.r8vec_linspace_new(ni, xmin, xmax);
        yi = Shepard.shepard_value_1d(nd, xd, yd, p, ni, xi);

        double ld = 0.0;

        for (i = 0; i < nd - 1; i++)
        {
            ld += Math.Sqrt(Math.Pow((xd[i + 1] - xd[i]) / (xmax - xmin), 2)
                            + Math.Pow((yd[i + 1] - yd[i]) / (ymax - ymin), 2));
        }

        double li = 0.0;

        for (i = 0; i < ni - 1; i++)
        {
            li += Math.Sqrt(Math.Pow((xi[i + 1] - xi[i]) / (xmax - xmin), 2)
                            + Math.Pow((yi[i + 1] - yi[i]) / (ymax - ymin), 2));
        }

        Console.WriteLine("");
        Console.WriteLine("  Normalized length of piecewise linear interpolant = " + ld + "");
        Console.WriteLine("  Normalized length of Shepard interpolant          = " + li + "");
    }
Esempio n. 10
0
    private static void newton_interp_1d_test01(int prob)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    NEWTON_INTERP_1D_TEST01 tests NEWTON_INTERP_1D.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    10 July 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        List <string> command_unit = new();
        List <string> data_unit    = new();
        int           i;
        List <string> interp_unit = new();
        int           j;

        Console.WriteLine("");
        Console.WriteLine("NEWTON_INTERP_1D_TEST01:");
        Console.WriteLine("  Interpolate data from TEST_INTERP problem #" + prob + "");

        int nd = TestInterp.p00_data_num(prob);

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

        double[] xy = TestInterp.p00_data(prob, 2, nd);

        double[] xd = new double[nd];
        double[] yd = new double[nd];

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

        typeMethods.r8vec2_print(nd, xd, yd, "  X, Y data:");
        //
        //  Get the Newton coefficients.
        //
        double[] cd = Newton1D.newton_coef_1d(nd, xd, yd);
        //
        //  #1:  Does interpolant match function at interpolation points?
        //
        int ni = nd;

        double[] xi = typeMethods.r8vec_copy_new(ni, xd);
        double[] yi = Newton1D.newton_value_1d(nd, xd, cd, ni, xi);

        double interp_error = typeMethods.r8vec_norm_affine(ni, yi, yd) / ni;

        Console.WriteLine("");
        Console.WriteLine("  L2 interpolation error averaged per interpolant node = " + interp_error + "");

        //
        //  #2: Compare estimated curve length to piecewise linear (minimal) curve length.
        //  Assume data is sorted, and normalize X and Y dimensions by (XMAX-XMIN) and
        //  (YMAX-YMIN).
        //
        double xmin = typeMethods.r8vec_min(nd, xd);
        double xmax = typeMethods.r8vec_max(nd, xd);
        double ymin = typeMethods.r8vec_min(nd, yd);
        double ymax = typeMethods.r8vec_max(nd, yd);

        ni = 501;
        xi = typeMethods.r8vec_linspace_new(ni, xmin, xmax);
        yi = Newton1D.newton_value_1d(nd, xd, cd, ni, xi);

        double ld = 0.0;

        for (i = 0; i < nd - 1; i++)
        {
            ld += Math.Sqrt(Math.Pow((xd[i + 1] - xd[i]) / (xmax - xmin), 2)
                            + Math.Pow((yd[i + 1] - yd[i]) / (ymax - ymin), 2));
        }

        double li = 0.0;

        for (i = 0; i < ni - 1; i++)
        {
            li += Math.Sqrt(Math.Pow((xi[i + 1] - xi[i]) / (xmax - xmin), 2)
                            + Math.Pow((yi[i + 1] - yi[i]) / (ymax - ymin), 2));
        }

        Console.WriteLine("");
        Console.WriteLine("  Normalized length of piecewise linear interpolant = " + ld + "");
        Console.WriteLine("  Normalized length of Newton interpolant           = " + li + "");

        //
        //  Create data file.
        //
        string data_filename = "data" + prob + ".txt";

        for (j = 0; j < nd; j++)
        {
            data_unit.Add("  " + xd[j]
                          + "  " + yd[j] + "");
        }

        File.WriteAllLines(data_filename, data_unit);
        Console.WriteLine("");
        Console.WriteLine("  Created graphics data file \"" + data_filename + "\".");
        //
        //  Create interp file.
        //
        ni   = 501;
        xmin = typeMethods.r8vec_min(nd, xd);
        xmax = typeMethods.r8vec_max(nd, xd);
        xi   = typeMethods.r8vec_linspace_new(ni, xmin, xmax);
        yi   = Newton1D.newton_value_1d(nd, xd, cd, ni, xi);

        string interp_filename = "interp" + prob + ".txt";

        if (interp_filename == null)
        {
            throw new ArgumentNullException(nameof(interp_filename));
        }

        for (j = 0; j < ni; j++)
        {
            interp_unit.Add("  " + xi[j]
                            + "  " + yi[j] + "");
        }

        File.WriteAllLines(interp_filename, interp_unit);
        Console.WriteLine("  Created graphics interp file \"" + interp_filename + "\".");
        //
        //  Plot the data and the interpolant.
        //
        string command_filename = "commands" + prob + ".txt";

        string output_filename = "plot" + prob + ".png";

        command_unit.Add("# " + command_filename + "");
        command_unit.Add("#");
        command_unit.Add("# Usage:");
        command_unit.Add("#  gnuplot < " + command_filename + "");
        command_unit.Add("#");
        command_unit.Add("set term png");
        command_unit.Add("set output '" + output_filename + "'");
        command_unit.Add("set xlabel '<---X--->'");
        command_unit.Add("set ylabel '<---Y--->'");
        command_unit.Add("set title 'Data versus Newton polynomial interpolant'");
        command_unit.Add("set grid");
        command_unit.Add("set style data lines");
        command_unit.Add("plot '" + data_filename
                         + "' using 1:2 with points pt 7 ps 2 lc rgb 'blue',\\");
        command_unit.Add("     '" + interp_filename
                         + "' using 1:2 lw 3 linecolor rgb 'red'");

        File.WriteAllLines(command_filename, command_unit);
        Console.WriteLine("  Created graphics command file \"" + command_filename + "\".");
    }
Esempio n. 11
0
    private static void Main()
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MAIN is the main program for RBF_INTERP_1D_TEST.
    //
    //  Discussion:
    //
    //    RBF_INTERP_1D_TEST tests the RBF_INTERP_1D library.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int prob;

        Console.WriteLine("");
        Console.WriteLine("RBF_INTERP_1D_TEST:");
        Console.WriteLine("  Test the RBF_INTERP_1D library.");
        Console.WriteLine("  The R8LIB library is needed.");
        Console.WriteLine("  The test needs the TEST_INTERP library.");

        int prob_num = TestInterp.p00_prob_num();

        for (prob = 1; prob <= prob_num; prob++)
        {
            //
            //  Determine an appropriate value of R0, the spacing parameter.
            //
            int      nd = TestInterp.p00_data_num(prob);
            double[] xy = TestInterp.p00_data(prob, 2, nd);
            double[] xd = new double[nd];
            int      i;
            for (i = 0; i < nd; i++)
            {
                xd[i] = xy[0 + i * 2];
            }

            double xmax = typeMethods.r8vec_max(nd, xd);
            double xmin = typeMethods.r8vec_min(nd, xd);
            double r0   = (xmax - xmin) / (nd - 1);

            test01(prob, RadialBasisFunctions.phi1, "phi1", r0);
            test01(prob, RadialBasisFunctions.phi2, "phi2", r0);
            test01(prob, RadialBasisFunctions.phi3, "phi3", r0);
            test01(prob, RadialBasisFunctions.phi4, "phi4", r0);
        }

        /*
         * Terminate.
         */
        Console.WriteLine("");
        Console.WriteLine("RBF_INTERP_1D_TEST:");
        Console.WriteLine("  Normal end of execution.");
        Console.WriteLine("");
    }
Esempio n. 12
0
    private static void test01(int prob, Func <int, double[], double, double[], double[]> phi,
                               string phi_name, double r0)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests RBF_INTERP_1D.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the index of the problem.
    //
    //    Input, double PHI ( int n, double r[], double r0, double v[] ),
    //    the name of the radial basis function.
    //
    //    Input, string PHI_NAME, the name of the radial basis function.
    //
    //    Input, double R0, the scale factor.  Typically, this might be
    //    a small multiple of the average distance between points.
    //
    {
        const bool debug = false;
        int        i;

        Console.WriteLine("");
        Console.WriteLine("TEST01:");
        Console.WriteLine("  Interpolate data from TEST_INTERP problem #" + prob + "");
        Console.WriteLine("  using radial basis function \"" + phi_name + "\".");
        Console.WriteLine("  Scale factor R0 = " + r0 + "");

        int nd = TestInterp.p00_data_num(prob);

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

        double[] xy = TestInterp.p00_data(prob, 2, nd);

        switch (debug)
        {
        case true:
            typeMethods.r8mat_transpose_print(2, nd, xy, "  Data array:");
            break;
        }

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

        int m = 1;

        double[] w = RadialBasisFunctions.rbf_weight(m, nd, xd, r0, phi, yd);
        //
        //  #1:  Does interpolant match function at interpolation points?
        //
        int ni = nd;

        double[] xi        = typeMethods.r8vec_copy_new(ni, xd);
        double[] yi        = RadialBasisFunctions.rbf_interp(m, nd, xd, r0, phi, w, ni, xi);
        double   int_error = typeMethods.r8vec_norm_affine(ni, yi, yd) / ni;

        Console.WriteLine("");
        Console.WriteLine("  L2 interpolation error averaged per interpolant node = " + int_error + "");

        //
        //  #2: Compare estimated curve length to piecewise linear (minimal) curve length.
        //  Assume data is sorted, and normalize X and Y dimensions by (XMAX-XMIN) and
        //  (YMAX-YMIN).
        //
        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);

        ni = 501;
        xi = typeMethods.r8vec_linspace_new(ni, xmin, xmax);
        yi = RadialBasisFunctions.rbf_interp(m, nd, xd, r0, phi, w, ni, xi);

        double ld = 0.0;

        for (i = 0; i < nd - 1; i++)
        {
            ld += Math.Sqrt(Math.Pow((xd[i + 1] - xd[i]) / (xmax - xmin), 2)
                            + Math.Pow((yd[i + 1] - yd[i]) / (ymax - ymin), 2));
        }

        double li = 0.0;

        for (i = 0; i < ni - 1; i++)
        {
            li += Math.Sqrt(Math.Pow((xi[i + 1] - xi[i]) / (xmax - xmin), 2)
                            + Math.Pow((yi[i + 1] - yi[i]) / (ymax - ymin), 2));
        }

        Console.WriteLine("  Normalized length of piecewise linear interpolant = " + ld + "");
        Console.WriteLine("  Normalized length of polynomial interpolant       = " + li + "");
    }