【问题标题】:How to fit f(x) = sin(x)*sin(x) function to your data?如何将 f(x) = sin(x)*sin(x) 函数拟合到您的数据中?
【发布时间】:2019-10-10 08:34:24
【问题描述】:

我正在尝试将 f(x) = sin(x)*sin(x) 函数拟合到我的数据中,但我无法准确地做到这一点: fitting result

我的数据可能有随机相移,这是此拟合的主要问题。

我使用 MathNet.Numerics 库。 我的拟合代码:

Func<double, double> f = Fit.LinearCombinationFunc(
xData,
yData,
x => 1.0,
x => Math.Pow(Math.Sin(x + 1.0), 2));

【问题讨论】:

    标签: c# trigonometry curve-fitting data-fitting mathnet-numerics


    【解决方案1】:

    我提取了(红色)数据进行分析。这是我的结果,使用方程式“y = 振幅 * sin(pi * (x - center) / width) * sin(pi * (x - center) / width”和合适的 C# 代码。我建议使用这个进行测试以这些参数值作为初始参数估计的方程。

    using System;
    
    class Trigonometric_SineSquared_Offset
    {
        double Trigonometric_SineSquared_Offset_model(double x_in)
        {
            double temp;
            temp = 0.0;
    
            // coefficients
            double amplitude = 2.4582405471785171E+02;
            double center = -7.3116553541287885E+02;
            double width = 3.1152304928336734E+00;
            double Offset = 1.3146489736138119E+02;
    
            temp = amplitude * Math.Sin(3.14159265358979323846 * (x_in - center) / width) * Math.Sin(3.14159265358979323846 * (x_in - center) / width);
            temp += Offset;
            return temp;
        }
    }
    

    【讨论】:

    • 我不确定如何使用“Fit.LinearCombinationFunc()”来做到这一点,因为我的数据每次获取时都可能具有不同的相位和幅度,所以系数会与众不同。也许您知道将函数拟合到数据的不同方法?
    • 我发布的方程在参数中不是线性的,因此需要非线性拟合器。我曾预计新数据需要新的拟合。
    【解决方案2】:

    我找到了非线性拟合的解决方案。 您可以使用 CenterSpace.NMath 库并执行以下操作(例如 f(x) = a + c*sin(x+b)*sin(x+b) ):

    DoubleParameterizedFunction func = new Function();
    
    var f = new DoubleParameterizedDelegate(
    func.Evaluate);
    
    var fitter = new OneVariableFunctionFitter<TrustRegionMinimizer>(f);
    DoubleVector x = new DoubleVector(xData);
    DoubleVector y = new DoubleVector(yData);
    DoubleVector init = new DoubleVector("100.0 1.0 100.0");
    DoubleVector solution = fitter.Fit(x, y, init);
    

    而 Function() 看起来像这样:

     public class Function : DoubleParameterizedFunction
        {
            public Function ()
            { }
    
            public override double Evaluate (DoubleVector p, double x)
            {
                double a = p[0];
                double b = p[1];
                double c = p[2];
                return a + c*Math.Sin(b + x) * Math.Sin(b + x);
            }
    
            public override void GradientWithRespectToParams (DoubleVector p,
              double x, ref DoubleVector grad)
            {
                double a = p[0];
                double b = p[1];
                double c = p[2];
                grad[0] = 1; //partial differential for a
                grad[1] = 2 * c * Math.Sin(x + b) * Math.Cos(x + b);  //partial differential for b
                grad[2] = Math.Sin(x + b) * Math.Sin(x + b);  //partial differential for c
            }
        }
    

    https://www.centerspace.net/doc/NMath/user/nonlinear-least-squares-86564.htm

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多