Curve fitting using Python
- AIM: To determine the code to fit a linear and cubic polynomial for the Cp data.
The link for the Cp vs Temperature data file is given below.
Click here to get the Cp vs Temperature data file
- THEORY : Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically constructthe functional relationship between the observed dataset and parameter values, etc.
The accuracy of the curve fit can be improved by increasing the degree of the polynomial. So, a 'Best fit' can be obtained by increasing the degree of the polynomial.
PERFECT FIT : The perfect fit is achieved when the geometric distances from the given points to the fitting curve are zero . This means that the curve fits perfectly on the points . To get a perfect fit we can increase to a order of the polynomials , this method increases the accuracy and reduces errors .
BEST FIt :The best fit is achieved when the geometric distances from the given points to the fitting curve are minimized, in the least squares sense. Finding the best fit reduces to the minimization of the objective function where denotes the fitting curve.
This will exactly fit four points.
Polynomial curves fitting points generated with a sine function. Red line is a first degree polynomial, green line is second degree, orange line is third degree and blue is fourth degree.
Fitting functions to data points
Most commonly, one fits a function of the form y=f(x).
- Most commonly, one fits a function of the form y=f(x).
- The first degree polynomial equation
y=ax+by=ax+b">y=ax+by=ax+by=ax+by=ax+bis a line with slope a. A line will connect any two points, so a first degree polynomial equation is an exact fit through any two points with distinct x coordinates.
- If the order of the equation is increased to a second degree polynomial, the following results:
y=ax2+bx+cy=ax2+bx+c">y=ax2+bx+cy=ax2+bx+cy=ax2+bx+cy=ax2+bx+cThis will exactly fit a simple curve to three points.
- If the order of the equation is increased to a third degree polynomial, the following is obtained:
y=ax3+bx2+cx+dy=ax3+bx2+cx+d">y=ax3+bx2+cx+dy=ax3+bx2+cx+dy=ax3+bx2+cx+dy=ax3+bx2+cx+d
This will exactly fit four points.
Upon finding a curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equation is a representing the datapoints.
- The sum of squares due to error (SSE)
- R-square
- Adjusted R-square
- Root mean squared error (RMSE)
Sum of Squares Due to Error
This statistic measures the total deviation of the response values from the fit to the response values. It is also called the summed square of residuals and is usually labeled as SSE.
SSE=∑ni=1wi(yi−ŷi)2SSE=∑i=1nwi(yi-ŷi)2">SSE=∑ni=1wi(yi−ŷi)2SSE=∑i=1nwi(yi−ŷi)2SSE=∑ni=1wi(yi−ŷi)2SSE=∑i=1nwi(yi-ŷi)2
A value closer to 0 indicates that the model has a smaller random error component, and that the fit will be more useful for prediction.
R- Square
This statistic measures how successful the fit is in explaining the variation of the data. Put another way, R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.
R-square is defined as the ratio of the sum of squares of the regression (SSR) and the total sum of squares (SST).
R2=SSRSST=1−SSESSTR2=SSRSST=1-SSESST">R2=SSRSST=1−SSESSTR2=SSRSST=1−SSESSTR2=SSRSST=1−SSESSTR2=SSRSST=1-SSESST
R-square can take on any value between 0 and 1, with a value closer to 1 indicating that a greater proportion of variance is accounted for by the model. For example, an R-square value of 0.8234 means that the fit explains 82.34% of the total variation in the data about the average.
Adjusted R-Square
The adjusted R-square statistic is generally the best indicator of the fit quality when you compare two models that are nested — that is, a series of models each of which adds additional coefficients to the previous model.
adjustedR-□= 1−SSE(n−1)SST(v)1-SSE(n-1)SST(v)">adjustedR−□= 1−SSE(n−1)SST(v)1−SSE(n−1)SST(v)adjustedR-□= 1−SSE(n−1)SST(v)1-SSE(n-1)SST(v)
The adjusted R-square statistic can take on any value less than or equal to 1, with a value closer to 1 indicating a better fit. Negative values can occur when the model contains terms that do not help to predict the response.
Root Mean Squared Error
This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data, and is defined as
RMSE=s=√MSERMSE=s=MSE">RMSE=s=√MSERMSE=s=MSERMSE=s=√MSERMSE=s=MSE
where MSE is the mean square error or the residual mean square
MSE=`SEvMSE=SSEv`
Just as with SSE, an MSE value closer to 0 indicates a fit that is more useful for prediction.
case 1:
pythob code for linear polynominal
#program to perform curve fitting for linear polynominal
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(t,a,b):
return a*t + b
# curve_fit(t,a,b)
file = open('data','r')
def read_file() :
temperature = []
cp =[]
for line in file :
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
temperature,cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)
plt.plot(temperature,cp,'k--')
plt.plot(temperature,fit_cp,linewidth = 2 , color = 'blue')
plt.xlabel('temperature in K')
plt.ylabel('specific heat')
plt.legend(['original dtataset','linear polynominal'])
plt.title('linear polynominal')
plt.grid()
plt.savefig('linear fit')
plt.show()
code explanation :
Data file should be on the same registry as the python file registry. The file containing the data(contain temperature and cp values ) is copied to a variable in the python program .then We have the equation for quadratic polynomial or 1 degree polynomial, and cubic polynomial or 3 degree polynomical
first of Required Libraries including numpy,scipy.optimize and matplotlib.pyplot are imported
- plot/output
Curve fitting with a linear polynomial:
case 2:
python code for cublic polynominal
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2) +c*t + d
# curve_fit(f,x,y)
file = open('data','r')
def read_file() :
temperature = []
cp =[]
for line in file :
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
temperature,cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)
plt.plot(temperature,cp,'k--')
plt.plot(temperature,fit_cp,linewidth = 2 , color = 'blue')
plt.legend('temperature','cp')
plt.legend(['original data','cubic_polynomial'])
plt.xlabel('temperature in K')
plt.ylabel('specific heat')
plt.title('cubic_polynomial')
plt.grid()
plt.savefig('cubic_fit')
plt.show()
- plot/output
Curve fitting with cublic polynomial:
- QUESTIONS :
1. What does popt and pcov mean?
'popt' and 'pocv' are the outputs of the function 'curve_fit'. 'popt' is the array that contains the optimal parameters or the optimum values of the coefficients of the cubic equation. 'pcov' is a 2D array that contains the estimated covariance of the parameters in 'popt'.
- CONCLUSION :