首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Math.NET C#计算导数

使用Math.NET C#计算导数
EN

Stack Overflow用户
提问于 2015-12-11 00:11:49
回答 4查看 16.5K关注 0票数 6

我正在寻找一个简单的函数,它将接受一个双精度值数组并返回数学导数。

Math.NET似乎有这样一个函数,但它要求使用以下语法:

代码语言:javascript
复制
double FirstDerivative(Func<double, double> f, double x)

我不确定为什么我需要指定一个函数。我只想要一个可以传递数据的预先存在的函数。

EN

回答 4

Stack Overflow用户

发布于 2015-12-11 04:33:25

获取数据点并创建一个Math.NET Numerics Cubic Spline对象。然后使用.Differentiate()方法获取每个所需点处的坡度。

示例

尝试下面的代码:

代码语言:javascript
复制
static class Program
{
    const int column_width = 12;
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        var xvec = new DenseVector(new double[] { 0.0, 1.0, 2.0, 3.0, 4.0 });
        var yvec = new DenseVector(new double[] { 3.0, 2.7, 2.3, 1.6, 0.2 });
        Debug.WriteLine("Input Data Table");
        Debug.WriteLine($"{"x",column_width} {"y",column_width}");
        for(int i = 0; i < xvec.Count; i++)
        {
            Debug.WriteLine($"{xvec[i],column_width:G5} {yvec[i],column_width:G5}");
        }
        Debug.WriteLine(" ");
        var cs = CubicSpline.InterpolateNatural(xvec, yvec);

        var x = new DenseVector(15);
        var y = new DenseVector(x.Count);
        var dydx = new DenseVector(x.Count);
        Debug.WriteLine("Interpoaltion Results Table");
        Debug.WriteLine($"{"x",column_width} {"y",column_width} {"dy/dx",column_width}");
        for(int i = 0; i < x.Count; i++)
        {
            x[i] = (4.0*i)/(x.Count-1);
            y[i] = cs.Interpolate(x[i]);
            dydx[i] = cs.Differentiate(x[i]);
            Debug.WriteLine($"{x[i],column_width:G5} {y[i],column_width:G5} {dydx[i],column_width:G5}");
        }


    }
}

并查看调试输出:

代码语言:javascript
复制
Input Data Table
           x            y
           0            3
           1          2.7
           2          2.3
           3          1.6
           4          0.2

Interpoaltion Results Table
           x            y        dy/dx
           0            3     -0.28214
     0.28571        2.919     -0.28652
     0.57143       2.8354     -0.29964
     0.85714       2.7469      -0.3215
      1.1429       2.6509     -0.35168
      1.4286       2.5454     -0.38754
      1.7143        2.429     -0.42864
           2          2.3       -0.475
      2.2857        2.154     -0.55809
      2.5714       1.9746      -0.7094
      2.8571       1.7422     -0.92894
      3.1429       1.4382      -1.1979
      3.4286       1.0646      -1.4034
      3.7143      0.64404      -1.5267
           4          0.2      -1.5679

票数 10
EN

Stack Overflow用户

发布于 2015-12-11 01:59:58

如果您不反对Math.Net以外的库,可以尝试AlgLib和它们的spline1ddiff函数

构建样条线很容易,Akima样条线通过点看起来很好很平滑。如果您想要一个接受一组数据并返回导数的方法,下面是一个使用AlgLib数学库的示例:

代码语言:javascript
复制
public static void CalculateDerivatives(this Dictionary<double, double> inputPoints, out Dictionary<double, double> firstDerivatives, out Dictionary<double, double> secondDerivatives)
{
        var inputPointsXArray = inputPoints.Keys.ToArray();
        var inputPointsYArray = inputPoints.Values.ToArray();

        spline1dinterpolant akimaSplineToDifferentiate;
        alglib.spline1dbuildakima(inputPointsXArray, inputPointsYArray, out akimaSplineToDifferentiate);

        firstDerivatives = new Dictionary<double, double>();
        secondDerivatives = new Dictionary<double, double>();
        foreach (var pair in inputPoints)
        {
            var xPoint = pair.Key;
            double functionVal, firstDeriv, secondDeriv;
            alglib.spline1ddiff(akimaSplineToDifferentiate, xPoint, out functionVal, out firstDeriv, out secondDeriv);

            firstDerivatives.Add(point, firstDeriv);
            secondDerivatives.Add(point, secondDeriv);
        }
}

警告: Akima Spline在您的数据集范围之外有不可预测的行为。

票数 1
EN

Stack Overflow用户

发布于 2015-12-11 22:34:52

感谢您的回复。

我相信我需要遍历数组,从Math.NET调用Differentiate函数,或者简单地编写我自己的(rise over run)计算。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34206611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档