Curve fitting
1. popt is a library of standard numeric values which can be use for generic calculations of python model. And pcov use for compute popt variance of deviation parameters.
2. np.array use for to convert temperature of degree centigrate into degree fahrenheit.
Code for linear curve fit:-
#[a(t)^2+bt+c] with this equation use for plot a curve.
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Curve fit function
def func(t,a,b,c):
return (a*pow(t,2)+b*t+c)
# Reading thermodynamic data file
def read_file():
temperature =[]
cp = []
for line in open(\'data\',\'r\'):
values = line.split(\',\')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature, cp]
# Main program
temperature, cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np. array(temperature),* popt)
plt.plot(temperature, cp, color=\"blue\",linewidth=3)
plt.plot(temperature, fit_cp, color=\"red\",linewidth=3)
plt.legend([\'Actual date\',\'curve fit\'])
plt.xlabel(\'temperature [k]\')
plt.ylabel(\'cp\')
plt.show()
Case-2:- Cubical polynomial equation of curve fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Curve fit function
def func(t,a,b,c,d):
return (a*pow(t,3)+b*pow(t,2)+c*t+d)
# Reading thermodynamic data file
def read_file():
temperature =[]
cp = []
for line in open(\'data\',\'r\'):
values = line.split(\',\')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature, cp]
# Main program
temperature, cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np. array(temperature),* popt)
plt.plot(temperature, cp, color=\"blue\",linewidth=3)
plt.plot(temperature, fit_cp, color=\"red\",linewidth=3)
plt.legend([\'Actual date\',\'curve fit\'])
plt.xlabel(\'temperature [k]\')
plt.ylabel(\'cp\')
plt.show()