public void SinFirstDerivativeAtZeroTest()
        {
            Func <double, double> f = Math.Sin;
            var df = new NumericalDerivative();

            Assert.AreEqual(1, df.EvaluateDerivative(f, 0, 1), 1e-10);
        }
 public void CubicPolynomialFunctionValueTest()
 {
     Func<double, double> f = x => 3 * Math.Pow(x, 3) + 2 * x - 6;
     var current = f(2);
     var df = new NumericalDerivative(3, 0);
     Assert.AreEqual(38, df.EvaluateDerivative(f, 2, 1, current), 1e-8);
 }
        public void CubicPolynomialFunctionValueTest()
        {
            Func <double, double> f = x => 3 * Math.Pow(x, 3) + 2 * x - 6;
            var current             = f(2);
            var df = new NumericalDerivative(3, 0);

            Assert.AreEqual(38, df.EvaluateDerivative(f, 2, 1, current), 1e-8);
        }
Esempio n. 4
0
        public static void GetError(Double fromInterval, Double toInterval, int n)
        {
            var derivative = new NumericalDerivative((n + 2), 0);
            var M          = derivative.EvaluateDerivative(Constants.Function, Math.Abs(fromInterval), n + 1);

            var R = (M * Math.Pow(Math.Abs(fromInterval) - 0, n + 1)) / SpecialFunctions.Factorial(n + 1);

            Console.WriteLine("Kai x priklauso intervalui [{0}, {1}], tai R{2}={3}", fromInterval, toInterval, n, R);
        }
Esempio n. 5
0
        public void CubicPolynomialFunctionValueTest()
        {
            double Func(double x) => 3 * Math.Pow(x, 3) + 2 * x - 6;

            var current = Func(2);
            var df      = new NumericalDerivative(3, 0);//CentredFiniteDifferenceDerivative(3, 0);

            Assert.AreEqual(38, df.EvaluateDerivative(Func, 2, 1, current), 1e-8);
        }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="td"></param>
        /// <param name="Ld"></param>
        /// <returns></returns>
        public static double PwDbLinearSealingFaultDerivative(double td, double Ld)
        {
            Condition.Requires(td, nameof(td)).IsGreaterOrEqual(0.0);
            Condition.Requires(Ld, nameof(Ld)).IsGreaterThan(0.0);

            var nd = new NumericalDerivative();
            var d  = nd.EvaluateDerivative(c => PwDbLinearSealingFault(c, Ld), td, 1);

            return(d * td);
        }
Esempio n. 7
0
        /// <summary>
        /// Evaluating Bucketed Delta
        /// </summary>
        /// <returns>The bucketed delta</returns>
        protected virtual Decimal EvaluateBucketedDelta1()
        {
            var solution            = new NumericalDerivative();
            Func <double, double> f = BucketedDeltaTargetFunction;
            var rate  = FloatingIndex;
            var dRate = Decimal.ToDouble(rate);
            var delta = (Decimal)solution.EvaluateDerivative(f, dRate, 1);

            return(delta / BasisPoint);
        }
Esempio n. 8
0
        /// <summary>
        /// Evaluating Bucketed Delta
        /// </summary>
        /// <returns>The bucketed delta</returns>
        public Decimal GetBucketedDelta1(decimal forwardRate)
        {
            //var realFunction = new RealFunction(BucketedDeltaTargetFunction);
            var rate      = forwardRate;
            var dRate     = Decimal.ToDouble(rate);
            var numerical = new NumericalDerivative();
            var delta     = (Decimal)numerical.EvaluateDerivative(BucketedDeltaTargetFunction, dRate, 1);

            return(delta / 10000);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="td"></param>
        /// <param name="cd"></param>
        /// <param name="skinFactor"></param>
        /// <returns></returns>
        public static double PwdRDerivative(double td, double cd, double skinFactor)
        {
            Condition.Requires(td, nameof(td)).IsGreaterOrEqual(0.0);
            Condition.Requires(cd, nameof(cd)).IsGreaterOrEqual(0.0);

            var nd = new NumericalDerivative();
            var d  = nd.EvaluateDerivative(c => PwdR(c, cd, skinFactor), td, 1);

            return(d * td);
        }
 public void CubicPolynomialThirdDerivativeAtAnyValTest()
 {
     Func<double, double> f = x => 3 * Math.Pow(x, 3) + 2 * x - 6;
     var df = new NumericalDerivative(5, 2);
     Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
     Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
     df.Center = 0;
     Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
     Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
     df.Center = 1;
     Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
     Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
     df.Center = 2;
     Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
     Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
     df.Center = 3;
     Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
     Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
     df.Center = 4;
     Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
     Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
 }
Esempio n. 11
0
        /// <summary>
        /// PTA derivative: dP * time
        /// </summary>
        /// <param name="time">elapsed time, hours</param>
        /// <param name="q">flow rate at surface, [STB/D]</param>
        /// <returns></returns>
        public double PressureDropDerivative(double time, double q)
        {
            var nd = new NumericalDerivative();

            if (time > 0.01)
            {
                nd.StepType = StepType.Relative;
                nd.Epsilon  = 1e-6;
            }

            var dP = nd.EvaluateDerivative(x => PressureDrop(x, q), time, 1);

            return(dP * time);
        }
        /// <summary>
        /// Evaluating the vector of Bucketed Delta
        /// </summary>
        /// <returns>The vector of bucketed delta</returns>
        protected override Decimal[] EvaluateBucketedDeltaVector()
        {
            Func <double, double> f           = BucketedDeltaTargetFunction;
            var           df                  = new NumericalDerivative();
            var           bucketedRates       = EvaluatedBucketedRates();
            var           bucketedDeltaVector = new decimal[bucketedRates.Length];
            const Decimal cDefault            = 0.0m;

            if (bucketedRates.Length == 0 && bucketedRates[0] == cDefault)
            {
                bucketedDeltaVector[0] = cDefault;
            }
            else
            {
                for (var index = bucketedRates.Length - 1; index >= 0; index--)
                {
                    var rate  = bucketedRates[index];
                    var dRate = Decimal.ToDouble(rate);
                    var temp  = (Decimal)df.EvaluateDerivative(f, dRate, 1);
                    bucketedDeltaVector[index] = temp / BasisPoint;
                }
            }
            return(bucketedDeltaVector);
        }
        public void CubicPolynomialThirdDerivativeAtAnyValTest()
        {
            Func <double, double> f = x => 3 * Math.Pow(x, 3) + 2 * x - 6;
            var df = new NumericalDerivative(5, 2);

            Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
            df.Center = 0;
            Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
            df.Center = 1;
            Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
            df.Center = 2;
            Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
            df.Center = 3;
            Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
            df.Center = 4;
            Assert.AreEqual(18, df.EvaluateDerivative(f, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(f, 10, 3));
        }
 public void SinFirstDerivativeAtZeroTest()
 {
     Func<double, double> f = Math.Sin;
     var df = new NumericalDerivative();
     Assert.AreEqual(1, df.EvaluateDerivative(f, 0, 1), 1e-10);
 }
Esempio n. 15
0
 public static double GetNthOrderDerivativeValueAt0(int n)
     {
     var derivative = new NumericalDerivative(6, 0);
     return derivative.EvaluateDerivative(Constants.Function, 0, n);
     }
Esempio n. 16
0
        public void CubicPolynomialThirdDerivativeAtAnyValTest()
        {
            double Func(double x) => 3 * Math.Pow(x, 3) + 2 * x - 6;

            var df = new NumericalDerivative(5, 2);//CentredFiniteDifferenceDerivative(5, 2)

            Assert.AreEqual(18, df.EvaluateDerivative(Func, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(Func, 10, 3));
            df.Center = 0;
            Assert.AreEqual(18, df.EvaluateDerivative(Func, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(Func, 10, 3));
            df.Center = 1;
            Assert.AreEqual(18, df.EvaluateDerivative(Func, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(Func, 10, 3));
            df.Center = 2;
            Assert.AreEqual(18, df.EvaluateDerivative(Func, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(Func, 10, 3));
            df.Center = 3;
            Assert.AreEqual(18, df.EvaluateDerivative(Func, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(Func, 10, 3));
            df.Center = 4;
            Assert.AreEqual(18, df.EvaluateDerivative(Func, 0, 3));
            Assert.AreEqual(18, df.EvaluateDerivative(Func, 10, 3));
        }