CURVE FITTING - Python
CURVE FITTING - Python
Aim :
To create a program for performing Curve fitting in python using the data of temperature and specific heat of oxygen from a file.
Description :
Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a smooth function is constructed that approximately fits the data.
In this project, linear and cubic curve fittings are done and analysation is done with the curves obtained with respect to the actual data curve.
Tasks & Solutions :
1. What does popt and pcov mean?
(Here, popt1,popt2 and pcov1,pcov2 are used just to differentiate between the linear and cubic curve fits)
popt is an array that stores the values of the coefficients that are being passed for a particular function.
pcov is an two dimensional array that stores the values of the estimated covarience of popt i.e the coefficients.
2. What does np.array(temperature) do?
The np.array(temperature) is used for passing the values of temperature as a numpy array to the function.
3. What does the * in *popt mean?
' * ' in *popt refers to the coefficients in the given equation by expanding the values stored in the array to the arguments of the function
4. Write code to fit a linear and cubic polynomial for the Cp data.
Program code :
"""
Program to perform Curve fitting in Python
with data from a file consisting of
temmperature and specific heat
"""
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
#function definition for linear curve fit
def linear(t,a,b):
return (a*t + b)
#function definition for cubic curve fit
def cubic(t,a,b,c,d):
return (a*pow(t,3) + b*pow(t,2) + c*t + d)
#reading data from 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]
#For linear curve fit
temp1,cp1 = read_file()
popt1,pcov1 = curve_fit(linear,temp1,cp1)
fit_cp1 = linear(np.array(temp1),*popt1)
#For cubic curve fit
temp2,cp2 = read_file()
popt2,pcov2 = curve_fit(cubic,temp2,cp2)
fit_cp2 = cubic(np.array(temp2),*popt2)
"""
As the actual data is same for all situations,
both linear and cubic curve fits are plotted
in a single figure with various colors and thickness
"""
#plotting
plt.plot(temp1,cp1,color="red",Linewidth=4)
plt.plot(temp1,fit_cp1,color="blue",linewidth=3)
plt.plot(temp2,fit_cp2,color="black",linewidth=3)
plt.legend(['Actual Data plot','Linear curve fit','Cubic curve fit'],loc='best')
plt.xlabel('Temperature[K]')
plt.ylabel('Specific heat - Cp')
plt.savefig('curvefit.png')
plt.show()
Output :
From the above plot, it can be clearly analysed that higher the order of the polynomial results in a better curve fit, therefore a cubic curve fit is much better and close to the actual data plot when compared to linear curve fit.
5. What needs to be done in order to make the curve fit perfect?
The curve fit can be made better by breaking the temperature into two different ranges or by using a higher order polynomial but at the same time increasing the degree of the polynomial has to be taken care as higher the order then more time is required and complexity of the equation also increases, so both the cases has to be compromised together to obtain a better curve fit.