hydroponic-controller/messung/approximate.py

118 lines
2.1 KiB
Python
Raw Permalink Normal View History

2024-04-23 21:39:18 +00:00
#Use venv
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
2024-04-23 21:39:18 +00:00
data = read_csv("20240423_EC_Calibration.csv")
2023-05-05 22:55:17 +00:00
# converting column data to list
2024-04-23 21:39:18 +00:00
solutionAdded = data['solutionAdded'].tolist() #in ml
2023-05-05 22:55:17 +00:00
tempReservoir = data['tempReservoir'].tolist() #in C
2024-06-25 19:10:12 +00:00
adc = data['ECadcAdjusted_A'].tolist() #adc reading
2023-05-05 22:55:17 +00:00
2024-06-25 19:10:12 +00:00
#solutionEC=5924.8 #mg/L NaCl
solutionEC=5690 #mg/L NaCl
2024-04-23 21:39:18 +00:00
startWaterAmount=300 #mL (same unit as solutionAded)
2023-05-05 22:55:17 +00:00
ppmToECfactor=1/0.46
2024-06-25 19:10:12 +00:00
## EC Calutation
'''
2024-04-23 21:39:18 +00:00
concentration = [x*solutionConcentration/(startWaterAmount+x) for x in solutionAdded]
ECcalculated = [x*ppmToECfactor for x in concentration] #uS/cm
2024-06-25 19:10:12 +00:00
'''
### OR
## EC of solution given
ECcalculated = [x*solutionEC/(startWaterAmount+x) for x in solutionAdded] #uS/cm
2023-05-05 22:55:17 +00:00
2024-04-23 21:39:18 +00:00
#ECmeasured = data['ecMeasured'].tolist() #in C
2023-05-05 22:55:17 +00:00
2024-06-25 19:10:12 +00:00
#print("Concentration")
#print(concentration)
2024-04-23 21:39:18 +00:00
print("")
print("ECcalculated")
print(ECcalculated)
2023-05-05 22:55:17 +00:00
x = adc
2024-04-23 21:39:18 +00:00
y = ECcalculated
#y = ECmeasured
2023-05-05 22:55:17 +00:00
'''
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()