Beispiel #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
        }
Beispiel #2
0
        public void RombergTest()
        {
            // Example from http://www.mathstat.dal.ca/~tkolokol/classes/1500/romberg.pdf

            double actual;

            actual = RombergMethod.Integrate(function2, 1, 2, 1);
            Assert.AreEqual(0.75, actual);

            actual = RombergMethod.Integrate(function2, 1, 2, 2);
            Assert.AreEqual(0.69444444444444431, actual);

            actual = RombergMethod.Integrate(function2, 1, 2, 4);
            Assert.AreEqual(0.6931474776448322, actual);

            actual = RombergMethod.Integrate(function2, 1, 2, 8);
            Assert.AreEqual(0.6931471805599444, actual);

            actual = RombergMethod.Integrate(function2, 1, 2, 16);
            Assert.AreEqual(0.69314718055994151, actual);
        }