Example #1
0
    static void PolynomialTest()
    {
        // y = -x*x + 2*x + 3
        double[] X = { 1, 2, 3, 8 };
        double[] Y = { 4, 3, 0, -45 };

        // f(x) = A*x*x + B*x + C
        GaussNewton.F f = delegate(double[] coefficients, double x)
        {
            return(coefficients[0] * x * x + coefficients[1] * x + coefficients[2]);
        };

        GaussNewton gaussNewton = new GaussNewton(3);

        gaussNewton.Initialize(Y, X, f);
        double[] answer = gaussNewton.Coefficients;     //A=-1 B=2 C=3
    }
Example #2
0
    static void _Main()
    {
        double[] Y =
        {
            9999992.348,
            9999992.35,
            9999992.354,
            9999992.359,
            9999992.361,
            9999992.365,
            9999992.366,
            9999992.37,
            9999992.371,
            9999992.374,
            9999992.376,
            9999992.377,
            9999992.379,
            9999992.38,
            9999992.382,
            9999992.384,
            9999992.386,
            9999992.387,
            9999992.389,
            9999992.39,
            9999992.39,
            9999992.392,
            9999992.392
        };
        double[] X = new double[Y.Length];
        for (int i = 0; i < X.Length; i++)
        {
            X[i] = i + 1;
        }

        // f(x) = A ln(x) + B
        GaussNewton.F f = delegate(double[] coefficients, double x)
        {
            return(coefficients[0] * Math.Log(x) + coefficients[1]);
        };
        GaussNewton gaussNewton = new GaussNewton(2);

        gaussNewton.Initialize(Y, X, f);
        double[] answer = gaussNewton.Coefficients;     //answer[0] = 0.016 answer[1]=9999992.3386
    }
Example #3
0
        private static void PolynomialTest()
        {
            //var source = "5.857486213 6.882938361 5.938018732 7.418233591 8.102131936 10.02132021 11.30071133 13.07283147 15.03172119 17.62765193 20.33497935 23.3258717 26.49699319 29.9783162 32.92265494 35.10837941 36.3501758 36.53764154 34.1757566 30.89822951 26.63771337 22.32763029 18.38308201 14.90463905 12.35598901 9.741861183 7.720235644 6.033749358 4.565486063 3.445948379 2.494921555 1.82715694 1.368045582 0.965480591 0.722276257 0.577920119 0.448848566 0.361900586 0.29215714 0.242289617 0.202792727 0.166097863 0.069075582 0.002831501 0.000875038 0.043841538";
            //var range = string.Join("\r\n", Enumerable.Range(1, 14));
            var datas       = new Datas();
            var sourceDatas = datas.LoadSourceDatas();

            foreach (var data in sourceDatas)
            {
                var values = data.Skip(32).Take(6);

                double[] X = Enumerable.Range(1, 6).Select(r => (double)r).ToArray();
                double[] Y = values.ToArray();

                // f(x) = A*x*x + B*x + C
                GaussNewton.F f = delegate(double[] coefficients, double x)
                {
                    return(coefficients[0] * x * x + coefficients[1] * x + coefficients[2]);
                };

                GaussNewton gaussNewton = new GaussNewton(3);
                gaussNewton.Initialize(Y, X, f);
                double[] answer = gaussNewton.Coefficients;

                List <double> result = new List <double>();
                for (int i = 1; i < 15; i++)
                {
                    result.Add(answer[0] * i * i + answer[1] * i + answer[2]);
                }

                for (int i = 0; i < 8; i++)
                {
                    data[data.Count - 1 - i] = result[result.Count - 1 - i];
                }

                SetUnitary(data);
            }

            datas.SaveResultDatas(sourceDatas, "PolynomialData.txt");
        }