Search code examples
pythondata-fittingfunction-fitting

Resistivity non linear fit in Python


I'm trying to fit Einstein approximation of resistivity in a solid in a set of experimental data. I have resistivity vs temperature (from 200 to 4 K)

import xlrd as xd
import matplotlib.pyplot as plt
import numpy as np
import pylab as pl
import scipy as sp
from scipy.optimize import curve_fit

#retrieve data from file
data = pl.loadtxt('salita.txt')
Temp = data[:, 1]
Res = data[:, 2]

#define fitting function
def einstein_func( T, ro0, AE, TE):
    nl = np.sinh(TE/(2*T))
    return ro0 + AE*nl*T

p0 = sp.array([1 , 1, 1])

coeffs, cov = curve_fit(einstein_func, Temp, Res, p0)

But I get these warnings

crio.py:14: RuntimeWarning: divide by zero encountered in divide
  nl = np.sinh(TE/(2*T))
crio.py:14: RuntimeWarning: overflow encountered in sinh
  nl = np.sinh(TE/(2*T))
crio.py:15: RuntimeWarning: divide by zero encountered in divide
  return ro0 + AE*np.sinh(TE/(2*T))*T
crio.py:15: RuntimeWarning: overflow encountered in sinh
  return ro0 + AE*np.sinh(TE/(2*T))*T
crio.py:15: RuntimeWarning: invalid value encountered in multiply
  return ro0 + AE*np.sinh(TE/(2*T))*T
Traceback (most recent call last):
  File "crio.py", line 19, in <module>
    coeffs, cov = curve_fit(einstein_func, Temp, Res, p0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/minpack.py", line 511, in curve_fit
    raise RuntimeError(msg)
RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 800.

I don't understand why it keeps saying that there is a divide by zero in sinh, since I have strictly positive values. Varying my starting guess has no effect on it.

EDIT: My dataset is organized like this:

4.39531E+0  1.16083E-7
4.39555E+0  -5.92258E-8
4.39554E+0  -3.79045E-8
4.39525E+0  -2.13213E-8
4.39619E+0  -4.02736E-8
4.43130E+0  -1.42142E-8
4.45900E+0  -2.60594E-8
4.46129E+0  -9.00232E-8
4.46181E+0  1.42142E-7
4.46195E+0  -2.13213E-8
4.46225E+0  4.26426E-8
4.46864E+0  -2.60594E-8
4.47628E+0  1.37404E-7
4.47747E+0  9.47612E-9
4.48008E+0  2.84284E-8
4.48795E+0  1.35035E-7
4.49804E+0  1.39773E-7
4.51151E+0  -1.75308E-7
4.54916E+0  -1.63463E-7
4.59176E+0  -2.36902E-9

where the first column is temperature and the second one is resistivity (negative values are due to noise in trial current since the sample is a PbIn alloy which becomes superconductive at temperature lower than 6.7-6.9K, here we are at 4.5K).

Argument I'm providing to sinh are Numpy arrays, with a linear function ro0 + AE*T my code works. I've tried with scipy.optimize.minimize but the result is the same. Now I see that I have almost nine hundred values in my file, may that be the problem?

I have edited my dataset removing some lines and now the only warning showing is

RuntimeWarning: overflow encountered in sinh

How can I workaround it?


Solution

  • Here are a couple of observations that could help:

    • You could try the least-squares fit directly with leastsq, providing the Jacobian, which might help tame it.

    • I'm guessing you don't want the superconducting temperatures in your data set at all if you're fitting to an Einstein model (do you have a source for this eqn, btw?)

    • Do make sure your initial guesses are as good as they could possibly be (ro0=AE=TE=1 probably won't cut it).

    • Plot your data and make sure there aren't any weird artefacts

    • You seem to be indexing your data array in the wrong way in your code example: if the data is structured as you say, you want:

      Temp = data[:, 0] Res = data[:, 1]

    (Python indexes start at 0).