首页 > 解决方案 > 曲线拟合到具有可变幂的 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:函数在任何轴上始终高于零

标签: c#curve-fittingmath.net

解决方案


我想我得到了它的工作。

这是代码:

    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);

推荐阅读