Advertisement
Guest User

Fit Steinhart-Hart Equation to Measurements

a guest
Dec 1st, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. #!/usr/bin/python
  2. import math
  3. import scipy.optimize
  4. import numpy
  5.  
  6. # Steinhart-Hart Equation
  7. # from https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
  8.  
  9. def steinhart_hart(r,a,b,c) :
  10.     ''' Steinhart-Hart Equation, as found on Wikipedia.
  11.    r : resistance of a thermistor, measured in Ohms
  12.    a,b,c : steinhart-hart coefficients [a] = 1/K
  13.  
  14.    returns : temperature in degrees celsius!
  15.    '''
  16.  
  17.     one_over_kelvins = a + b*numpy.log(r) + c*numpy.log(r)**3
  18.     return 1.0/one_over_kelvins - 273.15
  19.  
  20. def main():
  21.     exp_data = numpy.array([
  22.         [  1.9, 627.0e3 ], # Ice water
  23.         [ 19.8, 255.1e3 ], # Room Temperature water
  24.         [ 65.9,  37.0e3 ]  # Hot water
  25.     ])
  26.  
  27.     temps = exp_data[:,0]
  28.     resist = exp_data[:,1]
  29.  
  30.     # give a ballpark estimate to start from, minimization might not
  31.     # converge if we start off too far away...
  32.     p0 = [0.1e-3, 0.1e-4, 0.1e-7]
  33.  
  34.     print('Experimental Data:')
  35.     print('Temperatures [degC]:', temps)
  36.     print('Resistances  [Ohm] :', resist)
  37.  
  38.     par, cov = scipy.optimize.curve_fit(steinhart_hart, resist, temps, p0)
  39.  
  40.     print('A=',par[0])
  41.     print('B=',par[1])
  42.     print('C=',par[2])
  43.  
  44.     temps2 = steinhart_hart(resist, *par)
  45.  
  46.     print('Can we reproduce the measured temperatures?')
  47.     print('Temperatures [degC]:', temps2)
  48.  
  49. if __name__ == '__main__' :
  50.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement