TO FIND THE BEST CURVE FIT THROUGH DIFFERENT POLYNOMIAL EQUATIONS USING PYTHON
OBJECTIVE-
TO FIND THE BEST CURVE FIT THROUGH DIFFERENT POLYNOMIAL EQUATIONS
CODE SAMPLE-
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Curve fit function
def linear_func(t,a,b):
return a*t + b
def cubic_func(t1,a1,b1,c1,d1):
return a1*pow(t1,3) + b1*pow(t1,2) + c1*t1 + d1
# 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]
# required program
temperature,cp = read_file()
popt, pcov = curve_fit(linear_func, temperature, cp)
fit_cp1 = linear_func(np.array(temperature),*popt)
popt, pcov = curve_fit(cubic_func, temperature, cp)
fit_cp2 = cubic_func(np.array(temperature),*popt)
plt.figure(1)
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp1,color="red",linewidth=3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('cp')
plt.show()
plt.figure(2)
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp2,color="red",linewidth=3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('cp')
plt.show()
EXPLANATION-
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
This are the modules we have imported for finding the curvefit.
# Defining the required function for the curve fitting.
Then we have defined the function that are required for the curve fitting. basically the function defined are two polynomial equation, first is the linear polynomial equation and the second is the cubic polynomial equation .
The linear polynomial equation taken is a*t + b.
def linear_func(t,a,b):
return a*t + b
The cubical polynomial equation taken is a1*t1^3 + b1*t1^2 + c1*t1 + d1.
def cubic_func(t1,a1,b1,c1,d1):
return a1*pow(t1,3) + b1*pow(t1,2) + c1*t1 + d1
# 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]
So, basically we have the data file from there we had taken temperature and mass flow rate. Here, for loop is used to call the data and then we split the line so that we can differeniate the values of temperature and mass flow rate.
# Program required to plot the curve of linear and cubic polynomial equation.
temperature,cp = read_file()
popt, pcov = curve_fit(linear_func, temperature, cp)
fit_cp1 = linear_func(np.array(temperature),*popt)
popt, pcov = curve_fit(cubic_func, temperature, cp)
fit_cp2 = cubic_func(np.array(temperature),*popt)
Here, we have two curve fit along with command popt, pcov and *popt .
# What are the popt , pcov and why are they used?
The popt and pcov are the curve fit function which returns the two arrays. The popt is one dimensional array which contains the coefficient of functions which are determined.
The pcov is a two dimensional array or a matrix that contains the estimated returned parameter covariance matrix.
#what is *popt?
The * is used for multipication purpose and if it is used before popt then it used to call the variable included.
# what does np.array do?
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have.
#Plot of curve between Actual data and Curve fit-
plt.figure(1)
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp1,color="red",linewidth=3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('cp')
plt.show()
Above is the plot code to plot the curve fit were we see the Actual data and curve fit variation. In above figure we see that curve fit is linear because of linear polynomial quation and other is the curve which is obtained from the data provided.
plt.figure(2)
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp2,color="red",linewidth=3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('cp')
plt.show()
Above is the plot code to plot the curve fit were we see the Actual data and curve fit variation. In above figure we see that curve fit is matching with actual data because of cubic polynomial quation and other is the curve which is obtained from the data provided.
#What needs to be done in order to make the curve fit perfect?
Inorder to obtain the perfect curve fit with can increase the degree of polynomial equation as we had seen in above cases. However, it is quite difficult to make a perfect curve but still we can match it to find the optimum results.