Curve_fitting data (I'm close to the params values, but curve_fit says optimal parameters not found)
<p>I'm trying to fit the data in <a href="http://www.filedropper.com/aluminio33920umaire" rel="nofollow">this file</a> with <code>curve_fit</code> from scipy in Python. The file contains data points of temperature vs time in celsius and milliseconds. I convert them to kelvin and seconds:</p>

<pre><code>thefile = open("aluminio_33920um_aire.txt", "r")
data = np.loadtxt(thefile, delimiter='\t', skiprows=1)
Temp = data[:, 0] + 273.15  #kelvin
Time = data[:, 1]*1e-3  #secs
thefile.close()
</code></pre>

<p>I define a couple of functions to be fitted:</p>

<pre><code>def newton(t, a, b, tau):
    return a + b * np.exp(-t/tau)

def dulong(t, ta, dift, f, n):
    return ta + (dift + (n-1)*t/f)**(1/(1-n))
</code></pre>

<p>newton's fitting works perfectly. But dulong does not. I have plotted several values for the parameters of duolong to see which values draw a line that more or less fits the data, and I found the values given here:</p>

<pre><code>poptd, pcovd = curve_fit(dulong, Time, Temp, p0=[295, 0.155, 6000, 1.38], sigma=[1]*len(Temp), absolute_sigma=True)
</code></pre>

<p>However, passing these values <code>p0</code> to curve_fit does not help since I get the error</p>

<pre><code>RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 1000.
</code></pre>

<p>Don't know how to improve since the initial guess of my parameters is a really good guess. I appreciate your help.</p>

<p>When I call <code>curve_fit</code> using the <code>dulong</code> function I get the following warning:</p>

<blockquote>
  <p><code>RuntimeWarning: invalid value encountered in power</code></p>
</blockquote>

<p>This suggests that, as <code>curve_fit</code> tests various values of the parameters, evaluation of <code>dulong</code> requires computation of the form <code>(negative real)**(real)</code>, which results in a complex number. Hence, the optimization procedure fails. </p>

<p>One approach is to restrict the search space of your parameters so that this issue does not occur. Without knowing the physical meaning of the parameters, I can see that, as long as <code>n</code> is greater than <code>1</code> and all other parameters are non-negative, <code>dulong</code> will return only real numbers. </p>

<p>Calling <code>curve_fit</code> with a <code>bounds</code> option as described above has no problem finding the optimal parameters.</p>

<pre><code>poptd, pcovd = curve_fit(dulong, Time, Temp, p0=[295, 0.155, 6000, 1.38], sigma=[1]*len(Temp), absolute_sigma=True,
                        bounds = ([0,0,0,1],[1000,1,10000,10]))

print(poptd)
</code></pre>

<blockquote>
  <p><code>[  304.5965     0.0857  9999.1743     1.5099]</code></p>
</blockquote>

<p>Here's the plot of the fit</p>

<p><a href="http://i.stack.imgur.com/aLg7L.png" rel="nofollow"><img src="http://i.stack.imgur.com/aLg7L.png" alt="enter image description here"></a></p>

python scipy curve-fitting