Project

Curve fitting and Best Fit Analysis in PYTHON

OBJECTIVE:-

To write a code on curve fitting and demonstrate the best fit on the given thermodynamic data.

SOLUTION:-

Basically, 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 parametric values. It is highly effective in the mathematical modelling of some natural values.

Here in this challenge, we were given with a thermodynamic data set consisting of temperature and specific heat values. we have to read it and then plot the actual values curve fit and match the curve fit with various degree of the polynomial equation to match the actual curve fit as well as to determine the curve fit.

Here is the code as follows:-

\'\'\'
CODE TO ANALYZE THE CURVE FITTING BY INCREASING THE DEGREE OF POLYNOMIAL
BY SHRABAN
\'\'\'

import matplotlib.pyplot as plt 
import numpy as np 
from scipy.optimize import curve_fit

#QUADRATIC CURVE FIT FUNCTION
def quadratic_poly(t,a,b,c,d,e):
	A=a*pow(t,4)+b*pow(t,3)+c*pow(t,2)+d*t+e
	return A

#CUBIC CURVE FIT FUNCTION
def  cubic_poly(t,q,w,y,r):
	B=q*pow(t,3)+w*pow(t,2)+y*t+r
	return B

#LINEAR CURVE FUNCTION
def linear_poly(t,g,h):
	C=g*t + h
	return C

#READING THERMODYNAMIC DATA FILE FUNCTION
def  read_file():
	cp=[]
	temperature=[]
	for line in open(\'data\',\'r\'):
		values=line.split(\',\')
		cp.append(float(values[1]))
		temperature.append(float(values[0]))

	return [temperature,cp]

#PLOTTINF THE DATA FUNCTION
def perform_curve_fit(temperature,cp,fit_cp1,fit_cp2,fit_cp3):
	plt.figure(1)
	plt.plot(temperature,cp,\'c\',linewidth=2)
	plt.plot(temperature,fit_cp2,\'--k\')
	plt.plot(temperature,fit_cp3,\':r\')
	plt.legend([\'Actual Data\',\'Cubic Curve Fit\',\'Linear Curve Fit\'],loc=\'best\')
	plt.xlabel(\'Temperature[K]\',fontsize=12)
	plt.ylabel(\'Cp\',fontsize=12)
	plt.title(\'Performing linear and Cubic Curve Fits\',fontsize=15,color=\'m\')
	plt.savefig(\'cubic_linear.png\')
	plt.show()

	plt.figure(2)
	plt.plot(temperature,cp,\'c\',linewidth=2)
	plt.plot(temperature,fit_cp1,\'--k\')
	plt.legend([\'Actual Data\',\'Quadratic Curve Fit\'],loc=\'best\')
	plt.xlabel(\'Temperature[K]\',fontsize=12)
	plt.ylabel(\'Cp\',fontsize=12)
	plt.title(\'Performing Quadratic Curve Fits\',fontsize=15,color=\'b\')
	plt.savefig(\'quadratic.png\')
	plt.show()

\'\'\'
Storing the coefficients of polynomials in popt arrays
after reading the thermodynamic properties from data file
\'\'\'

temperature,cp =read_file()
popt1,pcov1=curve_fit(quadratic_poly,temperature,cp)
popt2,pcov2=curve_fit(cubic_poly,temperature,cp)
popt3,pcov3=curve_fit(linear_poly,temperature,cp)

\'\'\'
Generating numpy array for the temperature values and storing fit values for Cp using different polynomials
\'\'\'
temp=np.array(temperature)
fit_cp1=quadratic_poly(temp,*popt1)
fit_cp2=cubic_poly(temp,*popt2)
fit_cp3=linear_poly(temp,*popt3)

#plots
perform_curve_fit(temperature,cp,fit_cp1,fit_cp2,fit_cp3)

	

OUTPUTS:-

\"\"

\"\"

QUESTIONS GIVEN IN THIS CHALLENGE:-

1. What do popt and pcov mean? (Watch the video to get some context)

\"\"

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

3. What does the * in *popt mean?

4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.

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

ANSWERS:-

1.

popt- It is an array that contains the optimal parameters or the optimization values of the coefficients of the cubic equation.

pcov:- It is a 2D array that contains the estimated covariance of the parameters in popt. To compute one standard deviation errors on the parameters use:-

perr=np.sqrt(np.diag(pconv))

2.

\'np.array()\'- It is used to create a numpy array and temp is passed as data of the array. Numpy array is a central data structure of the numpy library, which is a numerical library of python.

3.

\'*popt\' is used to expand the list of the values stored in the array \'popt\' into the arguments of the function.

4.

From the plots obtained above,

  • The linear fit is showing poor results.
  • The cubic fit is showing good results that are quite accurate.
  • The quadratic fit is the best fit.

5.

Curve fit can be made perfect by Interpolation.

  • Curve fit applies a single function to the entire range of the data while the interpolation method applies a single function for each line of the graph.
  • As the polynomial order increases, the curve fit might match all the data points.
  • Choice of best fit always depends on the explanation of actual behaviour of the data i.e. data can be divided into small portions to achieve a better fit.
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.