Week 5 - Curve fitting
1. What does popt and pcov mean?
popt is a part of the solution, as it is an array that stores the values of the coefficients of the type of equation that we have defined to fit over the original set of data.
pcov is the second part of the solution and it is a 2-D array that stores the values of the estimated covariance of the the coefficients of the equation.
2. What does np.array(temperature) do?
While storing the values of the temperature from the data file in the 'temperature' variable, it should be noted that they are saved as a list. And since we need to perform mathematical calculations like pow(t,3) with the values of the temperature, we have to convert the list into a numpy array and 'np.array(temperature)' converts the list into a numpy array as the calculations cant be performed on a list file.
3. What does the * in *popt mean?
It acts as a reference to the values stored inside the popt array.
4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# defining the equation for linear curve fit
def lin_func(t,a,b):
return a*t+b
# defining the function for cubic curve fit
def cubic_func(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
# opening and reading the thermodynamic data file
def read_file():
temperature=[]
cp=[]
for line in open ('data','r'):
# using the split command to differentiate the temperature and cp values
values = line.split(',')
# storing the values from the data file into variables
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
# creating values of the variables for the linear fit equation
temperature, cp=read_file()
popt,pcov=curve_fit(lin_func,temperature,cp)
fit_cp=lin_func(np.array(temperature),*popt)
plt.plot(temperature,cp,color="blue",linewidth=2)
plt.plot(temperature,fit_cp,color="red",linewidth=2)
plt.legend(['actual data','linear curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('Cp')
plt.title('original data and linear fit comparision')
plt.show()
# creating values of the variables for the linear fit equation
temperature, cp=read_file()
popt,pcov=curve_fit(cubic_func,temperature,cp)
fit_cp=cubic_func(np.array(temperature),*popt)
plt.plot(temperature,cp,color="blue",linewidth=2)
plt.plot(temperature,fit_cp,color="red",linewidth=2)
plt.legend(['actual data','cubic curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('Cp')
plt.title('original data and cubic fit comparision')
plt.show()
RESULTS AND PLOTS:
linear fit:
cubic fit:
From the obtained plots, we can observe that the fitted curve doesnt perfectly fit the given dataa points plotting. Even though the cubic plot is a better fit than the linear fit, it still fails to perfectly represent the given data points. Hence, its not a very good curve fit.
5. What needs to be done in order to make the curve fit perfect?
Perfect fit refers to the fit, when the fitted line and the original line fully coincide with each other. In other words, the distance between the corresponding points of the fitted curve and the original curve is zero. The perfect fit csn be obtained by, either increasing the order of the polynomial, or by dividing the given data points into certain fragments and performing curve fit on each of those fragments.