Week 5 - Curve fitting
AIM: To study curve fitting using python.
- What does popt and pcov mean?
ans= popt is an array of optimal values for the parameters which minimizes the sum of square of residuals . pcov is a 2D array which contanis the estimated covariance of popt.
- What does np.array(temperature) do?
ams= It converts the python list of temperature to a numpy array since operations like pow are allowed in numpy arrays and not python lists.
- What does the *in*popt mean?
ans= It gives the reference to co-efficient in a given section.
- Write a python codes to fit a linear and cubic polynomial for the Cp data.
PROGRAM FOR LINEAR POLYNOMIAL:
#Importing the library
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
#Defining the curve fit function
def func(t,a,b,c,d,e):
return a*t+b
#Reading the thermo 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]
#Writing the program
temperature, cp= read_file()
popt,pcov= curve_fit(func,temperature,cp)
fit_cp= func(np.array(temperature),*popt)
#Plotting the curves
plt.plot(temperature,cp,color="red",linewidth=3.5)
plt.plot(temperature,fit_cp,color="green",linewidth=3.5)
plt.legend(['original data','curve fit'])
plt.xlabel('Temperature')
plt.ylabel('Cp')
plt.show()
OUTPUT GENERATED:
PROGRAM FOR CUBIC POLYNOMIAL:
#Importing the library
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
#Defining the function
def func(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
#Reading thermo 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]
#Writing the program
temperature,cp= read_file()
popt ,pcov= curve_fit(func,temperature,cp)
fit_cp= func(np.array(temperature),*popt)
#Plotting the curve
plt.plot(temperature,cp,color="red",linewidth=3.5)
plt.plot(temperature,fit_cp,color="green",linewidth=3.5)
plt.legend(['original data','curve fit'])
plt.xlabel('Temperature')
plt.ylabel('cp')
plt.show()
OUTPUT GENERATED:
EXPLANATION OF CODES:
- Firstly,import the math library,matplotlip.pyplot, numpy module
- Define a function for calculating firstly for linear polynomial and then for cubic polynomial.
- It is then followed by reading the thermo file.
- Create a for loop for calculation of temperature and cp values.
- Use the popt,pcov commands to write the file.
- Finally, we can plot the obtained results using the plot command and label the x and y axes.
ERROR FACED: There was a missing parenthesis in the command which lead to this error. It was easily resolved when the codes were checked again.
RESULT:
We could easily see that the cubic feet is more smooth and accurate then the linear fit.Thus, it is proved that curvefit can be made better by increasing the order of polynomial.
**Another method of improving the curve fit is to split the temperature ranges in two or three more parts.