Search code examples
curve-fitting

How do I fit a sine curve to my data – Part 2 (two sines)


I found useful the answers to the question with same title in this link.

After checking one of the answers by @JJaqueline I read the reference paper and tried to replicate some of the results.

If we focus on the fitting of the function: $asin(b+xc)$ The solution explained in the paper to solve for c is nice and simple.

I have tried the same process of integrating the following function twice: $a1sin(b1+xc1)+a2sin(b2+xc2)$ but in my attempts the expressions to solve for c1 and c2 get very complex, I may be overcomplicating things.

I was wondering if there is feasible way of solving the problem with the methodology in the paper or I have to go to for an iterative method in this case.

x = [0.        , 0.02564103, 0.05128205, 0.07692308, 0.1025641 ,
   0.12820513, 0.15384615, 0.17948718, 0.20512821, 0.23076923,
   0.25641026, 0.28205128, 0.30769231, 0.33333333, 0.35897436,
   0.38461538, 0.41025641, 0.43589744, 0.46153846, 0.48717949,
   0.51282051, 0.53846154, 0.56410256, 0.58974359, 0.61538462,
   0.64102564, 0.66666667, 0.69230769, 0.71794872, 0.74358974,
   0.76923077, 0.79487179, 0.82051282, 0.84615385, 0.87179487,
   0.8974359 , 0.92307692, 0.94871795, 0.97435897, 1.        ]
y = [2.98994949, 2.45766647, 1.51829117, 0.57449757, 0.15340313,
       0.64032065, 2.04580719, 3.93814129, 5.59607034, 6.3244815 ,
       5.7865173 , 4.18760477, 2.21232589, 0.73341424, 0.4246889 ,
       1.45943814, 3.43671601, 5.5689522 , 7.03928712, 7.35809663,
       6.55364103, 5.11721008, 3.74409743, 3.00704441, 3.12199756,
       3.90873324, 4.94401238, 5.80843722, 6.28943297, 6.43890658,
       6.47205068, 6.58358129, 6.80015951, 6.96078318, 6.83721446,
       6.31988894, 5.55089141, 4.91051686, 4.84543347, 5.62019942]

Solution

  • I suppose that you are interested in the method described in paper https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales .

    The case of fitting a function of the kind y(x)=a1 * sin(w1 * x+b1)+a2 * sin(w2 * x+b2) isn't explicitly treated in the paper. This will be done below.

    Nevertheless this case is difficult. The method is base on a linearisation thanks to a convenient integral equation. In this case the integral equation involves four successine numerical integrations. The deviations between the numerical integrations and the analytical integrations increase a lot the more successive integrations they are. So one cannot expect good result with so many integrations especially if the data is scattered. At least one can expect to obtain rough values of the parameters which could be used as initial values for non-linear regression.

    enter image description here

    Calculus with your example of data :

    enter image description here

    Considering the Mean Square Error the nonlinear regression doesn't impove a lot the fitting compare to the above result. This might mainly due to the data which seems not well correspond to a "two sine" model.

    A few tests carried out with not too much scattered data sets give rather good results better than expected even with the four successive numerical integrations.

    Example with the 30 first points only. The fitting is much better :

    enter image description here

    For information. The integral equation is :

    enter image description here