hydroponic-controller/messung/approximate.py

108 lines
2.0 KiB
Python

#Use venv
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
# importing module
from pandas import *
# reading CSV file
data = read_csv("20240423_EC_Calibration.csv")
# converting column data to list
solutionAdded = data['solutionAdded'].tolist() #in ml
tempReservoir = data['tempReservoir'].tolist() #in C
adc = data['ECadcAdjusted_B'].tolist() #adc reading
#solutionConcentration=5924.8 #mg/L NaCl
solutionConcentration=5690 #mg/L NaCl
startWaterAmount=300 #mL (same unit as solutionAded)
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(concentration)
print("")
print("ECcalculated")
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')
for model_order in [3,4,5,6]:
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()