Challenge

Week 5 - Curve fitting

AIM: Curve fitting using Python.

OBJECTIVES:

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

2. Comparing the the fit of linear and cubic polynomial.

3. About popt, pcov, np.array(temperature) and *popt.

PYTHON CODE FOR LINEAR POLYNOMIAL:

#Several libraries are imported 
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 ahve 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:

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. To overcome this, we will increase the order of the polynomial to get a better fit.

PYTHON CODE FOR CUBIC POLYNOMIAL:

#Several libraries are imported 
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 ahve 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:

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 coeffecients of the function which can be used in the curve fit function to get Cp values.

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

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

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

We can make the curve fit proper by improving the degree of the polynomial or by breaking the curve into tempearure 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.