Project

Curve fit using python

Curve fitting is done in order to study the characteristic equation between a dependent and one or more independent variables. The fitting is done in order to find out which order of polynomial offers the best fit and how many regions the data can be split into in order to figure out the optimal fit for the given data points.

This equation can then be used to plot the curves and study which polynomial offers the best fit.

1. The curve_fit function returns two values, popt and pcov. popt returns the values of the coefficients of the polynomial used and pcov refers to the covariance of these coefficients. The standard deviation of the coefficients can then be obtained by finding the square root of the diagonal elements of the matrix.

2. np.array(temperature) saves all the values of temperature in an array from the sequence it was saved as so that the values can be indexed and called for each coefficient to achieve the Cp at every temperature.

3. The * is used to call the arguments, which in this case are the polynomials. Instead of calling each coefficient separately, *popt can be used to call all the coefficients in one go, regardless of the number of coefficients.

4.Code to fit the curve for a linear and a cubic polynomial:

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

#defining the linear function
def lin_fun (t,a,b):
	
	return a*t + b

#defining the cubic function
def cub_fun (t,a,b,c,d):

	return a*pow(t,3)+b*pow(t,2)+c*t+d

#obtaining data from the file and storing in arrays
def 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]

#fitting the curve
temp, Cp = file()
popt, pcov = curve_fit(lin_fun, temp, Cp)
lin_Cp = lin_fun(np.array(temp), *popt)

popt, pcov = curve_fit(cub_fun, temp, Cp)
cub_Cp = cub_fun(np.array(temp), *popt)

#Plotting
plt.figure()
plt.plot(temp, Cp, \'r\', linewidth = 3)
plt.plot(temp, lin_Cp, \'b\', linewidth = 3)
plt.legend([\'Actual data\', \'Linear fit data\', \'Cubic fit data\'])
plt.savefig(\'lin.png\')

plt.figure()
plt.plot(temp, Cp, \'r\', linewidth = 3)
plt.plot(temp, cub_Cp, \'b\', linewidth = 3)
plt.legend([\'Actual data\', \'Cubic fit data\'])
plt.savefig(\'cub.png\')
plt.show()

Results:

Curve fit using a linear polynomial:

\"\"

Curve fit for a cubic polynomial:

\"\"

As we can see from the plots, the level of accuracy differs based on the order of the polynomial used. The more number of coefficients, the less the error due to truncation.

5. In order to have a perfect fit, there needs to be zero error due to truncation. While this is physically impossible, using higher order errors can reduce the error so that the fit is optimal. Another way to improve the fit is by using a smaller data range and plotting multiple times in order to have many linear plots rather than one curve. This can improve the level of fit for lower order polynomials.

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.