%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy.optimize
def ntc(R, A, B, C):
return 1/(A + B * np.log(R) + C * np.log(R)**3) - 273.15
meas = np.array([1613, 1783, 2304, 2873]) / 4095
temp = np.array([19.25, 24.12, 38.7, 58.94])
Rntc = (1-meas) * 10e3 / meas
Rntc
array([15387.47675139, 12966.90970275, 7773.4375 , 4253.39366516])
fit = scipy.optimize.curve_fit(ntc, Rntc, temp, p0=[1e-03, 2e-04, 1e-07])[0]
fit
array([-9.64222259e-05, 3.94199924e-04, -3.18715953e-07])
model = np.poly1d(np.polyfit(meas * 4095, temp, 2))
model
poly1d([ 5.66482342e-06, 5.86293885e-03, -4.70748428e+00])
res = np.linspace(2e3, 30e3, num=100)
R = 10e3
Uout = R / (R+res)
plt.figure()
plt.plot(ntc(res, *fit), Uout* 4095, label="Steinhart-Hart")
plt.plot(model(Uout* 4095), Uout* 4095, label="Polyfit")
plt.plot(temp, meas*4095, 'o', label="Messung")
plt.ylabel("ADC Counts")
plt.xlabel("Temperature in °C")
plt.grid()
plt.legend()
plt.show()
plt.figure()
plt.plot(ntc(res, *fit), model(Uout* 4095) - ntc(res, *fit) )
plt.grid()
plt.ylabel("Temperature Error in °C")
plt.xlabel("Temperature in °C")
plt.show()