-1
$\begingroup$

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!

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Community Bot
    Jul 19, 2023 at 13:06
  • $\begingroup$ 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. $\endgroup$ Jul 20, 2023 at 0:17

3 Answers 3

2
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – JJacquelin
    Jul 21, 2023 at 7:57
  • $\begingroup$ 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. $\endgroup$
    – JJacquelin
    Jul 21, 2023 at 8:22
  • $\begingroup$ @JJacquelin. Thanks, Jean. $\endgroup$ Jul 21, 2023 at 8:25
1
$\begingroup$

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

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

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

$\endgroup$
1
  • $\begingroup$ As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. $\endgroup$
    – Community Bot
    Jul 19, 2023 at 13:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .