1

I'm trying to fit f(x) = sin(x)*sin(x) function to my data, but I can not accurately do that: fitting result

My data could have random phase shift and that is the main problem with this fitting.

I use MathNet.Numerics library. My code for fitting:

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

2 Answers2

0

I extracted the (red) data for analysis. Here are my results, using the equation "y = amplitude * sin(pi * (x - center) / width) * sin(pi * (x - center) / width" with fitted C# code. I suggest making a test using this equation with these parameter values as initial parameter estimates.

enter image description here

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;
    }
}
James Phillips
  • 4,526
  • 3
  • 13
  • 11
  • I am not sure how to do that with "Fit.LinearCombinationFunc()" and it would be not helpful because my data could have different phase and amplitude every single time I acquire it, so the coefficients will be different. Maybe you know different method to fit function to your data? – MaV Oct 10 '19 at 11:56
  • The equation I posted is not linear in the parameters, and so will require a non-linear fitter. I had expected that a new fit would be required for new data. – James Phillips Oct 10 '19 at 12:11
0

I found a solution for non linear fitting. You can use CenterSpace.NMath library and do the folowing(for i.e. 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);

And Function() looks like that:

 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

MaV
  • 11
  • 2