TFLH/Model/LinearFittingModel.cs
2026-08-06 09:50:46 +08:00

260 lines
8.3 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
/// <summary>
/// 线性拟合
/// </summary>
public class LinearFittingModel
{
private double[,] gaussMatrix;
private int coe;
public LinearFittingModel()
{
}
public LinearFittingModel(double[] arrX, double[] arrY, int n)
{
coe = n;
gaussMatrix = GetGauss(GetXPowSum(arrX, n), GetXPowYSum(arrX, arrY, n), n);
}
public double[,] GetGaussMatrix()
{
return gaussMatrix;
}
public double[] GetResult()
{
return ComputeGauss(gaussMatrix, coe);
}
#region 计算获取x散点的幂次和数组
/// <summary> 计算获取x散点的幂次和数组 </summary> 
/// <param name="arrX">x散点序列</param> 
/// <param name="n">函数拟合次数</param> 
/// <returns></returns> 
protected double[] GetXPowSum(double[] arrX, int n)
{
int m = arrX.Length;//X散点的个数
double[] xPow = new double[2 * n + 1]; //存储X散点的幂次值
for (int i = 0; i < xPow.Length; i++)
{
if (i == 0)
{
xPow[i] = m;
}
else
{
//计算x的i次方和
double max = 0;
for (int j = 0; j < m; j++)
{
if (arrX[j] == 0)
max = max + 1;
else
max = max + Math.Pow(arrX[j], i);
}
xPow[i] = Math.Round(max, 4);
}
}
return xPow;
}
#endregion
#region 计算获取xy的幂次和序列
/// <summary>计算获取xy的幂次和序列 </summary> 
/// <param name="arrX">x散点序列</param> 
/// <param name="arrY">y散点序列</param> 
/// <param name="n">拟合曲线次数</param> 
/// <returns></returns> 
protected double[] GetXPowYSum(double[] arrX, double[] arrY, int n)
{
int m = arrX.Length;//X散点的个数
double[] xyPow = new double[n + 1]; //仓储X散点的幂次值
for (int i = 0; i < xyPow.Length; i++)
{
//计算xy的i次方和
double max = 0;
for (int j = 0; j < m; j++)
{
if (arrX[j] == 0)
max = max + 1;
else
max = max + Math.Pow(arrX[j], i) * arrY[j];
}
xyPow[i] = Math.Round(max, 4);
}
return xyPow;
}
#endregion
#region 获取高斯矩阵(增广矩阵)
/// <summary> 获取高斯矩阵(增广矩阵) </summary>     
/// <param name="arrX">X的幂次和</param> 
/// <param name="arrXY">XY的幂次和</param> 
/// <param name="n">拟合曲线次数</param> 
/// <returns></returns> 
protected double[,] GetGauss(double[] arrX, double[] arrXY, int n)
{
double[,] gauss = new double[n + 1, n + 2];
for (int i = 0; i < n + 1; i++)
{
int j;
int m = i;
for (j = 0; j < n + 1; j++)
{
gauss[i, j] = arrX[m];
m++;
}
gauss[i, j] = arrXY[i];
}
return gauss;
}
#endregion
#region 求解拟合曲线的系数
/// <summary> 求解拟合曲线的系数 </summary>           
/// <param name="gauss">线性方程的增广矩阵</param> 
/// <param name="n">方程次数</param> 
/// <returns></returns> 
protected double[] ComputeGauss(double[,] gauss, int n)
{
double[] a = new double[n + 1];
double s;
int matrixLine = n + 1;
for (int i = 0; i < n + 1; i++)
{
a[i] = 0;
}
//循环每列
for (int j = 0; j < matrixLine; j++)
{
//每列J行以后的绝对值最大值
double max = 0;
int k = j;
for (int i = j; i < matrixLine; i++)
{
if (Math.Abs(gauss[i, j]) > max)
{
max = gauss[i, j];
k = i;
}
}
//判断j行否为最大值行 若不是将j行调换为最大值行
if (k != j)
{
double temp;
for (int m = j; m < matrixLine + 1; m++)
{
temp = gauss[j, m];
gauss[j, m] = gauss[k, m];
gauss[k, m] = temp;
}
}
if (max == 0)
{
//奇异矩阵无解
return a;
}
//进行初等行变换得到上三角矩阵
for (int i = j + 1; i < matrixLine; i++)
{
s = gauss[i, j];
for (int m = j; m < matrixLine + 1; m++)
{
gauss[i, m] = Math.Round(gauss[i, m] - gauss[j, m] * s / gauss[j, j], 6);
}
}
}
//根据倒推方式一次计算现行方程的解
for (int i = matrixLine - 1; i >= 0; i--)
{
s = 0;
for (int j = i + 1; j < matrixLine; j++)
{
s += gauss[i, j] * a[j];
}
a[i] = Math.Round((gauss[i, matrixLine] - s) / gauss[i, i], 6);
}
//返回方程的解即拟合曲线的系数
return a;
}
#endregion
/// <summary>
/// 对一组点通过最小二乘法进行线性回归
/// </summary>
/// <param name="parray"></param>
public static void LinearRegression(Point[] parray)
{
//点数不能小于2
if (parray.Length < 2)
{
Console.WriteLine("点的数量小于2,无法进行线性回归");
return;
}
//求出横纵坐标的平均值
double averagex = 0, averagey = 0;
foreach (Point p in parray)
{
averagex += p.X;
averagey += p.Y;
}
averagex /= parray.Length;
averagey /= parray.Length;
//经验回归系数的分子与分母
double numerator = 0;
double denominator = 0;
foreach (Point p in parray)
{
numerator += (p.X - averagex) * (p.Y - averagey);
denominator += (p.X - averagex) * (p.X - averagex);
}
//回归系数b(Regression Coefficient)
double RCB = numerator / denominator;
//回归系数a
double RCA = averagey - RCB * averagex;
Console.WriteLine("回归系数A: " + RCA.ToString("0.0000"));
Console.WriteLine("回归系数B: " + RCB.ToString("0.0000"));
Console.WriteLine(string.Format("方程为: y = {0} + {1} * x",
RCA.ToString("0.0000"), RCB.ToString("0.0000")));
//剩余平方和与回归平方和
double residualSS = 0; //(Residual Sum of Squares)
double regressionSS = 0; //(Regression Sum of Squares)
foreach (Point p in parray)
{
residualSS += (p.Y - RCA - RCB * p.X) * (p.Y - RCA - RCB * p.X);
regressionSS += (RCA + RCB * p.X - averagey) * (RCA + RCB * p.X - averagey);
}
Console.WriteLine("剩余平方和: " + residualSS.ToString("0.0000"));
Console.WriteLine("回归平方和: " + regressionSS.ToString("0.0000"));
}
}
}