Note that there are some explanatory texts on larger screens.

plurals
  1. POSciPy leastsq fit to a sine wave failing
    primarykey
    data
    text
    <p>I am trying to figure out what it is I don't understand here.</p> <p>I am following <a href="http://www.scipy.org/Cookbook/FittingData" rel="nofollow noreferrer">http://www.scipy.org/Cookbook/FittingData</a> and trying to fit a sine wave. The real problem is satellite magnetometer data which makes a nice sine wave on a spinning spacecraft. I created a dataset then am trying to fit it to recover the inputs.</p> <p>Here is my code:</p> <pre><code>import numpy as np from scipy import optimize from scipy.optimize import curve_fit, leastsq import matplotlib.pyplot as plt class Parameter: def __init__(self, value): self.value = value def set(self, value): self.value = value def __call__(self): return self.value def fit(function, parameters, y, x = None): def f(params): i = 0 for p in parameters: p.set(params[i]) i += 1 return y - function(x) if x is None: x = np.arange(y.shape[0]) p = [param() for param in parameters] return optimize.leastsq(f, p, full_output=True, ftol=1e-6, xtol=1e-6) # generate a perfect data set (my real data have tiny error) def mysine(x, a1, a2, a3): return a1 * np.sin(a2 * x + a3) xReal = np.arange(500)/10. a1 = 200. a2 = 2*np.pi/10.5 # omega, 10.5 is the period a3 = np.deg2rad(10.) # 10 degree phase offset yReal = mysine(xReal, a1, a2, a3) # plot the real data plt.figure(figsize=(15,5)) plt.plot(xReal, yReal, 'r', label='Real Values') # giving initial parameters amplitude = Parameter(175.) frequency = Parameter(2*np.pi/8.) phase = Parameter(0.0) # define your function: def f(x): return amplitude() * np.sin(frequency() * x + phase()) # fit! (given that data is an array with the data to fit) out = fit(f, [amplitude, frequency, phase], yReal, xReal) period = 2*np.pi/frequency() print amplitude(), period, np.rad2deg(phase()) xx = np.linspace(0, np.max(xReal), 50) plt.plot( xx, f(xx) , label='fit') plt.legend(shadow=True, fancybox=True) </code></pre> <p>Which makes this plot: <img src="https://i.stack.imgur.com/91cGN.png" alt="enter image description here"></p> <p>The recovered fit parameters of <code>[44.2434221897 8.094832581 -61.6204033699]</code> have no resemblance to what I started with.</p> <p>Any thoughts on what I am not understanding or doing wrong?</p> <pre><code>scipy.__version__ '0.10.1' </code></pre> <hr> <p>Edit: Fixing one parameter was suggested. In the example above fixing the amplitude to <code>np.histogram(yReal)[1][-1]</code> still produces unacceptable output. Fits: <code>[175.0 8.31681375217 6.0]</code> Should I try a different fitting method? Suggestions on which?</p> <p><img src="https://i.stack.imgur.com/KFEwf.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload