Week 5 - Curve fitting
AIM:
Program for linear, quadratic and cubic polynomial and checking results with the actual plot.
Description:
Curve fitting can be defined as the process of constructing a curve or mathematical function that has the best fit to a series of data points subjects to constraints.
Here, the curve for specific heat as a function of temperature and it will be plotted and the plot will be compared with the actual plot. The specific heats at a different temperature will be provided as inputs.
Initially, we will be introducing a program for linear, quadratic and cubic polynomial and check the results with the actual plot. Based on the results we are going to figure out the best possible ways to have a perfect fit.
We are using:
- popt: It is an array with the optimal values for the parameters so that the sum of squared residuals od data is minimized.
- pcov: It is a 2-D array with the estimated covariance of the parameters in popt. It can be used to calculate the standard deviation error of popt.
- np.array(temperature): This function stores the values of temperature in the form of an array and passes it to the function numpy.
- *in*popt : It stores the values of the constant parameters(a,b,c) in the array popt.
Program for Curve fitting:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Curve fir for linear equation
def func_linear(t,a,b):
return a*t + b
#Curve fit for quadraric equation
def func_quad(t,a,b,c):
return a*pow(t,2) + b*t + c
# Curve fit for Cubic equation
def func_cubic(t,a,b,c,d):
return a*pow(t,3) + b*pow(t,2) + c*t + d
# Reading the 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 - Linear
temperature, cp = read_file()
popt,pcov = curve_fit(func_linear, temperature, cp)
fit_cp = func_linear(np.array(temperature), *popt)
plt.plot(temperature,cp,, linewidth=3)
plt.plot(temperature,fit_cp,, linewidth=3)
plt.legend(['Actual Data', 'Curve Fit'])
plt.title('Specific Heat Vs. Temperature')
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
# Main program - Quadratic
temperature, cp = read_file()
popt,pcov = curve_fit(func_quad, temperature, cp)
fit_cp = func_quad(np.array(temperature), *popt)
plt.plot(temperature,cp,, linewidth=3)
plt.plot(temperature,fit_cp,, linewidth=3)
plt.legend(['Actual Data', 'Curve Fit'])
plt.title('Specific Heat Vs. Temperature')
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
# Main program - Cubic
temperature, cp = read_file()
popt,pcov = curve_fit(func_cubic, temperature, cp)
fit_cp = func_cubic(np.array(temperature), *popt)
plt.plot(temperature,cp,, linewidth=3)
plt.plot(temperature,fit_cp,, linewidth=3)
plt.legend(['Actual Data', 'Curve Fit'])
plt.title('Specific Heat Vs. Temperature')
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
Results:
Linear Curve fit:

Quadratic Curve fit:

Cubic Curve fit:

Conclusion:
It is clear from the above figures that the curve fits better when the degree of the polynomial increases. It can be observed that the deviation of the
curve fit with the actual data is more for linear equation and gradually the deviation decreases in the order of the polynomial. If we consider fourth,
fifth-order polynomial then the curve fir will overlap with the actual data.
Another method to get a better fit could be using the split-wise polynomial.