Python-Curve Fitting
Please Find The Code,Explanation,Results,Extra Questions and Errors Below.
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.
Results
Extra Questions
1.What does popt and pcov mean?
popt is an array with the optimal parameters obtained using a non-linear least squaresfit.
pcov is a 2d array with the estimated covariance of the parameters in popt.
2.What does np.array(temperature) do?
A numpy array is a grid of values, all of the same type.Here it is being used to pass the values of temperatue to the function.
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?
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.