Ejemplo n.º 1
0
        public void OdeNonlinear()
        {
            // y = \frac{y_0}{1 - y_0 (x - x_0)}
            Func <double, double, double> f = (double x, double y) => MoreMath.Sqr(y);

            int count = 0;
            OdeEvaluationSettings settings = new OdeEvaluationSettings()
            {
                RelativePrecision = 1.0E-8,
                EvaluationBudget  = 1024,
                Listener          = (OdeResult) => count++
            };
            OdeResult result = FunctionMath.IntegrateOde(f, 0.0, 1.0, 0.99, settings);

            Assert.IsTrue(TestUtilities.IsNearlyEqual(result.Y, 1.0 / (1.0 - 1.0 * (0.99 - 0.0)), result.Settings));

            Assert.IsTrue(count > 0);

            Console.WriteLine(result.EvaluationCount);
        }
Ejemplo n.º 2
0
        public void OdeSine()
        {
            // The sine and cosine functions satisfy
            //   y'' = - y
            // This is perhaps the simplest conservative differential equation.
            // (i.e. right hand side depends only on y, not y')

            Func <double, double, double> f        = (double x, double y) => - y;
            OdeEvaluationSettings         settings = new OdeEvaluationSettings()
            {
                Listener = (OdeResult r) => {
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(
                                      MoreMath.Sqr(r.Y) + MoreMath.Sqr(r.YPrime), 1.0, r.Settings
                                      ));
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(
                                      r.Y, MoreMath.Sin(r.X), r.Settings
                                      ));
                }
            };
            OdeResult result = FunctionMath.IntegrateConservativeOde(f, 0.0, 0.0, 1.0, 5.0, settings);

            Assert.IsTrue(TestUtilities.IsNearlyEqual(result.Y, MoreMath.Sin(5.0)));
            Console.WriteLine(result.EvaluationCount);
        }