Accord.Math.Differentiation.FiniteDifferences.Interpolate C# (CSharp) Method

Interpolate() private static method

Interpolates the points to obtain an estimative of the derivative at x.
private static Interpolate ( double coefficients, double points, int order, int center, double step ) : double
coefficients double
points double
order int
center int
step double
return double
        private static double Interpolate(double[][,] coefficients, double[] points,
            int order, int center, double step)
        {
            double sum = 0.0;

            for (int i = 0; i < points.Length; i++)
            {
                double c = coefficients[center][order, i];

                if (c != 0)
                    sum += c * points[i];
            }

            return sum / Math.Pow(step, order);
        }

Usage Example

Beispiel #1
0
        /// <summary>
        ///   Computes the derivative for a simpler unidimensional function.
        /// </summary>
        ///
        /// <param name="function">The function to be differentiated.</param>
        /// <param name="order">The derivative order that should be obtained. Default is 1.</param>
        /// <param name="stepSize">The relative step size used to approximate the derivatives. Default is 0.01.</param>
        /// <param name="value">The value <c>x</c> at which the derivative should be evaluated.</param>
        ///
        /// <returns>The derivative of the function at the point <paramref name="value">x</paramref>.</returns>
        ///
        public static double Derivative(Func <double, double> function, double value, int order, double stepSize)
        {
            double output   = function(value);
            double original = value;

            if (original != 0.0)
            {
                stepSize *= System.Math.Abs(original);
            }


            // Create the interpolation points
            double[] outputs = new double[coefficientCache.Length];

            int center = (outputs.Length - 1) / 2;

            for (int i = 0; i < outputs.Length; i++)
            {
                if (i != center)
                {
                    // Recompute the function to measure its importance
                    outputs[i] = function(original + (i - center) * stepSize);
                }
                else
                {
                    // The center point is the original function
                    outputs[i] = output;
                }
            }

            return(FiniteDifferences.Interpolate(coefficientCache,
                                                 outputs, order, center, stepSize));
        }