Curve fitting using PYTHON
Aim:To write a PYTHON Program for curve fitting of linear and cubic polynomial for the temperature and Cp data.
Introduction:Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, 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. A related topic is regression analysis which focuses more on questions of statistical inference such as how much uncertainty is present in a curve that is fit to data observed with random errors. Fitted curves can be used as an aid for data visualization, to infer values of a function where no data are available, and to summarize the relationships among two or more variables. Extrapolation refers to the use of a fitted curve beyond the range of the observed data, and is subject to a degree of uncertainty, since it may reflect the method used to construct the curve as much as it reflects the observed data.
Problem Statement:
1. What does popt and pcov mean?
Ans 'popt' is an array which stores the best values of the coefficients of the polynomial.
'pcov' is a matrix of order depends on degree of polynomial (i.e. if polynomial is linear then the matrix is 2-D and if polynomial is quadratic then the matrix is 3-D) in which the diagonal contains the variance of 'popt' such that we can extract the one standard deviation error by taking the square root of these elements.
2. What does np.array(temperature) do?
Ans It stores the value of temperature in the form of array.As we know in PYTHON we have to use numpy module for assignment of an array.
3. What does the * in *popt mean?
Ans '*' reference to 'popt'/the values stored in the array 'popt'
3. What does the * in *popt mean?
Ans '*' reference to 'popt'/the values stored in the array '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 function
def func(t,a,b):
return a*pow(t,1) + b #Linear Polynomial
#Reading thermodynamic data file
def read_file():
temperature=[]
cp=[]
for line in open('data','r'):
values=line.split(',')
temperature.append(float(values[0])) #Here tempearture values are stored from data
cp.append(float(values[1])) #Here cp values are stored from data
return [temperature,cp]
#Main Program
temperature,cp=read_file()
popt,pcov=curve_fit(func,temperature,cp) #Best valueof coefficients are obtained
fit_cp=func(np.array(temperature),*popt) #Curve fitting
plt.plot(temperature,cp,color="blue",linewidth=3) #Plotting
plt.plot(temperature,fit_cp,color="red",linewidth=3)
plt.legend(['Actual Data','Curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('Cp')
plt.show()
Code for cubic polynomial:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#Curve fit function
def func(t,a,b,c,d):
return a*pow(t,3) + b*pow(t,2) + c*t +d #Cubic Polynomial
#Reading thermodynamic data file
def read_file():
temperature=[]
cp=[]
for line in open('data','r'):
values=line.split(',')
temperature.append(float(values[0])) #Here tempearture values are stored from data
cp.append(float(values[1])) #Here cp values are stored from data
return [temperature,cp]
#Main Program
temperature,cp=read_file()
popt,pcov=curve_fit(func,temperature,cp) #Best valueof coefficients are obtained
fit_cp=func(np.array(temperature),*popt) #Curve fitting
plt.plot(temperature,cp,color="blue",linewidth=3) #Plotting
plt.plot(temperature,fit_cp,color="red",linewidth=3)
plt.legend(['Actual Data','Curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('Cp')
plt.show()
Procedure:1.First the modules are assigned for the curve fitting.
2.Then we have defined our function i.e. whether it is linear or cubic.
3.Then the values of temperature and cp are obtained from data file for which the curve fitting should be performed.
4.AFter that the best values of the coefficients is obtained and then fitted in the function to obtain the fitted function .
5.The comparison is seen through graph between them i.e. how closely they are matching with themselves.
Result:
Plot for Linear Polynomial:
Plot for Linear Polynomial:
Conclusion: As can be seen in the plots that cubic is very good curve fitting because both the values of function are very closely matching with each other.But in case of linear polynomial the curve fitting is not very good as they do not match very closely with each other.