多项式拟合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32352825/article/details/86536418

多项式拟合C#

Class1.cs-拟合类

using System;
using System.Collections.Generic;
using System.Text;

namespace 最小二乘法拟合多项式
{
    class Class1
    {
        ///<summary>
        ///用最小二乘法拟合二元多次曲线
        ///</summary>
        ///<param name="arrX">已知点的x坐标集合</param>
        ///<param name="arrY">已知点的y坐标集合</param>
        ///<param name="length">已知点的个数</param>
        ///<param name="dimension">方程的最高次数</param>
        public double[] MultiLine(double[] arrX, double[] arrY, int length, int dimension)//二元多次线性方程拟合曲线
        {
            int n = dimension + 1;                  //dimension次方程需要求 dimension+1个 系数
            double[,] Guass = new double[n, n + 1];      //高斯矩阵 例如: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 double SumArr(double[] arr, int n, int length) //求数组的元素的n次方的和
        {
            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 double SumArr(double[] arr1, int n1, double[] arr2, int n2, int length)
        {
            double 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 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;//初始化

            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)
                {
                    // "此线性方程为奇异线性方程" 
                    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]);
                    }
                }

            }//结束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;
        }//返回值是函数的系数
        //例如:y=a0+a1*x 返回值则为a0 a1
        //例如:y=a0+a1*x+a2*x*x 返回值则为a0 a1 a2
    }
}

Progran.cs - 测试类

using System;
using System.Collections.Generic;
using System.Text;

namespace 最小二乘法拟合多项式
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] xCoe;

            double[] xLeastValue = {
0.005,
1.01,
2.019,
3.025,
4.027,
5.025,
6.016,
7.007,
7.999,
8.998,
10.006};
            double[] zLeastValue = {
0,
0.25,
0.5,
0.75,
1,
1.25,
1.5,
1.75,
2,
2.25,
2.5};
            //例如:y=a0+a1*x+a2*x*x 返回值则为a0 a1 a2
            Class1 sb = new Class1();
            xCoe = sb.MultiLine(xLeastValue, zLeastValue, 11, 5);
            Console.WriteLine("5阶多项式因子:");
            for(int i = 0;i < xCoe.Length; i++)
            {
                Console.WriteLine("a"+i+":"+xCoe[i]);
            }

            Console.WriteLine("多项式计算出来的位移值 标准位移值 两者差值 线性度:");
            for (int i = 0; i < xLeastValue.Length; i++)
            {
                double Y = xCoe[0] + xCoe[1] * Math.Pow(xLeastValue[i], 1) + xCoe[2] * Math.Pow(xLeastValue[i], 2) + xCoe[3] * Math.Pow(xLeastValue[i], 3)
                    + xCoe[4] * Math.Pow(xLeastValue[i], 4) + xCoe[5] * Math.Pow(xLeastValue[i], 5);
                double dev = Y - zLeastValue[i];
                double error = dev / 2.5;
                Console.WriteLine(Y + "   " + zLeastValue[i] + "  " + dev + "    " + error);
            }

             Console.ReadKey(true);
        }


    }
}

猜你喜欢

转载自blog.csdn.net/qq_32352825/article/details/86536418