Project

Curve fitting using PYTHON

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

#x should be varied from 1 to 5
x=5

if x is 1:
	def func(t,a,b):
		return a*t +b
elif x is 2:
	def func(t,a,b,c):
		return a*pow(t,2)+b*t +c
elif x is 3:
	def func(t,a,b,c,d):
		return a*pow(t,3)+b*pow(t,2) +c*t +d
elif x is 4:
	def func(t,a,b,c,d,e):
		return a*pow(t,4)+b*pow(t,3) + c*pow(t,2) +d*t + e
elif x is 5:
	def func(t,a,b,c,d,e,f):
		return a*pow(t,5) +b*pow(t,4)+ c*pow(t,3) + d*pow(t,2) +e*t + f 
def read_file():
	temp=[]
	cp=[]
	for line in open(\'data\',\'r\'):
		values=line.split(\',\')
		temp.append(float(values[0]))
		cp.append(float(values[1]))
	return [temp,cp]
temp,cp =read_file()
print([cp],[temp])
popt,pcov =curve_fit(func,temp,cp)
fit_cp =func(np.array(temp),*popt)


if x is 1 :
	x_name=\'first order\'
elif x is 2 :
	x_name=\'2nd order\'
elif x is 3 :
	x_name=\'3rd order \'
elif x is 4 :
	x_name=\'4th order\'
elif x is 5 :
	x_name=\'5th order \'

plt.plot(temp,cp,color=\'red\')
plt.plot(temp,fit_cp,color=\'blue\')
plt.title(\'plot for \'+ x_name  +\'polynomial\')
plt.xlabel(\'temperature\')
plt.ylabel(\'Cp\')
plt.legend([\'actual data\',\'curve fit\'])
plt.show()

Firstly, we define the polynomial equation as a function which will be used for curve fitting.

For each defined value of x the polynimial is going to have a different value.Linear for x=1,quadratic for x=2 and so on.X denotes the degree of the polynomial.We obtain the curve fir plot for each value of x from 1 to 5


Then another function is created which reads the data file and segregates the temperature and cp values into two lists as floats.
Then after reading the data, we use the ‘curve_fit’ function which tries to estimate the curve according to the specified polynomial.


What does popt and pcov mean?

popt contains the values of the constants of the ‘predicted curve’ equation. ie. the optimized values of the coeffecients a,b,c... in the specified polynomial.


pcov is a 2d array which is the estimated covariance of popt. The covaiance is proportional to the slope of line created by array values as a function.

Then we create another list ‘fit_cp’ which is the fit curve.
The original and curve fit are both plotted together in different colours.


What does np.array(temperature) do?


np.array(temperature) converts the list \'temperature’ into an array. This is done because we can’t perform operations on the entire list at once. However, we can do so after creating an array of it.

What does the * in *popt mean?
Since popt can hold different number of values depending on the order of the function polynomial, we use ‘*popt’ which allows us to pass arbitrary number of arguments.


Outputs of linear and cubic polynomial fit:

It is evident from the graphs that the cubic polynomial fit is better than the linear polynomial fit.

Infact the more,degree of the polynomial is raised,the more it fits the function.



What needs to be done in order to make the curve fit perfect?
In order to make the curve fit perfectly, we can do the following things:

The order of the polynomial should be one less than the number of data points.
Or we can divide the curve into different parts and curve fit each of them separately.

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.