Ejemplo n.º 1
0
        public void TestInterpolationMethod_NevillePolynomial()
        {
            double[] t = new double[] { 0.0, 1.0, 3.0, 4.0 };
            double[] x = new double[] { 0.0, 3.0, 1.0, 3.0 };

            IInterpolationMethod method = Interpolation.CreatePolynomial(t, x);

            Assert.IsInstanceOfType(typeof(PolynomialInterpolation), method, "Type");

            double dx, d2x;

            for (int i = 0; i < t.Length; i++)
            {
                // verify the interpolated values exactly at the sample points.
                Assert.AreEqual(x[i], method.Interpolate(t[i]), "A Exact Point " + i.ToString());
                Assert.AreEqual(x[i], method.Differentiate(t[i], out dx, out d2x), "B Exact Point " + i.ToString());
            }

            // Maple: "with(CurveFitting);"
            // Maple: "evalf(subs({x=0.1},PolynomialInterpolation([[0,0],[1,3],[3,1],[4,3]], x)),20);"
            NumericAssert.AreAlmostEqual(.57225000000000000000, method.Interpolate(0.1), 1e-15, "A 0.1");
            NumericAssert.AreAlmostEqual(.57225000000000000000, method.Differentiate(0.1, out dx, out d2x), 1e-15, "B 0.1");
            NumericAssert.AreAlmostEqual(1.8840000000000000000, method.Interpolate(0.4), 1e-15, "A 0.4");
            NumericAssert.AreAlmostEqual(1.8840000000000000000, method.Differentiate(0.4, out dx, out d2x), 1e-15, "B 0.4");
            NumericAssert.AreAlmostEqual(3.0314166666666666667, method.Interpolate(1.1), 1e-15, "A 1.1");
            NumericAssert.AreAlmostEqual(3.0314166666666666667, method.Differentiate(1.1, out dx, out d2x), 1e-15, "B 1.1");
            NumericAssert.AreAlmostEqual(1.034666666666666667, method.Interpolate(3.2), 1e-15, "A 3.2");
            NumericAssert.AreAlmostEqual(1.034666666666666667, method.Differentiate(3.2, out dx, out d2x), 1e-15, "B 3.2");
            NumericAssert.AreAlmostEqual(6.281250000000000000, method.Interpolate(4.5), 1e-15, "A 4.5");
            NumericAssert.AreAlmostEqual(6.281250000000000000, method.Differentiate(4.5, out dx, out d2x), 1e-15, "B 4.5");
            NumericAssert.AreAlmostEqual(277.50000000000000000, method.Interpolate(10.0), 1e-15, "A 10.0");
            NumericAssert.AreAlmostEqual(277.50000000000000000, method.Differentiate(10.0, out dx, out d2x), 1e-15, "B 10.0");
            NumericAssert.AreAlmostEqual(-1010.8333333333333333, method.Interpolate(-10.0), 1e-15, "A -10.0");
            NumericAssert.AreAlmostEqual(-1010.8333333333333333, method.Differentiate(-10.0, out dx, out d2x), 1e-15, "B -10.0");
        }
        public void TestInterpolationMethod_NevillePolynomial()
        {
            double[] t = new double[] { 0.0, 1.0, 3.0, 4.0 };
            double[] x = new double[] { 0.0, 3.0, 1.0, 3.0 };

            IInterpolationMethod method = Interpolation.CreatePolynomial(t, x);

            Assert.That(method, Is.TypeOf(typeof(PolynomialInterpolation)), "Type");

            double dx, d2x;

            for (int i = 0; i < t.Length; i++)
            {
                // verify the interpolated values exactly at the sample points.
                Assert.That(method.Interpolate(t[i]), Is.EqualTo(x[i]), "A Exact Point " + i.ToString());
                Assert.That(method.Differentiate(t[i], out dx, out d2x), Is.EqualTo(x[i]), "B Exact Point " + i.ToString());
            }

            // Maple: "with(CurveFitting);"
            // Maple: "evalf(subs({x=0.1},PolynomialInterpolation([[0,0],[1,3],[3,1],[4,3]], x)),20);"
            Assert.That(method.Interpolate(0.1), NumericIs.AlmostEqualTo(.57225000000000000000, 1e-15), "A 0.1");
            Assert.That(method.Differentiate(0.1, out dx, out d2x), NumericIs.AlmostEqualTo(.57225000000000000000, 1e-15), "B 0.1");
            Assert.That(method.Interpolate(0.4), NumericIs.AlmostEqualTo(1.8840000000000000000, 1e-15), "A 0.4");
            Assert.That(method.Differentiate(0.4, out dx, out d2x), NumericIs.AlmostEqualTo(1.8840000000000000000, 1e-15), "B 0.4");
            Assert.That(method.Interpolate(1.1), NumericIs.AlmostEqualTo(3.0314166666666666667, 1e-15), "A 1.1");
            Assert.That(method.Differentiate(1.1, out dx, out d2x), NumericIs.AlmostEqualTo(3.0314166666666666667, 1e-15), "B 1.1");
            Assert.That(method.Interpolate(3.2), NumericIs.AlmostEqualTo(1.034666666666666667, 1e-15), "A 3.2");
            Assert.That(method.Differentiate(3.2, out dx, out d2x), NumericIs.AlmostEqualTo(1.034666666666666667, 1e-15), "B 3.2");
            Assert.That(method.Interpolate(4.5), NumericIs.AlmostEqualTo(6.281250000000000000, 1e-15), "A 4.5");
            Assert.That(method.Differentiate(4.5, out dx, out d2x), NumericIs.AlmostEqualTo(6.281250000000000000, 1e-15), "B 4.5");
            Assert.That(method.Interpolate(10.0), NumericIs.AlmostEqualTo(277.50000000000000000, 1e-15), "A 10.0");
            Assert.That(method.Differentiate(10.0, out dx, out d2x), NumericIs.AlmostEqualTo(277.50000000000000000, 1e-15), "B 10.0");
            Assert.That(method.Interpolate(-10.0), NumericIs.AlmostEqualTo(-1010.8333333333333333, 1e-15), "A -10.0");
            Assert.That(method.Differentiate(-10.0, out dx, out d2x), NumericIs.AlmostEqualTo(-1010.8333333333333333, 1e-15), "B -10.0");

            // Test Linear Case
            for (int k = 2; k < 7; k++)
            {
                double[] linx, liny, linxtest, linytest;
                BuildLinearCase(2, k, out linx, out liny, out linxtest, out linytest);
                IInterpolationMethod linearMethod = Interpolation.CreatePolynomial(linx, liny);
                for (int i = 0; i < linxtest.Length; i++)
                {
                    Assert.That(linearMethod.Interpolate(linxtest[i]), NumericIs.AlmostEqualTo(linytest[i], 1e-12), String.Format("Linear k={0} i={1}", k, i));
                }
            }
        }