lx
2025-08-15 c97331d8eca8f9ef6a0c18ffce3518578b5638ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ErrorAnalysis.Service
{
    public class Utility
    {
        /// <summary> 
        /// 单线性插值法
        /// </summary>
        /// <param name="value">插值</param>
        /// <param name="x0">起始值</param>
        /// <param name="y0">起始值对应值</param>
        /// <param name="x1">结束值</param>
        /// <param name="y1">结束值对应值</param>
        /// <returns>插值对应结果</returns>
        public static double Interpolate(double value, double x0, double y0, double x1, double y1)
        {
            // Calculate the interpolated value using linear interpolation formula
            double y = y0 + (value - x0) * (y1 - y0) / (x1 - x0);
            return y;
        }
 
        public static (double slope, double intercept) FitLine(PointF[] points)
        {
            // 计算各求和项
            int n = points.Length;
            double sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;
 
            foreach (var p in points)
            {
                sumX += p.X;
                sumY += p.Y;
                sumXY += p.X * p.Y;
                sumX2 += p.X * p.X;
            }
 
            // 计算斜率和截距
            double denominator = n * sumX2 - sumX * sumX;
            if (Math.Abs(denominator) < 1e-10) // 避免除以0
                throw new InvalidOperationException("点集过于集中,无法拟合直线");
 
            double slope = (n * sumXY - sumX * sumY) / denominator;
            double intercept = (sumY - slope * sumX) / n;
 
            return (slope, intercept);
        }
 
        public static double GetLineValue(double x, double slope, double intercept) => slope * x + intercept;
 
        public static (double a, double b, double c) FitParabola(PointF[] points)
        {
            if (points.Length < 3)
                throw new ArgumentException("至少需要3个点进行抛物线拟合");
 
            // 1. 计算各项求和值[1,6](@ref)
            int n = points.Length;
            double sx = 0, sy = 0, sx2 = 0, sx3 = 0, sx4 = 0, sxy = 0, sx2y = 0;
 
            foreach (var p in points)
            {
                double x = p.X;
                double y = p.Y;
                double x2 = x * x;
                double x3 = x2 * x;
                double x4 = x2 * x2;
 
                sx += x;
                sy += y;
                sx2 += x2;
                sx3 += x3;
                sx4 += x4;
                sxy += x * y;
                sx2y += x2 * y;
            }
 
            // 2. 构建正规方程矩阵[1,6](@ref)
            double[,] matrix = {
            { sx4, sx3, sx2, sx2y },
            { sx3, sx2, sx,  sxy  },
            { sx2, sx,  n,   sy   }
        };
 
            // 3. 使用高斯消元法求解
            double[] coefficients = GaussElimination(matrix);
 
            return (coefficients[0], coefficients[1], coefficients[2]);
        }
 
        // 高斯消元法实现(带部分主元选择)
        private static double[] GaussElimination(double[,] matrix)
        {
            int n = matrix.GetLength(0);
            double[] result = new double[n];
 
            // 前向消元
            for (int i = 0; i < n - 1; i++)
            {
                // 部分主元选择
                int maxRow = i;
                for (int k = i + 1; k < n; k++)
                {
                    if (Math.Abs(matrix[k, i]) > Math.Abs(matrix[maxRow, i]))
                        maxRow = k;
                }
 
                // 行交换
                if (maxRow != i)
                {
                    for (int j = 0; j <= n; j++)
                    {
                        double temp = matrix[i, j];
                        matrix[i, j] = matrix[maxRow, j];
                        matrix[maxRow, j] = temp;
                    }
                }
 
                // 消元过程
                for (int k = i + 1; k < n; k++)
                {
                    double factor = matrix[k, i] / matrix[i, i];
                    for (int j = i; j <= n; j++)
                    {
                        matrix[k, j] -= factor * matrix[i, j];
                    }
                }
            }
 
            // 回代求解
            for (int i = n - 1; i >= 0; i--)
            {
                result[i] = matrix[i, n];
                for (int j = i + 1; j < n; j++)
                {
                    result[i] -= matrix[i, j] * result[j];
                }
                result[i] /= matrix[i, i];
            }
 
            return result;
        }
 
        public static double GetParabolaValue(double x, double a, double b, double c)
        {
            return a * x * x + b * x + c;
        }
    }
}