Week 5 - Curve fitting
1). What does popt and pcov mean?
The curve_fit function returns two items, which we can popt and pcov.
The popt argument are the best-fit paramters for co.efficient a,b,c,d.(cubic) or a,b (linear).
The pcov variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters. This is mostly useful when the data has uncertainties.
2).What does np.array(temperature) do?
Converts the python list of temperature to a numpy array. Which can be use to visualise the curve-fit with our tempreture and cp.
3). What does the * in *popt mean?
As we know, The popt argument are the best-fit paramters for co.efficient a,b,c,d.(cubic) or a,b (linear).
So, the ‘*’ refers the value which is stored in popt .
4). Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
code for linear polynomial.
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit for linear function
def func(t,a,b):
return a*t+b
#reading data file
def read_file():
temperature=[]
cp=[]
for line in open ('data','r'): #opening the file in read mode and reading data line by line
values =line.split(',') # spliting the (left)temp and (right)cp by ','
temperature.append(float(values[0])) #converting strings into float values and append to the empty array
cp.append(float(values[1]))
return [temperature,cp]
# main program
temperature, cp=read_file() #calling the function
popt,pcov=curve_fit(func,temperature,cp) #operating curve_fit
fit_cp=func(np.array(temperature),*popt) #for visualize our curve_fit
#plot
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp,color="red",linewidth=3)
plt.legend(['actual data','curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('Cp')
plt.show()
plot for linear polynomial:
code for cubic polynomial:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit cubic function
def func(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
#reading data file
def read_file():
temperature=[]
cp=[]
for line in open ('data','r'): #opening the file in read mode and reading data line by line
values =line.split(',') # spliting the (left)temp and (right)cp by ','
temperature.append(float(values[0])) #converting strings into float values and append to the empty array
cp.append(float(values[1]))
return [temperature,cp]
# main program
temperature, cp=read_file() #calling the function
popt,pcov=curve_fit(func,temperature,cp) #operating curve_fit
fit_cp=func(np.array(temperature),*popt) #for visualize our curve_fit
#plot
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp,color="red",linewidth=3)
plt.legend(['actual data','curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('Cp')
plt.show()
plot for cubic polynomial:
Results : As shown in plots the cubic function has better curve fit than linear function.
5). What needs to be done in order to make the curve fit perfect?
As explain in results, we can conclude that as we increase the degree of function the curve fit is keep going better.