Curve fitting using python code
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*t*t+c*t+d
def func1(t,a,b,c,d):
return a*t+b
#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()
#For Cubic Equation
popt,pcov=curve_fit(func,temperature,cp)
fit_cp=func(np.array(temperature), *popt)
#For Linear Equation
popt1,pcov1=curve_fit(func1,temperature,cp)
fit_cp1=func1(np.array(temperature), *popt1)
#Cubic Equation
plt.figure(1)
plt.plot(temperature,cp,color=\"blue\",Linewidth=3)
plt.plot(temperature,fit_cp,color=\"red\",Linewidth=3)
plt.xlabel(\'Temperature[k]\')
plt.ylabel(\'Cp\')
#Linear Equation
plt.figure(2)
plt.plot(temperature,cp,color=\"blue\",Linewidth=3)
plt.plot(temperature,fit_cp1,color=\"red\",Linewidth=3)
plt.xlabel(\'Temperature[k]\')
plt.ylabel(\'Cp\')
plt.xlabel(\'temperature[k]\')
plt.ylabel(\'Cp\')
plt.show()
Code Explanation
-Required Libraries including numpy,scipy.optimize and matplotlib.pyplot are imported
-Func and func1 are programmed to accept the values of t,a,b,c and d and to return the value after calculating by using them all in cubic and linear equations respectively
-The thermodynamic values file is read using read function,its value is seperated using split function and stored in two different matrices called temperature and cp.
-popt and pcov values are found out using curve_fit function. Further the calculated values are stored by calling func function for cubic equations and stored in fit_cp.
-popt1 and pcov1 values are found out using curve_fit function. Further the calculated values are stored by calling func1 function for Linear equations and stored in fit_cp1.
-The plot of fit_cp and actual values are plotted on same graph for cubic polynomial equation.
-The plot of fit_cp1 and actual values are plotted on same graph for linear equation.
1. What does popt and pcov mean?
popt- An array of optimal values for the parameters which minimizes the sum of squares of residuals.
pcov-2d array which contains the estimated covariance of popt. The diagonals provide the variance of the parameter estimate.
2. What does np.array(temperature) do?
np.array- converts the python list of temperature to a numpy array since operations like pow are allowed on numpy arrays and not python lists.
3.What does the * in *popt mean?
The star here means that it is being referenced. Any and all change which could have occure to popt while in a function would be reflected in the original popt.
5. What needs to be done in order to make the curve fit perfect?
- By increasing the degree of the curve we can make the best curve fit.
- If we further increase the degree of the curve to 4 then the curve will be more accurate than the cubic polynomial.
- Further increasing the degree of polynomial will increase the calculating complexity and time required, hence 4th order polynomial can be taken as a sufficient curve fit.
- A curve fit can almost never be perfect because experimental data will alwaysvary from a perfect equation.
- However the fit can be made as close as possible by trying different order equations,extrapolation,dividing the data and curve fitting them seperately or on very rare case finding an equation which fits the data perfectly.
Explaination on Result.
The results are good because the cubic fit is better curve fit than the linear one as we can see from the output plots.
Output: