# Trying to fit a quasi-periodic sequence

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.

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!

• Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
– Community Bot
Jul 19, 2023 at 13:06
• Seems like you should be able to do this with standard machine learning optimization techniques. Pick a convex loss function, i.e. a convex function of the difference between your function and the target function, and then choose parameters to minimize that loss function using gradient descent or some other related optimization technique. Jul 20, 2023 at 0:17

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.

• Hi Claude ! I agree with you. This method is simple and good in theory. But only if the model equation is well chosen. If not the minimum of SSQ is never good enough and the "best" fit remains always very bad. This is what we observe with the OP's model equation. That is why I proposed a more convenient equation. Jul 21, 2023 at 7:57
• Bytheway I tried your method with the equation model $y=a+b\exp(f\,\sin(x))$. It works very well and leads to $f=-2.903$ , $a=10.003$ , $b=0.544$ which is good agreement with the "simplified" function in my answer. Jul 21, 2023 at 8:22
• @JJacquelin. Thanks, Jean. Jul 21, 2023 at 8:25

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.

A simplified function (slightly less accurate) is :

• Hi Jean ! I was waiting for you here. Are you OK ? Cheers Jul 21, 2023 at 6:19
• @Claude Leibovici. Hi Claude! I am fine. I wish the same for you. Cheers. Jul 21, 2023 at 8:00

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$$.)