Example #1
0
    private static void lageven_interp_1d_test(int prob, int nd)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    LAGEVEN_INTERP_1D_TEST tests LAGEVEN_INTERP_1D.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    30 September 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the problem index.
    //
    //    Input, int ND, the number of data points to use.
    //
    {
        Console.WriteLine("");
        Console.WriteLine("LAGEVEN_INTERP_1D_TEST:");
        Console.WriteLine("  LAGEVEN_INTERP_1D uses even spacing for data points.");
        Console.WriteLine("  Interpolate data from TEST_INTERP_1D problem #" + prob + "");
        Console.WriteLine("  Number of data points = " + nd + "");
        //
        //  Define the data.
        //
        const double a = 0.0;
        const double b = +1.0;

        double[] xd = typeMethods.r8vec_midspace_new(nd, a, b);
        double[] yd = Data_1D.p00_f(prob, nd, xd);

        switch (nd)
        {
        case < 10:
            typeMethods.r8vec2_print(nd, xd, yd, "  Data array:");
            break;
        }

        //
        //  #1:  Does the interpolant match the function at the interpolation points?
        //
        double[] xi = typeMethods.r8vec_copy_new(nd, xd);
        double[] yi = BarycentricInterp1D.lageven_interp_1d(nd, xd, yd, nd, xi);

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

        Console.WriteLine("");
        Console.WriteLine("  L2 interpolation error averaged per interpolant node = " + int_error + "");
    }
Example #2
0
    private static void test01(int prob, int nc, int nd)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests PWL_APPROX_1D.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    10 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the problem index.
    //
    //    Input, int NC, the number of control points.
    //
    //    Input, int ND, the number of data points.
    //
    {
        Console.WriteLine("");
        Console.WriteLine("TEST01:");
        Console.WriteLine("  Approximate data from TEST_INTERP_1D problem #" + prob + "");
        Console.WriteLine("  Number of control points = " + nc + "");
        Console.WriteLine("  Number of data points = " + nd + "");

        const double xmin = 0.0;
        const double xmax = 1.0;

        double[] xd = typeMethods.r8vec_linspace_new(nd, xmin, xmax);
        double[] yd = Data_1D.p00_f(prob, nd, xd);

        switch (nd)
        {
        case < 10:
            typeMethods.r8vec2_print(nd, xd, yd, "  Data array:");
            break;
        }

        //
        //  Determine control values.
        //
        double[] xc = typeMethods.r8vec_linspace_new(nc, xmin, xmax);
        double[] yc = Approx1D.pwl_approx_1d(nd, xd, yd, nc, xc);
        //
        //  #1:  Does approximant come close to function at data points?
        //
        double[] xi = typeMethods.r8vec_copy_new(nd, xd);
        double[] yi = Approx1D.pwl_interp_1d(nc, xc, yc, nd, xi);

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

        Console.WriteLine("");
        Console.WriteLine("  L2 approximation error averaged per data node = " + app_error + "");
    }
Example #3
0
    private static void test02(int prob, int m, int nd)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST02 tests LAGRANGE_APPROX_1D with evenly spaced data
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the problem index.
    //
    //    Input, int M, the polynomial approximant degree.
    //
    //    Input, int ND, the number of data points.
    //
    {
        Console.WriteLine("");
        Console.WriteLine("TEST02:");
        Console.WriteLine("  Approximate evenly spaced data from TEST_INTERP_1D problem #" + prob + "");
        Console.WriteLine("  Use polynomial approximant of degree " + m + "");
        Console.WriteLine("  Number of data points = " + nd + "");

        const double a = 0.0;
        const double b = 1.0;

        double[] xd = typeMethods.r8vec_linspace_new(nd, a, b);

        double[] yd = Data_1D.p00_f(prob, nd, xd);

        switch (nd)
        {
        case < 10:
            typeMethods.r8vec2_print(nd, xd, yd, "  Data array:");
            break;
        }

        //
        //  #1:  Does approximant come close to function at data points?
        //
        int ni = nd;

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

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

        Console.WriteLine("");
        Console.WriteLine("  L2 approximation error averaged per data node = " + int_error + "");
    }
Example #4
0
    private static void test01(int prob, int nd)

    //*****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests LAGRANGE_VALUE_1D with evenly spaced data.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    12 September 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the problem index.
    //
    //    Input, int ND, the number of data points to use.
    //
    {
        int i;

        Console.WriteLine("");
        Console.WriteLine("TEST01:");
        Console.WriteLine("  Interpolate data from TEST_INTERP_1D problem #" + prob + "");
        Console.WriteLine("  Use even spacing for data points.");
        Console.WriteLine("  Number of data points = " + nd + "");

        const double a = 0.0;
        const double b = 1.0;

        double[] xd = typeMethods.r8vec_linspace_new(nd, a, b);

        double[] yd = Data_1D.p00_f(prob, nd, xd);

        switch (nd)
        {
        case < 10:
            typeMethods.r8vec2_print(nd, xd, yd, "  Data array:");
            break;
        }

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

        double[] xi = typeMethods.r8vec_copy_new(ni, xd);
        double[] yi = Lagrange1D.lagrange_value_1d(nd, xd, yd, 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 ymin = typeMethods.r8vec_min(nd, yd);
        double ymax = typeMethods.r8vec_max(nd, yd);

        ni = 501;
        xi = typeMethods.r8vec_linspace_new(ni, a, b);
        yi = Lagrange1D.lagrange_value_1d(nd, xd, yd, ni, xi);

        double ld = 0.0;

        for (i = 0; i < nd - 1; i++)
        {
            ld += Math.Sqrt(Math.Pow((xd[i + 1] - xd[i]) / (b - a), 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]) / (b - a), 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 + "");
    }