Ejemplo n.º 1
0
    public static double[] lagrange_interp_2d(int mx, int my, double[] xd_1d, double[] yd_1d,
                                              double[] zd, int ni, double[] xi, double[] yi)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    LAGRANGE_INTERP_2D evaluates the Lagrange interpolant for a product grid.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    13 September 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int MX, MY, the polynomial degree in X and Y.
    //
    //    Input, double XD_1D[MX+1], YD_1D[MY+1], the 1D data locations.
    //
    //    Input, double ZD[(MX+1)*(MY+1)], the 2D array of data values.
    //
    //    Input, int NI, the number of 2D interpolation points.
    //
    //    Input, double XI[NI], YI[NI], the 2D interpolation points.
    //
    //    Output, double LAGRANGE_INTERP_2D[NI], the interpolated values.
    //
    {
        int k;

        double[] zi = new double[ni];

        for (k = 0; k < ni; k++)
        {
            int l = 0;
            zi[k] = 0.0;
            int j;
            for (j = 0; j < my + 1; j++)
            {
                int i;
                for (i = 0; i < mx + 1; i++)
                {
                    double lx = Lagrange1D.lagrange_basis_function_1d(mx, xd_1d, i, xi[k]);
                    double ly = Lagrange1D.lagrange_basis_function_1d(my, yd_1d, j, yi[k]);
                    zi[k] += zd[l] * lx * ly;
                    l     += 1;
                }
            }
        }
        return(zi);
    }
Ejemplo n.º 2
0
    public static double[] lagrange_interp_nd_value2(int m, int[] ind, double[] a, double[] b,
                                                     int nd, double[] zd, int ni, double[] xi)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    LAGRANGE_INTERP_ND_VALUE2 evaluates an ND Lagrange interpolant.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    30 September 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int M, the spatial dimension.
    //
    //    Input, int IND[M], the index or level of the 1D rule
    //    to be used in each dimension.
    //
    //    Input, double A[M], B[M], the lower and upper limits.
    //
    //    Input, int ND, the number of points in the product grid.
    //
    //    Input, double ZD[ND], the function evaluated at the points XD.
    //
    //    Input, int NI, the number of points at which the
    //    interpolant is to be evaluated.
    //
    //    Input, double XI[M*NI], the points at which the interpolant
    //    is to be evaluated.
    //
    //    Output, double ZI[NI], the interpolant evaluated at the
    //    points XI.
    //
    {
        int j;

        typeMethods.r8vecDPData data = new();

        double[] w  = new double[nd];
        double[] zi = new double[ni];

        for (j = 0; j < ni; j++)
        {
            int i;
            for (i = 0; i < nd; i++)
            {
                w[i] = 1.0;
            }

            for (i = 0; i < m; i++)
            {
                int      n    = Order.order_from_level_135(ind[i]);
                double[] x_1d = ClenshawCurtis.cc_compute_points(n);
                int      k;
                for (k = 0; k < n; k++)
                {
                    x_1d[k] = 0.5 * ((1.0 - x_1d[k]) * a[i]
                                     + (1.0 + x_1d[k]) * b[i]);
                }

                double[] value = Lagrange1D.lagrange_base_1d(n, x_1d, 1, xi, xiIndex: +i + j * m);
                typeMethods.r8vec_direct_product2(ref data, i, n, value, m, nd, ref w);
            }

            zi[j] = typeMethods.r8vec_dot_product(nd, w, zd);
        }

        return(zi);
    }
Ejemplo n.º 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 + "");
    }
Ejemplo n.º 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 + "");
    }