Esempio 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
        }
Esempio n. 2
0
        private void Defuzzificate()
        {
            Func <double, double> numeratorSubFunction = (x) => x *JointFunctions.GetValue(x);

            Func <double, double> denumeratorSubFunction = (x) => JointFunctions.GetValue(x);

            double max = JointFunctions.MaxInputValue;
            double min = JointFunctions.MinInputValue;

            // Integrate!
            double numerator   = TrapezoidalRule.Integrate(numeratorSubFunction, min, max, steps: 100);
            double denumerator = TrapezoidalRule.Integrate(denumeratorSubFunction, min, max, steps: 100);

            Output = numerator / denumerator;
        }
Esempio n. 3
0
        public void TrapezoidalTest()
        {
            double actual;

            actual = TrapezoidalRule.Integrate(function1, 0, 2, 1);
            Assert.AreEqual(4.0486368718741526, actual, 1e-15);
            Assert.IsFalse(Double.IsNaN(actual));

            actual = TrapezoidalRule.Integrate(function1, 0, 2, 2);
            Assert.AreEqual(3.6081715993899337, actual, 1e-15);
            Assert.IsFalse(Double.IsNaN(actual));

            actual = TrapezoidalRule.Integrate(function1, 0, 2, 4);
            Assert.AreEqual(3.4971047822027077, actual, 1e-15);
            Assert.IsFalse(Double.IsNaN(actual));

            actual = TrapezoidalRule.Integrate(function1, 0, 2, 8);
            Assert.AreEqual(3.4692784302833672, actual, 1e-15);
            Assert.IsFalse(Double.IsNaN(actual));

            actual = TrapezoidalRule.Integrate(function1, 0, 2, 16);
            Assert.AreEqual(3.4623181105467689, actual, 1e-15);
            Assert.IsFalse(Double.IsNaN(actual));
        }