Project

Curve Fitting using Python

AIM

To fit a given set of data by linear and cubic polynomial

OBJECTIVES

  • To plot linear and cubic fit curves along with raw data points
  • To measure the goodness of the fit

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 construct the functional relationship between the observed data-set and parameter values, etc. It is highly effective in the mathematical modeling of some natural processes.

How to measure goodness of fit

The goodness of the fit is measured using the R-square method.

Assume that the data points are (x1,y1) , (x2,y2) , (x3,y3) , ... (x(n),y(n)) and the curve fit for this set of points will be y = f(x).

If each of these n points has an individual error(i.e difference in y-values), then in general the error can be given by

And a quantity SSE (Sum of squared errors) can be described as

If the mean is taken for all the n number of data

And the SSR (Sum of squares of Regression), where Regression is difference between the value of the function at a given point and the Mean.

Now, the term SST is defined as

The quantity R-square that measures how successful the fit is in explaining the variation of the data. 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.

In layman's terms is that the higher the value of R-square the better the fit, the lower it is the lousier the fit.

R-square is defined as

R-square ranges from a value of 0 to 1

  • 0 for the worst possible fit for the data set
  • 1 for the best possible fit for the data set

So, closer the value to 1,better the fit.

INPUT DATA

The Temperature-Specific Heat data is provided in the link

https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view

CODE

Programming Language used- PYTHON

#Curve fitting 
#Importing the required modules
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

#Defining the curve fitting functions
#Linear function
def func1(t,a1,b1):
 	return a1*t+b1

#Cubic function
def func2(t,a2,b2,c2,d2):
 	return a2*pow(t,3)+b2*pow(t,2)+c2*t+d2

#Function to read from 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]

#Main program
temperature,cp=read_file()
popt1,pcov1=curve_fit(func1,temperature,cp)
popt2,pcov2=curve_fit(func2,temperature,cp)
fit_cp1=func1(np.array(temperature),*popt1)
fit_cp2=func2(np.array(temperature),*popt2)

#Plotting actual fit and curve fit for linear polynomial
plt.suptitle('Plots for curve fitting of Specific Heat vs Temperature data')
plt.subplot(1,2,1)
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp1,color='red',linewidth=3)
plt.legend(['Actual fit','Linear Curve fit'])
plt.xlabel('Temperature (in Kelvin)')
plt.ylabel('Specific Heat(in J/kgK)')
plt.title('Linear curve fit')

#Pltting actual fit and curve fit for cubic polynomial
plt.subplot(1,2,2)
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp2,color='red',linewidth=3)
plt.legend(['Actual fit','Cubic Curve fit'])
plt.xlabel('Temperature (in Kelvin)')
plt.ylabel('Specific Heat(in J/kgK)')
plt.title('Cubic curve fit')
plt.show()

#Calculating Goodness of fit
#Mean value of specific heat from data
M=np.mean(cp)

#Function for calculating Goodness of fit
def gof(cp,fit_cp):
	#Sum of squares of errors
	SSE=0
	#Sum of squares of regression
	SSR=0

	for i in range(1,len(cp)):
		SSE=SSE+(pow((cp[i]-fit_cp[i]),2))
		SSR=SSR+(pow((fit_cp[i]-M),2))

	#Total sum of squares
	SST=SSE+SSR
	#Calculating R_square
	R_square=SSR/SST
	return R_square

#Goodness of Fit for linear fit
R_square1=gof(cp,fit_cp1)
print('The value of R_square for the linear fit is',R_square1)

#Goodness of Fit for cubic fit
R_square2=gof(cp,fit_cp2)
print('The value of R_square for the cubic fit is',R_square2)

PROCEDURE

  1. First the required modules are imported.
  2. Then the linear and cubic polynomial functions are defined.
  3. The function to read and extract the required data(temperature and cp values) from the data file, ‘read_file’ is defined.
  4. Then the ‘read_file’ is called and the values are stored.
  5. After that the ‘curve_fit’ function is used for both linear and cubic polynomials.
  6. The new values of specific heat are obtained by passing the obtained values from ‘curve_fit’ to the defined functions(linear and cubic)
  7. Then the plots for curve fit and actual data are plotted.
  8. Then the mean value of specific heat from the data(to be required to calculate goodness of fit) is calculated and stored using the ‘mean()’ function.
  9. For calculating the goodness of fit, a function, ‘gof’ is defined which uses the specific heat of data file and the specific heat calculated from the curve fit as input arguments.
  10. Then the goodness of fit is calculated and printed by calling the function and passing the required arguments for linear and cubic polynomial respectively.

RESULTS

Curve fit for linear and cubic polynomial

Goodness of fit

EXPLANATION

  • The ‘numpy’ module is used for ‘array()’ command. Similarly the‘matplotlib’ module is used for plotting graphs.The curve_fit() function is used from the ‘scipy.optimize’ module.
  • These functions were plotted with the given raw data values and the plots were obtained.
  • The goodness of curve fit is obtained by the R-square method.
  • The plots are obtained and curve fit is observed. The goodness of fit is printed. From the plot and the results/values of R-square it was found out that cubic fit is a better fit.

1.What does popt and pcov mean?

popt-It is the returned array of optimal values of the coefficients

pcov-It is a 2D array that contains the estimated covariance of popt. The diagonals provide the variance of the parameter estimate.

2.What does np.array(temperature) do?

It converts the python list of temperatures to a numpy array since operations like pow() are allowed on numpy arrays and not python lists.

3.What does the * in *popt mean?

The ‘*’ in ‘*popt’ is a reference /pointer to the values respectively stored in popt.

4.What needs to be done in order to make the curve fit perfect?

To make the curve fit perfect, higher degree of the polynomial may be used or the data may be divided into different sections with different degree of polynomials for curve fit.

CONCLUSION

So, the curve fitting of the given data was done and it was found that cubic fit is a better fit than linear fit as it has a value of R-square of 0.996726.

To improve the cubic fit, we can use other degree polynomial for curve fitting for different intervals of value of data.

REFERENCE

https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.