Trying To Fit A Trig Function To Data With Scipy
Solution 1:
If no values are provided for initial guess of the parameters p0
then a value of 1
is assumed for each of them. From the docs:
p0 : array_like, optional Initial guess for the parameters (length N). If None, then the initial values will all be 1 (if the number of parameters for the function can be determined using introspection, otherwise a ValueError is raised).
Since your data has very large x-values and very small y-values an initial guess of 1
is far from the actual solution and hence the optimizer does not converge. You can help the optimizer by providing suitable initial parameter values that can be guessed / approximated from the data:
- Amplitude:
A = (y.max() - y.min()) / 2
- Offset:
C = (y.max() + y.min()) / 2
- Frequency: Here we can estimate the number of zero crossing by multiplying consecutive y-values and check which products are smaller than zero. This number divided by the total x-range gives the frequency and in order to get it in units of
pi
we can multiply that number bypi
:y_shifted = y - offset; oemga = np.pi * np.sum(y_shifted[:-1] * y_shifted[1:] < 0) / (t.max() - t.min())
- Phase shift: can be set to zero,
dphi = 0
So in summary, the following initial parameter guess can be used:
offset= (y.max() + y.min()) /2
y_shifted = y -offset
p0 = (
(y.max() - y.min()) /2,
np.pi * np.sum(y_shifted[:-1] * y_shifted[1:] <0) / (t.max() - t.min()),
0,
offset
)
popt, pcov = curve_fit(func_cos, t, y, p0=p0)
Which gives me the following fit function:
Post a Comment for "Trying To Fit A Trig Function To Data With Scipy"