Fitting two-yuan multiple curves with least squares method

Source: Internet
Author: User
Tags pow

The original source is unknown, the mathematical principle probably does not understand, the code is valid. The function is known as a pile of sample data, fitted with an approximate 2-yuan n-th function expression.

 public class Line {///<summary>///fitting two-yuan multiple curves with least squares///</summary>///<p Aram Name= "Arrx" > set of x coordinates of known points </param>///<param name= "Arry" > y-coordinate set of known points </param>///<par Am name= "Length" > number of known points </param>///<param name= "Dimension" > equation highest number of </param> public St  Atic double[] MultiLine (double[] Arrx, double[] arry, int length, int dimension)//two yuan multiple linear equation fitting curve {int n                  = dimension + 1;      The dimension equation requires dimension+1 coefficients double[,] guass = new Double[n, n + 1];
                Gaussian matrices such as: y=a0+a1*x+a2*x*x for (int i = 0; i < n; i++) {int J;
                for (j = 0; J < N; j +) {guass[i, j] = Sumarr (Arrx, j + i, length);
            Guass[i, J] = Sumarr (Arrx, I, Arry, 1, length);

        Return Computgauss (Guass, N);

    }    public static double Sumarr (double[] arr, int n, int length)//n-th square of the elements of an array and {double s = 0; for (int i = 0; i < length; i++) {if (Arr[i]!= 0 | | | n!= 0) s
                = S + Math.pow (Arr[i], n);
            else S = s + 1;
        return s; public static double Sumarr (double[] arr1, int n1, double[] arr2, int n2, int length) {Doub
            Le s = 0; for (int i = 0; i < length; i++) {if ((arr1[i)!= 0 | | N1!= 0) && (arr2[i]!= 0 | |
                N2!= 0)) s = s + math.pow (arr1[i), N1) * MATH.POW (Arr2[i), N2);
            else S = s + 1;

        return s;
            public static double[] Computgauss (double[,] guass, int n) {int i, J;
            int k, M;
            Double temp;
           Double Max; Double S;

            double[] x = new Double[n]; for (i = 0; i < n; i++) X[i] = 0.0;//initialization for (j = 0; J < N; j +) {max = 0

                ;
                K = J; 
                        for (i = j; i < n; i++) {if (Math.Abs (Guass[i, j]) > Max) {
                        Max = Guass[i, j];
                    K = i;
                    } if (k!= j) {for (M = j; m < n + 1; m++)
                        {temp = guass[j, M];
                        Guass[j, M] = guass[k, M];

                    Guass[k, m] = temp; 

                    } if (0 = max) {//"This linear equation is singular linear equation"
                return x; for (i = j + 1; i < n; i++) {s = GuasS[i, j];  for (M = j; m < n + 1; m++) {guass[i, M] = guass[i, M]-guass[j, m] * S/

                    (Guass[j, j]);
                }}//end for (j=0;j<n;j++) for (i = n-1; I >= 0; i--) {
                s = 0;
                for (j = i + 1; j < N; j +) {s = s + guass[i, j] * X[j];

            } X[i] = (guass[i, n]-s)/guass[i, I];
        return x; }
    }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.