Interpolate() public static method

Interpolate - adapted from "Numerical Recipes in C"
public static Interpolate ( double xs, double ys, int n, int offset, double x ) : ValueWithError
xs double
ys double
n int
offset int
x double
return ValueWithError
Beispiel #1
0
        /// <summary>
        /// Romberg - adapted from "Numerical Recipes in C"
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public double Romberg(double min, double max)
        {
            int    MAX       = 20;
            double TOLERANCE = 1e-7;
            int    K         = 4;

            double[]       s      = new double[MAX + 1];
            double[]       h      = new double[MAX + 1];
            ValueWithError result = new ValueWithError(0, 0);

            h[0] = 1.0;
            for (int j = 1; j <= MAX; j++)
            {
                s[j - 1] = Trapezoid(min, max, j);
                if (j >= K)
                {
                    result = Polynomial.Interpolate(h, s, K, j - K, 0.0);
                    if (Math.Abs(result.Error) < TOLERANCE * result.Value)
                    {
                        break;
                    }
                }
                s[j] = s[j - 1];
                h[j] = 0.25 * h[j - 1];
            }

            return(result.Value);
        }
Beispiel #2
0
        /// <summary>
        /// Integrate the polynomial function from <paramref name="min"/> to <paramref name="min"/> using the Romberg rule.
        /// </summary>
        /// <param name="min">The lower limit of the integrate range.</param>
        /// <param name="max">The upper limit of the integrate range.</param>
        /// <returns></returns>
        /// <remarks>Romberg - adapted from "Numerical Recipes in C"</remarks>
        public double Romberg(double min, double max)
        {
            int maxCount = 20;
            int K        = 4;

            double[] s = new double[maxCount + 1];
            double[] h = new double[maxCount + 1];
            Tuple <double, double> result = Tuple.Create(0d, 0d);

            h[0] = 1.0;
            for (int j = 1; j <= maxCount; j++)
            {
                s[j - 1] = Trapezoid(min, max, j);
                if (j >= K)
                {
                    result = Polynomial.Interpolate(h, s, K, j - K, 0.0);
                    if (Math.Abs(result.Item2) < Tolerance * result.Item1)
                    {
                        break;
                    }
                }
                s[j] = s[j - 1];
                h[j] = 0.25 * h[j - 1];
            }

            return(result.Item1);
        }