hydroponic-controller/messung/approximate.py

102 lines
1.8 KiB
Python
Raw Normal View History

2023-05-05 22:55:17 +00:00
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
# importing module
from pandas import *
# reading CSV file
2023-05-09 21:20:47 +00:00
data = read_csv("20230509_NaCl_manuell.csv")
2023-05-05 22:55:17 +00:00
# converting column data to list
#solutionAdded = data['solutionAdded'].tolist() #in ml
tempReservoir = data['tempReservoir'].tolist() #in C
adc = data['ECadcAdjusted'].tolist() #adc reading
solutionConcentration=5924.8 #mg/L NaCl
startWaterAmount=0.3 #L
ppmToECfactor=1/0.46
#concentration = [x*solutionConcentration/(startWaterAmount+x) for x in solutionAdded]
#ECcalculated = [x*ppmToECfactor for x in concentration] #uS/cm
ECmeasured = data['ecMeasured'].tolist() #in C
#print(concentration)
#print(ECcalculated)
x = adc
#y = ECcalculated
y = ECmeasured
'''
def model(x, a, b, c, d, e):
y = a * x**4 + b *x**3 + c*x**2 + d*x + e
return y
popt, pcov = curve_fit(model, x, y)
#y1 = a * numpy.exp(b * x)
#print("popt="+str(popt))
for p in popt:
print(p)
'''
plt.plot(x, y, 'ok')
xstart = np.min(adc)
xstop = np.max(adc)
increment = 10
xmodel = np.arange(xstart,xstop,increment)
#ymodel = model(xmodel, *popt)
#plt.plot(xmodel,ymodel, 'r')
2023-05-09 21:20:47 +00:00
for model_order in [3,4,5,6]:
2023-05-05 22:55:17 +00:00
print("model order="+str(model_order))
# Finding the Model
p = np.polyfit(x, y, model_order)
#print(p)
polystring=""
for i in np.arange(model_order+1):
_c=str(p[i])
if p[i]>=0:
_c="+"+_c #add leading +
polystring+=_c+"*x^"+str(model_order-i)+" "
print(polystring)
print("Excel:")
for i in np.arange(model_order+1):
print(str(p[i]))
print("Array:")
arraystring="{"
for i in np.arange(model_order+1):
arraystring+=str(p[model_order-i])
arraystring+=","
arraystring=arraystring[:-1]+"}"
print(arraystring)
# Plot the Model
ymodel = np.polyval(p, xmodel)
plt.plot(xmodel,ymodel,label="order="+str(model_order))
print()
plt.legend()
plt.grid()
plt.show()