Ejemplo n.º 1
0
        public void TestInterpolationMethod_CubicSpline_BoundaryNatural()
        {
            double[] t = new double[] { -2.0, -1.0, 0.0, 1.0, 2.0 };
            double[] x = new double[] { 1.0, 2.0, -1.0, 0.0, 1.0 };

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

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

            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());
            }

            // Maple: "with(CurveFitting);"
            // Maple: "evalf(subs({x=-2.4},Spline([[-2,1],[-1,2],[0,-1],[1,0],[2,1]], x, degree=3, endpoints='natural')),20);"
            NumericAssert.AreAlmostEqual(.144000000000000000, method.Interpolate(-2.4), 1e-15, "A -2.4");
            NumericAssert.AreAlmostEqual(1.7906428571428571429, method.Interpolate(-0.9), 1e-15, "A -0.9");
            NumericAssert.AreAlmostEqual(.47321428571428571431, method.Interpolate(-0.5), 1e-15, "A -0.5");
            NumericAssert.AreAlmostEqual(-.80992857142857142857, method.Interpolate(-0.1), 1e-15, "A -0.1");
            NumericAssert.AreAlmostEqual(-1.1089285714285714286, method.Interpolate(0.1), 1e-15, "A 0.1");
            NumericAssert.AreAlmostEqual(-1.0285714285714285714, method.Interpolate(0.4), 1e-15, "A 0.4");
            NumericAssert.AreAlmostEqual(.30285714285714285716, method.Interpolate(1.2), 1e-15, "A 1.2");
            NumericAssert.AreAlmostEqual(189, method.Interpolate(10.0), 1e-15, "A 10.0");
            NumericAssert.AreAlmostEqual(677, method.Interpolate(-10.0), 1e-15, "A -10.0");
        }
Ejemplo n.º 2
0
        private List <Position> GetPoints(List <Position> points, int type)
        {
            try
            {
                List <Position>      result = new List <Position>();
                IInterpolationMethod method = null;
                List <double>        xs     = new List <double>();
                List <double>        ys     = new List <double>();
                if (type > 0)
                {
                    for (int i = 0; i < points.Count; i++)
                    {
                        xs.Add(points[i].X);
                        ys.Add(points[i].Z);
                    }
                }

                if (type == 0)
                {
                    result.AddRange(points);
                }
                else if (type == 1)
                {
                    method = Interpolation.CreateNaturalCubicSpline(xs, ys);
                }
                else if (type == 2)
                {
                    method = Interpolation.CreateAkimaCubicSpline(xs, ys);
                }

                if (method != null)
                {
                    for (int i = 0; i < points.Count; i++)
                    {
                        Position p1 = points[i];
                        Position p2 = points[i + 1];
                        double   x  = (p1.X + p2.X) / 2;
                    }
                }

                return(result);
            }
            catch (System.Exception ex)
            {
                Log.Error(hisTag, "GetPoints", "Exception:" + ex);
                return(null);
            }
        }
        public void TestInterpolationMethod_CubicSpline_BoundaryNatural()
        {
            double[] t = new double[] { -2.0, -1.0, 0.0, 1.0, 2.0 };
            double[] x = new double[] { 1.0, 2.0, -1.0, 0.0, 1.0 };

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

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

            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());
            }

            // Maple: "with(CurveFitting);"
            // Maple: "evalf(subs({x=-2.4},Spline([[-2,1],[-1,2],[0,-1],[1,0],[2,1]], x, degree=3, endpoints='natural')),20);"
            Assert.That(method.Interpolate(-2.4), NumericIs.AlmostEqualTo(.144000000000000000, 1e-15), "A -2.4");
            Assert.That(method.Interpolate(-0.9), NumericIs.AlmostEqualTo(1.7906428571428571429, 1e-15), "A -0.9");
            Assert.That(method.Interpolate(-0.5), NumericIs.AlmostEqualTo(.47321428571428571431, 1e-15), "A -0.5");
            Assert.That(method.Interpolate(-0.1), NumericIs.AlmostEqualTo(-.80992857142857142857, 1e-15), "A -0.1");
            Assert.That(method.Interpolate(0.1), NumericIs.AlmostEqualTo(-1.1089285714285714286, 1e-15), "A 0.1");
            Assert.That(method.Interpolate(0.4), NumericIs.AlmostEqualTo(-1.0285714285714285714, 1e-15), "A 0.4");
            Assert.That(method.Interpolate(1.2), NumericIs.AlmostEqualTo(.30285714285714285716, 1e-15), "A 1.2");
            Assert.That(method.Interpolate(10.0), NumericIs.AlmostEqualTo((double)189, 1e-15), "A 10.0");
            Assert.That(method.Interpolate(-10.0), NumericIs.AlmostEqualTo((double)677, 1e-15), "A -10.0");

            // Test Linear Case
            for (int k = 2; k < 6; k++)
            {
                double[] linx, liny, linxtest, linytest;
                BuildLinearCase(2, k + 1, out linx, out liny, out linxtest, out linytest);
                IInterpolationMethod linearMethod = Interpolation.CreateNaturalCubicSpline(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));
                }
            }
        }