Beispiel #1
0
    private static void test01(int nt)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests CIRCLE_RULE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    06 April 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int[] e = new int[2];
        int   e1;

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  CIRCLE_RULE can compute a rule Q(f) for the unit circle");
        Console.WriteLine("  using NT equally spaced angles.");
        Console.WriteLine("  Estimate integrals I(f) where f = x^e(1) * y^e(2)");
        Console.WriteLine("  using " + nt + " points.");
        //
        //  Compute the quadrature rule.
        //
        double[] w = new double[nt];
        double[] t = new double[nt];

        QuadratureRule.circle_rule(nt, ref w, ref t);
        //
        //  Apply it to integrands.
        //
        Console.WriteLine("");
        Console.WriteLine("  E(1)  E(2)    I(f)            Q(f)");
        Console.WriteLine("");
        //
        //  Specify a monomial.
        //
        for (e1 = 0; e1 <= 6; e1 += 2)
        {
            e[0] = e1;

            int e2;
            for (e2 = e1; e2 <= 6; e2 += 2)
            {
                e[1] = e2;

                double q = 0.0;
                int    i;
                for (i = 0; i < nt; i++)
                {
                    double x = Math.Cos(t[i]);
                    double y = Math.Sin(t[i]);
                    q += w[i] * Math.Pow(x, e[0]) * Math.Pow(y, e[1]);
                }

                q = 2.0 * Math.PI * q;

                double exact = Integrals.circle01_monomial_integral(e);

                Console.WriteLine("  " + e[0].ToString(CultureInfo.InvariantCulture).PadLeft(2)
                                  + "  " + e[1].ToString(CultureInfo.InvariantCulture).PadLeft(2)
                                  + "  " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                                  + "  " + q.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
            }
        }
    }
Beispiel #2
0
    private static void test01()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 uses CIRCLE01_SAMPLE with an increasing number of points.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    12 January 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int m = 2;
        const int n = 4192;
        int       test;
        const int test_num = 20;

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  Use CIRCLE01_SAMPLE to compare exact and");
        Console.WriteLine("  estimated integrals along the circumference");
        Console.WriteLine("  of the unit circle in 2D.");
        //
        //  Get sample points.
        //
        int seed = 123456789;

        double[] x = Integrals.circle01_sample(n, ref seed);

        Console.WriteLine("");
        Console.WriteLine("  Number of sample points used is " + n + "");
        //
        //  Randomly choose X, Y exponents.
        //
        Console.WriteLine("");
        Console.WriteLine("  If any exponent is odd, the integral is zero.");
        Console.WriteLine("  We restrict this test to randomly chosen even exponents.");
        Console.WriteLine("");
        Console.WriteLine("  Ex  Ey     MC-Estimate           Exact      Error");
        Console.WriteLine("");

        for (test = 1; test <= test_num; test++)
        {
            int[] e = UniformRNG.i4vec_uniform_ab_new(m, 0, 5, ref seed);

            int i;
            for (i = 0; i < m; i++)
            {
                e[i] *= 2;
            }

            double[] value = Monomial.monomial_value(m, n, e, x);

            double result = Integrals.circle01_length() * typeMethods.r8vec_sum(n, value)
                            / n;
            double exact = Integrals.circle01_monomial_integral(e);
            double error = Math.Abs(result - exact);

            Console.WriteLine("  " + e[0].ToString(CultureInfo.InvariantCulture).PadLeft(2)
                              + "  " + e[1].ToString(CultureInfo.InvariantCulture).PadLeft(2)
                              + "  " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + error.ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }
    }
Beispiel #3
0
    private static void circle01_sample_random_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CIRCLE01_SAMPLE_RANDOM_TEST uses CIRCLE01_SAMPLE_RANDOM with an increasing number of points.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 January 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int[] e      = new int[2];
        int[] e_test =
        {
            0, 0,
            2, 0,
            0, 2,
            4, 0,
            2, 2,
            0, 4,
            6, 0
        }

        ;
        int i;
        int j;

        Console.WriteLine("");
        Console.WriteLine("CIRCLE0_SAMPLE_RANDOM_TEST");
        Console.WriteLine("  CIRCLE01_SAMPLE_RANDOM randomly samples the unit circle.");
        Console.WriteLine("  Use it to estimate integrals.");

        int seed = 123456789;

        Console.WriteLine("");
        Console.WriteLine("         N        1              X^2             Y^2" +
                          "             X^4           X^2Y^2          Y^4          X^6");
        Console.WriteLine("");

        int n = 1;

        while (n <= 65536)
        {
            double[] x    = MonteCarlo.circle01_sample_random(n, ref seed);
            string   cout = "  " + n.ToString(CultureInfo.InvariantCulture).PadLeft(8);
            for (j = 0; j < 7; j++)
            {
                for (i = 0; i < 2; i++)
                {
                    e[i] = e_test[i + j * 2];
                }

                double[] value = Monomial.monomial_value(2, n, e, x);

                double result = Integrals.circle01_length() * typeMethods.r8vec_sum(n, value) / n;
                cout += "  " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14);
            }

            Console.WriteLine(cout);

            n = 2 * n;
        }

        Console.WriteLine("");
        string cout2 = "     Exact";

        for (j = 0; j < 7; j++)
        {
            for (i = 0; i < 2; i++)
            {
                e[i] = e_test[i + j * 2];
            }

            double exact = Integrals.circle01_monomial_integral(e);
            cout2 += "  " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14);
        }

        Console.WriteLine(cout2);
    }