Ejemplo n.º 1
0
        public void MultipleTests_Examples()
        {
            // Let's say we would like to compute the definite
            // integral of the function f(x) = cos(x) from -1 to 1

            Func <double, double> f = (x) => Math.Cos(x);

            double a = -1;
            double b = +1;

            double trapez  = TrapezoidalRule.Integrate(f, a, b, steps: 1000); // 1.6829414
            double romberg = RombergMethod.Integrate(f, a, b);                // 1.6829419
            double nagk    = NonAdaptiveGaussKronrod.Integrate(f, a, b);      // 1.6829419

            // Now let's say we would like to compute an improper integral
            // from -infinite to +infinite, such as the integral of a Gaussian
            // PDF (which should evaluate to 1):

            Func <double, double> g = (x) => (1 / Math.Sqrt(2 * Math.PI)) * Math.Exp(-(x * x) / 2);

            double iagk = InfiniteAdaptiveGaussKronrod.Integrate(g,
                                                                 Double.NegativeInfinity, Double.PositiveInfinity); // Output should be 0.99999...


            Assert.AreEqual(1.6829414086350976, trapez); // 1.6829414086350976
            Assert.AreEqual(1.682941969615797, romberg); // 1.682941969615797
            Assert.AreEqual(1.6829419595739716, nagk);   // 1.6829419595739716
            Assert.AreEqual(0.99999999999999978, iagk);  // 0.99999999999999978
        }
Ejemplo n.º 2
0
        public void GaussKronrodTest()
        {
            double expected = Math.Sin(2);
            double actual   = NonAdaptiveGaussKronrod.Integrate(Math.Cos, 0, 2);

            Assert.AreEqual(expected, actual, 1e-6);
        }