|
楼主 |
发表于 2023-5-25 15:46:14
|
显示全部楼层
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;
class Program
{
static void Main(string[] args)
{
// 定义数据
double[] x = { 1, 2, 3, 4, 5 };
double[] y = { 2.1, 3.9, 7.2, 12.5, 20.1 };
// 定义二次多项式的阶数
int order = 2;
// 进行拟合
Vector<double> coefficients = Fit.Polynomial(x, y, order);
// 定义任意一个x值
double xValue = 3.5;
// 计算拟合线对应的y值
double yValue = 0;
for (int i = 0; i < coefficients.Count; i++)
{
yValue += coefficients[i] * Math.Pow(xValue, i);
}
Console.WriteLine("拟合线在x={0}处的y值为:{1}", xValue, yValue);
}
}
|
|