Curve fitting for Linear and Cubic Polynomial Using Python
Objective:
The main objective is to create and perform a Curve Fitting of Linear and Cubic Polynomial using Python.
Theory:
Curve Fitting:
Curve fitting is the process of finding a curve from a set of curves that best matches a series of data points. The set of curves is defined in terms of curve parameters. In other words, curve fitting consists of finding the curve parameters that produce the best match.
Types of curve fitting include:
Interpolation: where you discover a function that is an exact fit to the data points. Since this assumes no measurement error, it has limited applicability to real-life scenarios.
Smoothing: when we find a function that is an approximate fit to the data points, but we give room for error and we allow our actual points to be near, but not necessarily on the line; given the error is minimized overall.
Linear curve fitting (linear regression): It is when the data is fit to a straight line. Although there might be some curve to your data, a straight line provides a reasonable enough fit to make predictions. Since the equation of a generic straight line is always given by `f(x)=ax+b`, the question becomes: what values of ‘a’ & ‘b’ will give us the best fit line for our data?
Considering the vertical distance from each point to a prospective line as an error, and summing them up over our range, gives us a concrete number that expresses how far from ‘best’ the prospective line is. A line that provides a minimum error can be considered the best straight line.
Cubic Polynomial:
A cubic polynomial is a polynomial of degree 3. A univariate cubic polynomial has the form `f(x)=ax^3+bx^2+cx+d` . An equation involving a cubic polynomial is called a cubic equation. A closed-form solution known as the cubic formula exists for the solutions of an arbitrary cubic equation.
Equations Used:
For Linear Curve Fit:
`f(t)=at+b`
For Cubic Polynomial Curve Fit:
`f(t)=at^3+bt^2+ct+d`
Where,
a, b, c, d are real values (For Both Equations)
‘t’ is the Temperature Variable (For Both Equations)
Procedure:
- Initially, the libraries are imported. The libraries used are NumPy, Scipy.optimize.curve_fit, and matplotlib.pyplot modules.
# Importing the Libraries
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
- The function is created to Take Thermodynamic Readings from the given ‘data’ file. Here, Empty Arrays of Temperature and Specific Heat are created and the ‘data’ file which is stored in the same folder as the program code is read by using the following lines given in the code.
#Reading thermodynamic data file
def readfile():
temperature= [] # An Empty Array created to store the Temperature
cp=[] # An Empty array created to store the Specific Heat denoted by Specific Heat
for line in open('data','r'): # For Every line in 'data'
values = line.split(',') # Splits the line w.r.t. ',''
temperature.append(float(values[0])) # stores Temperature Values in Empty Temperatature array
cp.append(float(values[1])) # stores Cp Values in Empty Cp array
return [temperature,cp]
- Next, The Linear Curve Fit function is defined.
# Defining the function for Linear Curve Fit
def Linear_func(t,a,b): #Where a,b are variables and t is the temperature
x= a*t + b
return x
- The main code is then written to call the function to get the final solution for the code to run.
#Main Code to Read the file and produce the Output
temperature,cp =readfile()
popt,pcov= curve_fit(Linear_func,temperature,cp)
fit_cp= Linear_func(np.array(temperature), *popt)
- The graph is then plotted using the following commands. And the code for Linear Curve Fit is completed.
#Plotting of Graph for Linear Curve Fit
plt.figure(1)
plt.plot(temperature,cp, color ='blue', linewidth =3)
plt.plot(temperature,fit_cp, color ='red', linewidth =3)
plt.title('Specific Heat Vs Temperature for Linear Polynomial')
plt.legend(['actual data','Curve fit'])
plt.xlabel('Temperature (K)')
plt.ylabel('Specific Heat (Cp)')
plt.savefig('Linear Curve Fit')
plt.show()
- After the Linear Curve Fit, the Cubic Polynomial Curve fit function is Defined.
#Defining the Function for Cubic Polynomial
def CubeFunc(t,a,b,c,d):
y= a*pow(t,3)+ b*pow(t,2)+c*t+d #Where a,b,c,d are variables and t is the temperature
return y
- Again the main code is written to call the function to get the final solution for the code to run. (Same as the Linear Curve fit but with minor changes in the code)
temperature,cp =readfile()
popt1,pcov1= curve_fit(CubeFunc,temperature,cp)
fit_cp= CubeFunc(np.array(temperature), *popt1)
- The graph is then plotted, saved, and showed.
#Plotting of graphs for Cubic Polynomial
plt.figure(2)
plt.plot(temperature,cp, color ='blue', linewidth =3)
plt.plot(temperature,fit_cp, color ='red', linewidth =3)
plt.title('Specific Heat Vs Temperature for Cubic Polynomial')
plt.legend(['actual data','Curve fit'])
plt.xlabel('Temperature(K)')
plt.ylabel('Specific Heat (Cp)')
plt.savefig('Cubic Polynomial Curve Fit')
plt.show()
The Complete Code can be referred to below.
#Creating an Python Code for Curve Fitting using Linear and Cubic Polynomial
# Importing the Libraries
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#Reading thermodynamic data file
def readfile():
temperature= [] # An Empty Array created to store the Temperature
cp=[] # An Empty array created to store the Specific Heat denoted by Specific Heat
for line in open('data','r'): # For Every line in 'data'
values = line.split(',') # Splits the line w.r.t. ',''
temperature.append(float(values[0])) # stores Temperature Values in Empty Temperatature array
cp.append(float(values[1])) # stores Cp Values in Empty Cp array
return [temperature,cp]
#Curve fit function for Linear Regression
# Defining the function for Linear Curve Fit
def Linear_func(t,a,b): #Where a,b are variables and t is the temperature
x= a*t + b
return x
#Main Code to Read the file and produce the Output
temperature,cp =readfile()
popt,pcov= curve_fit(Linear_func,temperature,cp)
fit_cp= Linear_func(np.array(temperature), *popt)
#Plotting of Graph for Linear Curve Fit
plt.figure(1)
plt.plot(temperature,cp, color ='blue', linewidth =3)
plt.plot(temperature,fit_cp, color ='red', linewidth =3)
plt.title('Specific Heat Vs Temperature for Linear Polynomial')
plt.legend(['actual data','Curve fit'])
plt.xlabel('Temperature (K)')
plt.ylabel('Specific Heat (Cp)')
plt.savefig('Linear Curve Fit')
plt.show()
#Curve fit function for Cubic Polynomial
#Defining the Function for Cubic Polynomial
def CubeFunc(t,a,b,c,d):
y= a*pow(t,3)+ b*pow(t,2)+c*t+d #Where a,b,c,d are variables and t is the temperature
return y
temperature,cp =readfile()
popt1,pcov1= curve_fit(CubeFunc,temperature,cp)
fit_cp= CubeFunc(np.array(temperature), *popt1)
#Plotting of graphs for Cubic Polynomial
plt.figure(2)
plt.plot(temperature,cp, color ='blue', linewidth =3)
plt.plot(temperature,fit_cp, color ='red', linewidth =3)
plt.title('Specific Heat Vs Temperature for Cubic Polynomial')
plt.legend(['actual data','Curve fit'])
plt.xlabel('Temperature(K)')
plt.ylabel('Specific Heat (Cp)')
plt.savefig('Cubic Polynomial Curve Fit')
plt.show()
Questions Asked:
- What does popt and pcov mean?
popt,pcov= curve_fit(Linear_func,temperature,cp)
popt and pcov are parameters of Scipy.optimize.curve_fit Module.
popt: It is an array that provides an optimal value so that the sum of the squared residuals of the function. In general the return value ‘popt’ contains the best fit values of the parameters.
pcov: It contains the covariance matrix for the fit parameters. In general, the ‘pcov’ variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters. This is most useful when the data has uncertainties.
- What does np.array(temperature) do?
NumPy is used to work with arrays. The array object in NumPy is called ndarray. We can create a NumPy ‘ndarray’ object by using the array() function.
- What does the * in *popt mean?
The star (*) is used as a reference or as an argument to a function. The star (*) notation will expand a list of values into the arguments of the function. This is useful if the function has more than one or two parameters. In general, the star (*) notation is used so that the variable which is placed after the star (*) is being referenced. The change occurred in the popt is reflected in the original popt value.
- Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
The Code is written and explained above. The results are explained in the Result tab below.
- What needs to be done to make the curve fit perfectly?
The curve fit can never be perfect. The main reason for that is the actual Experimental value and the ideal curve-fit value always vary. However, the value of actual data may come close to the ideal curve fit value. This can be seen by the code and graph shown. As the order of the equations increase, the value gets closer to the ideal value. There are other methods like dividing the data and curve fitting them separately or extrapolation.
Terms used:
In case of clarity of some of the above unknown terms, refer to the following terms.
Covariance:
It is a measure of the joint variability of two random variables. If the greater values of one variable mainly correspond with the greater values of the other variable, and the same holds for the lesser values, (i.e., the variables tend to show similar behavior), the covariance is positive. In the opposite case, when the greater values of one variable mainly correspond to the lesser values of the other, (i.e., the variables tend to show the opposite behavior), the covariance is negative. The sign of the covariance, therefore, shows the tendency in the linear relationship between the variables. The magnitude of the covariance is not easy to interpret because it is not normalized and hence depends on the magnitudes of the variables. The normalized version of the covariance, the correlation coefficient, however, shows by its magnitude the strength of the linear relation.
Arrays:
An array is a data structure that stores values of the same data type. This is generally different than list as lists consist of any or all data types in it but the array has the same data type.
Extrapolation:
In mathematics, extrapolation is a type of estimation, beyond the original observation range, the value of a variable based on its relationship with another variable. In General, extrapolation is the process of generating points outside a given set of known data points.
Result:
The code is executed without any errors. Since, all the arguments and information required are gathered and processed so that there won’t be any mistake. The information gathered is mostly from google and Engineering Mathematics textbook online.
The Output of the program is as below.
The graph shows the Temperature Vs Specific Heat for Linear Regression.
The graph shows the Temperature Vs Specific Heat for Cubic Polynomial.
By the above graphs, we can conclude that with the increase in the order of the polynomial, the value of the actual data comes closer to the Ideal Curve fit.