Curve Fitting using Python
Objective:
- To write code to fit a linear and cubic polynomial for the Cp data.
- To plot the linear and cubic fit curves along with the raw data points.
- To explain the parameters used to measure the fitness characteristics for both the curves.
Introduction:
What is curve fitting?
Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to a series of the data point, 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.
Code:
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
# curve fit function
# for second order polynomial
def func1(t,a,b):
return a*t + b
def func2(t,a,b,c,d,e,f,g):
return a*pow(t,6) + b*pow(t,5) + c*pow(t,4) + d*pow(t,3) + e*pow(t,2) + f*t + g
def func3(t,a,b,c,d):
return a*pow(t,3) + b*pow(t,2) + c*t + d
# Reading thermodynamic data file
def read_file():
temperature = []
cp = []
for line in open(\'data (1)\',\'r\'):
values = line.split(\',\')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
# Main program
temperature, cp = read_file()
popt1, pcov1 = curve_fit(func1,temperature,cp)
fit_cp1 = func1(np.array(temperature), *popt1)
popt2, pcov2 = curve_fit(func2,temperature,cp)
fit_cp2 = func2(np.array(temperature), *popt2)
popt3, pcov3 = curve_fit(func3,temperature,cp)
fit_cp3 = func3(np.array(temperature), *popt3)
# plotting
plt.plot(temperature,cp,color=\'black\',linewidth=2)
plt.plot(temperature,fit_cp1,color=\'blue\',linewidth=2)
plt.plot(temperature,fit_cp3,color=\'red\',linewidth=2)
plt.legend([\'Actual data\',\'Linear polynomial\',\'Cubic polynomial\'])
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'Cp\')
plt.title(\'Curve Fitting\')
plt.show()
plt.plot(temperature,cp,color=\'black\',linewidth=2)
plt.plot(temperature,fit_cp2,color=\'blue\',linewidth=2)
plt.plot(temperature,fit_cp3,color=\'green\',linewidth=2)
plt.legend([\'Actual data\',\'Sixth order polynomial\',\'Cubic polynomial\'])
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'Cp\')
plt.title(\'Curve Fitting\')
plt.show()
Explanation:
1. \"popt\" represents the value of coefficients which when plotted gives the best curve .
2. \"pcov\" represents the covariance matrix, which indicates the uncertainties and correlations between parameters. This is mostly useful when the data has uncertainties.
3. \"np.array(temperature)\" stores the value of temperature in an array for it\'s easy execution.
4. \"*\" in *popt represents that all the values under variable \"popt\" of different dimensions should be executed one by one.
Results:
The above figure shows plotting of linear and cubic curve for the actual data. From the figure we can conclude that cubic fit curve approximately depicts the actual data more perfectly than linear curve.
Linear curve is linear in nature and does not depicts the actual data.
From the above figure we can conclude that the 6th order polynomial is the perfect fit for the actual data. Hence in order to increase the perfection of the curve we need to increase the degree of polynomial.