title: scipy curve_fit raises "OptimizeWarning: Covariance of the parameters could not be estimated"
body: <p>I am trying to fit this function to some data:</p>
  
  <p><a href="https://i.stack.imgur.com/22ZYx.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/22ZYx.gif" alt="enter image description here"></a></p>
  
  <p>But when I use my code </p>
  
  <pre><code>import numpy as np
  from scipy.optimize import curve_fit
  import matplotlib.pyplot as plt
  
  def f(x, start, end):
      res = np.empty_like(x)
      res[x &lt; start] =-1
      res[x &gt; end] = 1
      linear = np.all([[start &lt;= x], [x &lt;= end]], axis=0)[0]
      res[linear] = np.linspace(-1., 1., num=np.sum(linear))
      return res
  
  if __name__ == '__main__':
  
      xdata = np.linspace(0., 1000., 1000)
      ydata = -np.ones(1000)
      ydata[500:1000] = 1.
      ydata = ydata + np.random.normal(0., 0.25, len(ydata))
  
      popt, pcov = curve_fit(f, xdata, ydata, p0=[495., 505.])
      print(popt, pcov)
      plt.figure()
      plt.plot(xdata, f(xdata, *popt), 'r-', label='fit')
      plt.plot(xdata, ydata, 'b-', label='data')
      plt.show()
  </code></pre>
  
  <p>I get the error </p>
  
  <blockquote>
    <p>OptimizeWarning: Covariance of the parameters could not be estimated</p>
  </blockquote>
  
  <p>Output:</p>
  
  <p><a href="https://i.stack.imgur.com/uz1lg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/uz1lg.png" alt="enter image description here"></a></p>
  
  <p>In this example start and end should be closer to 500, but they dont change at all from my initial guess.</p>