コード例 #1
0
    private static void test02(int dim_num, Func <int, double[], double> func)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST02 tests P5_ND.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    26 February 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DIM_NUM, the spatial dimension.
    //
    //    Input, double FUNC ( int dim_num, double x[] ), evaluates the function
    //    to be integrated.
    //
    {
        int dim;
        int eval_num = 0;

        //
        //  Set the integration limits.
        //
        double[] a = new double[dim_num];
        double[] b = new double[dim_num];

        for (dim = 0; dim < dim_num; dim++)
        {
            a[dim] = 0.0;
        }

        for (dim = 0; dim < dim_num; dim++)
        {
            b[dim] = 1.0;
        }

        double result = P5.p5_nd(func, dim_num, a, b, ref eval_num);

        Console.WriteLine("  P5_ND:          "
                          + result.ToString("0.############").PadLeft(20)
                          + "  " + eval_num.ToString().PadLeft(8) + "");
    }
コード例 #2
0
    private static void test05(int dim_num, Func <int, double[], double> func)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST05 demonstrates how to refine multi-dimensional integration results.
    //
    //  Discussion:
    //
    //    This routine is only set up for DIM_NUM = 2 for now.
    //
    //    We are given a routine, NDP5, which will integrate over a
    //    DIM_NUM dimensional hypercube using a fixed method.  In order to
    //    improve the approximation to an integral, we can subdivide
    //    the hypercube and call NDP5 to integrate again over each of
    //    these regions.
    //
    //    The information that we gather can be used to tell us when
    //    to expect that we have achieved a certain degree of accuracy.
    //
    //    With a little more work, we could make this code adaptive.
    //    That is, it would only refine SOME of the subregions, where
    //    the approximation to the integral was still not good enough.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    26 February 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, integer DIM_NUM, the spatial dimension.
    //
    //    Input, double FUNC ( int dim_num, double x[] ), evaluates the function
    //    to be integrated.
    //
    {
        int dim;
        int eval_num = 0;
        int igrid;

        double[] a   = new double[dim_num];
        double[] b   = new double[dim_num];
        double[] xlo = new double[dim_num];
        double[] xhi = new double[dim_num];

        for (dim = 0; dim < dim_num; dim++)
        {
            xlo[dim] = 0.0;
        }

        for (dim = 0; dim < dim_num; dim++)
        {
            xhi[dim] = 1.0;
        }

        for (igrid = 1; igrid <= 6; igrid++)
        {
            int ngrid = (int)Math.Pow(2, igrid - 1);

            double result_total = 0.0;
            int    eval_total   = 0;

            int i;
            for (i = 1; i <= ngrid; i++)
            {
                a[0] = ((ngrid - i + 1) * xlo[0]
                        + (i - 1) * xhi[0])
                       / ngrid;

                b[0] = ((ngrid - i) * xlo[0]
                        + i * xhi[0])
                       / ngrid;

                int j;
                for (j = 1; j <= ngrid; j++)
                {
                    a[1] = ((ngrid - j + 1) * xlo[1]
                            + (j - 1) * xhi[1])
                           / ngrid;

                    b[1] = ((ngrid - j) * xlo[1]
                            + j * xhi[1])
                           / ngrid;

                    double result = P5.p5_nd(func, dim_num, a, b, ref eval_num);

                    result_total += result;
                    eval_total   += eval_num;
                }
            }

            Console.WriteLine("  P5_ND+:         "
                              + result_total.ToString("0.############").PadLeft(20)
                              + "  " + eval_total.ToString().PadLeft(8) + "");
        }
    }