Trying to fit a quasi-periodic sequence

92 Views Asked by At

I am trying to fit dataset with a function, but with little success, as illustrated below. The blue line is the data I wish to fit, and the red line is my attempt.

image

Below is the function I used:

$$ g(x) = -A\sin\left(f \frac{\pi}{2}\cos(x)\right)^2 + D $$

where $A$, $f$, and $D$ are parameters. In Python,

-A*np.sin(f*(np.pi/2)*np.cos(x))**2+D

Any help would be greatly appreciated!

3

There are 3 best solutions below

1
On

Half the frequency of your function (replace x with x / 2), and then square the entire result. The resulting function should be proportional to a rough approximation of the function you are trying to approximate, so you can just scale it down until you are happy with the result. Eyeballing it from your image, I would guess that an added factor of $0.2$ would do the trick ($10^2\div20=5$, $1\div5=0.2$). (Bear in mind that you would be multiplying your entire function by $0.5$, not dividing by $0.5$.)

3
On

You have $n$ data points $(x_i,y_i)$ and you want to fit based on the model $$y= -A\sin\left(f \,\frac{\pi}{2}\,\cos(x)\right)^2 + D$$ or (notations are not clear) $$y= -A\sin^2\left(f \,\frac{\pi}{2}\,\cos(x)\right)+ D$$ but this does not matter.

The model is nonlinear because of $f$. So, give it a value and define $$t_i=\sin^2\left(f \,\frac{\pi}{2}\,\cos(x_i)\right)$$ Now, the model is linear and for any $f$ you have $A(f)$ and $D(f)$ (even explicitly).

For this value of $f$ compute the sum of squares of residuals $SSQ(f)$ and change the value of $f$ until you see a minimum.

At this point, you then have good estimates of the three parameters and you can start a nonlinear regression.

2
On

Of course the shape of the experimental curve seems sinusoidal in the range of the higher values of y. But in the range of the smaller values of y the shape looks more as arcs of exponential than sinusoidal. That is why I tried an exponential function of a sinusoidal function (Instead of a sinusoidal function of a sinusoidal function).

  • The blue line is a copy of the blue line from the figure joint to the question.
  • The red line is drawn with the proposed function.

enter image description here

A simplified function (slightly less accurate) is :

enter image description here