Challenge

Week 5 - Curve fitting

AIM: Curve fitting using Python.

OBJECTIVES: 1. Code to fit a linear and cubic polynomial for fitting Cp data.

  1. Comparing the fit of linear polynomials.
  2. Comparing the fit of cubic polynomials.
  3. About popt, pcov, np.array(temperature) and *popt.



CODE FOR LINEAR POLYNOMIAL:

'''PYTHON CODE FOR LINEAR POLYNOMIAL'''
#import library
import math
import numpy
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
# Linear curve fit function
def func(t,a,b): #function defined
	return a*pow(t,1)+b
# Reading the thermodynamic datafile
def read_file():
	#empty array of temperature and array are created
	temperature=[] 
	cp=[]
	#used for reading the data from the file
	for line in open('data','r'):
		values=line.split(',') #the line is split at the juncture of the ','
		#the values of temperature and cp are appended in the the respective arrays
		temperature.append(float(values[0])) #float is used as the data in the file is in string format and
 # we have to convert into float format
		cp.append(float(values[1]))
	return [temperature,cp]
#Main Program
temperature,cp=read_file() #function call to read the temperature and the cp from the file data
popt,pcov=curve_fit(func, temperature, cp) # we get coeff of func in popt and pcov is used 
fit_cp=func(numpy.array(temperature),*popt) # cp values from the equation formed 
#Plot
plt.plot(temperature,cp,'blue')
plt.plot(temperature,fit_cp,'red')
plt.legend(['Actual Data','Curve Fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()



Plot graph :-

As you can see in the above plot the actual data and the curve fit don't match and it is not a good curve fit Soto overcome this, we will increase the order of the polynomial to get a better fit.

CODE FOR CUBIC POLYNOMIAL:

#PYTHON CODE FOR CUBIC POLYNOMIAL:-
#import library
import math
import numpy
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

# Cubic curve fit function
def func(t,a,b,c,d): #function defined
	return a*pow(t,3)+b*pow(t,2)+c*pow(t,1)+d


# Reading the thermodynamic datafile
def read_file():
	#empty array of temperature and array are created
	temperature=[] 
	cp=[]
	#used for reading the data from the file
	for line in open('data','r'):
		values=line.split(',') #the line is split at the juncture of the ','
		#the values of temperature and cp are appended in the the respective arrays
		temperature.append(float(values[0])) #float is used as the data in the file is in string format and
#we have to convert into float format
		cp.append(float(values[1]))
	return [temperature,cp]
#Main Program
temperature,cp=read_file() #function call to read the temperature and the cp from the file data
popt,pcov=curve_fit(func, temperature, cp) # we get coeff of func in popt and pcov is used 
fit_cp=func(numpy.array(temperature),*popt) # cp values from the equation formed 


#Plotting 
plt.plot(temperature,cp,'blue')
plt.plot(temperature,fit_cp,'red')
plt.legend(['Actual Data','Curve Fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()

Plot graph:-

As you can see in the above plot the results of the curve fit are more accurate and this can be termed as a good fit.

POPT: This refers to the array containing coefficients of the function which can be used in the curve fit function to get Cp values.

PCOV: This is used to finding the coefficient of variance of the data.

*POPT : This points to the coefficients of the function within the popt array.

numpy.array(temperature) : It is used to convert a list of temperature items into an array.

We can make the curve fit properly by improving the degree of the polynomial or by breaking the curve into temperature intervals and using different polynomials for the same.

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.