c# - 曲线拟合到具有可变幂的 3D 多项式
问题描述
我有 3 个数据集。2 表示多项式本身(我们称它们为 x 和 y),1 表示函数值(它将是 z)。
多项式看起来像这样(假设两个维度的幂都是 3):
z = a00 + a01*x + a02*x^2 + a03*x^3 + a10*y + a11*y*x + a12*y*x^2 ... etc
在准备“a”值的近似值时,我需要能够设置每个维度的幂。
我并不真正了解 CurveFitting 函数如何与 Math.NET Numerics 一起使用,但我尝试过 Fit.LinearMultiDim 和 MultipleRegression.QR。我在初始化 Func 委托时遇到问题
var zs = new double[]
{
//values here
};
var x = new double[]
{
//values here
};
var y = new double[]
{
//values here. But the amounts are equal
};
var design = Matrix<double>.Build.DenseOfRowArrays(Generate.Map2(x, y,(t, w) =>
{
var list = new List<double>(); //Can i get this working?
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
list.Add(Math.Pow(t, j)*Math.Pow(w, i));
}
}
return list.ToArray();
}));
double[] p = MultipleRegression.QR(design, Vector<double>.Build.Dense(zs)).ToArray();
所以理想情况下,我需要能够用某种循环来组合函数,该循环解释了两个变量的最大功率。
UPD:函数在任何轴上始终高于零
解决方案
我想我得到了它的工作。
这是代码:
List<Func<double[], double>> ps = new List<Func<double[],double>>();
for (int i = 0; i <= polynomialOrderFirstVal; i++)
{
for (int j = 0; j <= polynomialOrderSecVal; j++)
{
var orderFirst = j;
var orderSecond = i;
ps.Add(d => Math.Pow(d[0], orderFirst) * Math.Pow(d[1],orderSecond));
}
}
var psArr = ps.ToArray();
double[] coefs = Fit.LinearMultiDim(x, y, psArr);
推荐阅读
- c - 关于数组的困惑
- discord - 让 Discord Bot 检查它是否对自己的帖子做出反应
- r - 从 r 中的数据框(或矩阵)中查找前 n 个最大值
- loops - 带有用户输入的汇编程序 Intel x86 循环 n 次
- forms - 选择相同元素时Vue表单选择发出事件
- excel - 将值从 30 本书复制到一本书 - Excel VBA
- codenameone - Cloudinary 图像未显示在 IOS 调试版本上
- r - 为什么将.txt文件导入R时所有变量都压缩为一列
- c - readlines 函数将文本从文件逐行插入到指针传递的字符串数组中
- excel - 如何通过 WebBrowser 运行 Internet Explorer 应用程序?