Пример #1
0
    void CalculateLSM()
    {
        var xArr = new float[dots.Length];
        var yArr = new float[dots.Length];

        for (int i = 0; i < dots.Length; i++)
        {
            xArr[i] = dots[i].position.x;
            yArr[i] = dots[i].position.y;
        }

        lsm = new LSM(xArr, yArr);
        lsm.Polynomial(degreeOrder);

        lsmfunction = (x, y) =>
        {
            float yValue = 0;

            for (int i = 0; i < degreeOrder; i++)
            {
                yValue += Mathf.Pow(x, i) * lsm.Coeff[i];
            }
            return(new Vector2(x, yValue));
        };
    }
Пример #2
0
        public void LSMAbsenceOfDataException()
        {
            double[] xValue = new double[0] {
            };
            double[] yValue = new double[0] {
            };


            Assert.Throws <ArgumentException>(() =>
            {
                LSM lsm = new LSM(xValue, yValue);
            });
        }
Пример #3
0
        public void LSMPolinomPowEqualNumberOfPointsException()
        {
            double[] xValue = new double[5] {
                -1D, 0D, 1D, 2D, 3D
            };
            double[] yValue = new double[5] {
                3D, 2D, 3D, 6D, 11D
            };
            LSM lsm = new LSM(xValue, yValue);

            Assert.Throws <ArgumentException>(() =>
            {
                lsm.Polynomial(5);
            });
        }
Пример #4
0
        public void LSMPolinomPowLessThenOneException()
        {
            double[] xValue = new double[5] {
                -1D, 0D, 1D, 2D, 3D
            };
            double[] yValue = new double[5] {
                3D, 2D, 3D, 6D, 11D
            };
            LSM lsm = new LSM(xValue, yValue);

            Assert.Throws <ArgumentException>(() =>
            {
                lsm.Polynomial(0);
            });
        }
Пример #5
0
        public void LSMInverseMatrixMoesNotExistExceptio()
        {
            double[] xValue = new double[5] {
                -1D, -1D, -1D, -1D, 3D
            };
            double[] yValue = new double[5] {
                3D, 23, 3D, 3D, 3D
            };
            LSM lsm = new LSM(xValue, yValue);

            Assert.Throws <ArgumentException>(() =>
            {
                lsm.Polynomial(3);
            });
        }
Пример #6
0
        public void LSMEqualDimensionOfXandYException()
        {
            double[] xValue = new double[5] {
                -1D, 0D, 1D, 2D, 3D
            };
            double[] yValue = new double[4] {
                3D, 2D, 3D, 6D
            };


            Assert.Throws <ArgumentException>(() =>
            {
                LSM lsm = new LSM(xValue, yValue);
            });
        }
Пример #7
0
            static void Main(string[] args)
            {
                //Исходные данные
                double[] x     = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                double[] y     = new double[] { 8, 8, 0, 5, 5, 2, 4, 3, 8, 9, 6 };
                LSM      myReg = new LSM(x, y);

                // Апроксимация заданных значений линейным полиномом
                myReg.Polynomial(1);
                // Вывод коэффициентов b и а
                for (int i = 0; i < myReg.Coeff.Length; i++)
                {
                    Console.WriteLine(myReg.Coeff[i]);
                }
                Console.WriteLine();
                Console.ReadLine();
            }
Пример #8
0
        public void LSMEQuadraticEquation()
        {
            double[] xValue = new double[5] {
                -1D, 0D, 1D, 2D, 3D
            };
            double[] yValue = new double[5] {
                3D, 2D, 3D, 6D, 11D
            };
            LSM lsm = new LSM(xValue, yValue);

            lsm.Polynomial(3);
            var actual = lsm.Coeff;

            double[] expected = new double[4] {
                2D, 0D, 1D, 0D
            };
            Assert.That(actual, Is.EqualTo(expected).Within(0.0001));
        }
Пример #9
0
        public void LSMLinierEquation()
        {
            double[] xValue = new double[5] {
                1D, 2D, 3D, 4D, 5D
            };
            double[] yValue = new double[5] {
                1D, 2D, 3D, 4D, 5D
            };
            LSM lsm = new LSM(xValue, yValue);

            lsm.Polynomial(3);
            var actual = lsm.Coeff;

            double[] expected = new double[4] {
                0D, 1D, 0D, 0D
            };
            Assert.That(actual, Is.EqualTo(expected).Within(0.0001));
        }
Пример #10
0
    void squares_method(double[] x, double[] y)
    {
        LSM myReg = new LSM(x, y);

        double[] args = new double[4];
        // Апроксимация заданных значений линейным полиномом
        myReg.Polynomial(3);
        Debug.Log(myReg.Coeff.Length + " is len");
        for (int i = 0; i < myReg.Coeff.Length; i++)
        {
            args[i] = myReg.Coeff[i];
        }
        renders[0] = Instantiate(renderer);
        Color        c1           = new Color(255, 2, 0, 255);
        LineRenderer lineRenderer = renders[0].GetComponent <LineRenderer>();

        lineRenderer.material.color = c1;
        lineRenderer.material.SetFloat("_Metallic", 0.94f);
        lineRenderer.SetColors(c1, c1);
        print_graph_squares(args);
    }
Пример #11
0
        private void OnCalculateButtonClicked()
        {
            string failedMessage = "";
            bool   isFailed      = false;

            if (_yValues != null)
            {
                try
                {
                    double[] xValues = new double[_yValues.Length];
                    for (int i = 0; i < _yValues.Length; i++)
                    {
                        xValues[i] = i + 1;
                    }
                    LSM lsm = new LSM(xValues, _yValues);
                    lsm.Polynomial(3);
                    StringBuilder polinom = new StringBuilder();
                    for (int i = 0; i < lsm.Coeff.Length; i++)
                    {
                        polinom.Append("x " + i + "*" + lsm.Coeff[i].ToString("0.00") + " + ");
                    }
                    GraphicView.DrawLSM(CreateSetOfPointsFromPolinom(lsm.Coeff));
                }
                catch
                {
                    failedMessage += "МНК модели";
                    isFailed       = true;
                }

                try
                {
                    double[] xs = new double[_yValues.Length];
                    for (int i = 0; i < _yValues.Length; i++)
                    {
                        xs[i] = i + 1;
                    }
                    Lagrange lagrange = new Lagrange(xs, _yValues);

                    Vector2[] setOfPoints = new Vector2[Constants.PointsNumber];
                    for (int i = 0; i < setOfPoints.Length; i++)
                    {
                        double x      = (i + 10) / 10.0f;
                        double height = lagrange.GetValue(x);

                        setOfPoints[i] = new Vector3((float)x, (float)height, 0);
                    }
                    GraphicView.DrawLagrange(setOfPoints);
                }
                catch
                {
                    if (failedMessage.Length > 0)
                    {
                        failedMessage += ", ";
                    }
                    failedMessage += "Лагранж модели";
                    isFailed       = true;
                }
                if (isFailed)
                {
                    Text.text = "Ошибка при расчете " + failedMessage;
                }
            }
        }