(C#)曲线拟合的最小二乘法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 数值分析实验报告
{
    class Gauss曲线拟合的最小二乘法
    {
        static void Main()
        {
            Imput();
            wucha();
        }
        #region 曲线拟合的最小二乘法
        private static void Imput()
        {
            //Console.WriteLine("请输入数据对:");
            //string str = Console.ReadLine();
            //string[] x = str.Split(')');
            //for (int i = 0; i < x.Length; i++)
            //{
            //    double[] Xarr = new double[x.Length];
            //    double[] Yarr = new double[x.Length];
            //    string X = x[i].Replace("(", "");
            //    string[] XI = X.Split(',');
            //    Xarr[i] = double.Parse(XI[0]);
            //    Yarr[i] = double.Parse(XI[1]);
            //    Console.ReadLine();
            //}
            Console.WriteLine("请输入实验数据的x值:");
            string[] x = Console.ReadLine().Split(',');
            Console.WriteLine("请输入实验数据的y值:");
            string[] y = Console.ReadLine().Split(',');
            if (x.Length != y.Length)
            {
                Console.WriteLine("输入有误,输入的x值与y的值个数不对应!");
                return;
            }
            Console.WriteLine("请输入实验数据各个点的权函数值:");
            string[] w = Console.ReadLine().Split(',');
            if (x.Length != w.Length)
            {
                Console.WriteLine("输入有误,输入的w值与x和y的个数不对应!");
                return;
            }
            Console.WriteLine("请输入拟合曲线的最高项次数:");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine("实验数据的法方程为:");
            double[,] G = new double[n + 1, n + 2];
            for (int i = 0; i < n + 1; i++)
            {
                for (int j = 0; j < n + 2; j++)
                {
                    double X_num = 0;
                    double Y_Num = 0;
                    if (i + j == 0)
                    {
                        for (int m = 0; m < x.Length; m++)
                        {
                            X_num += double.Parse(w[m]);
                        }
                    }
                    else
                    {
                        for (int X = 0; X < x.Length; X++)
                        {
                            double N = 1;
                            for (int m = 0; m < i + j; m++)
                            {
                                if (m != n + 1)
                                {
                                    N = N * double.Parse(x[X]);
                                }
                            }
                            X_num += N * double.Parse(w[X]);
                            if (j == n + 1)
                            {
                                double P = 1;
                                for (int p = 0; p < i; p++)
                                {
                                    P = P * double.Parse(x[X]);
                                }
                                Y_Num += P * double.Parse(w[X]) * double.Parse(y[X]);
                            }
                        }
                    }
                    
                    if (j == n + 1)
                    {
                        G[i, j] = Y_Num;
                    }
                    else
                    {
                        G[i, j] = X_num;
                    }
                }
            }
            for (int i = 0; i < n + 1; i++)
            {
                for (int j = 0; j < n + 2; j++)
                {
                    Console.Write(G[i, j] + "\t");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }

        #endregion

        private static void wucha()
        {
            //Console.WriteLine("请输入上面最小二乘法拟合曲线的系数:");
            //string[] xishu = Console.ReadLine().Split(' ');
            double[] wucha=new double[12];
            double[] x = { 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 };
            double[] f = { 0, 1.27, 2.16, 2.86, 3.44, 3.87, 4.15, 4.37, 4.51, 4.58, 4.62, 4.64 };
            for (int i = 0; i < 12; i++)
            {
                double t1 = x[i];
                double t2 = x[i] * x[i];
                double t3 = x[i] * x[i] * x[i];
                double f1 = 1.34680 + 0.06876 * t1 + 5.30520 * t2 + 1.73252 * t3;
                wucha[i] = f1 - f[i];
                Console.WriteLine("拟合曲线与在点{0}处的值与真实值的误差为:\n",x[i]);
                Console.WriteLine(wucha[i]);
            }
            Console.ReadLine();
        }

    }
}
这个程序只是实现了根据已知点来得到方程组的矩阵,解方程组的程序实现还没有实现,不过可以参考上篇高斯列主元法解方程组的程序!
对于使用C#实现最小二乘法进行曲线拟合,你可以按照以下步骤进行操作: 1. 首先,确定你想要拟合的曲线类型,例如直线、多项式等。假设你选择的是多项式曲线拟合。 2. 创建一个C#类,并添加一个方法来执行最小二乘法拟合。该方法将接受输入的数据点集合和拟合的阶数作为参数,并返回拟合结果。 3. 在最小二乘法拟合方法中,首先计算数据点的总数以及自变量和因变量的和与平方和。 4. 接下来,根据拟合的阶数,构建一个系数矩阵和一个常数向量。系数矩阵的行数等于数据点的总数,列数等于拟合的阶数加1。 5. 使用数据点的自变量值和因变量值来填充系数矩阵和常数向量。每个元素的值等于自变量值的幂次方。 6. 利用最小二乘法的公式计算出系数矩阵的逆矩阵和最终的系数向量。 7. 最后,返回拟合的系数向量作为最小二乘法曲线拟合的结果。 这是一个简单的示例代码,用于执行多项式曲线拟合最小二乘法: ```csharp using System; using MathNet.Numerics.LinearAlgebra; public class LeastSquaresFitting { public static Vector<double> PolynomialFit(Vector<double> xData, Vector<double> yData, int order) { int n = xData.Count; int m = order + 1; Matrix<double> A = Matrix<double>.Build.Dense(n, m); Vector<double> b = Vector<double>.Build.Dense(n); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { A[i, j] = Math.Pow(xData[i], j); } b[i] = yData[i]; } Matrix<double> ATransposeA = A.TransposeAndMultiply(A); Vector<double> ATransposeb = A.TransposeThisAndMultiply(b); Vector<double> coefficients = ATransposeA.Solve(ATransposeb); return coefficients; } } ``` 在这个示例中,我们使用了MathNet.Numerics库来处理线性代数运算。你可以根据你的实际需求进行修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值