Project

curvefit in python

1.pcov is a 2d array with the estimated covariance of the parameters in popt. It canbe used to calculate the the standard deviation errors of popt.

2.popt gives the output of the coefficients that are obtained after calculation by the function curve_fit.

3.*popt refers to the values of coefficeints a,b,c of the curvefit.

# Code to fit a Linear and Cubic Polynomial for the Cp Data

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

# Curve fit function
# Cubic Polynomial
def cubic_func(t, a, b, c, d):
	return a*pow(t,3) + b*pow(t,2) + c*t + d

#Linear Polynomial
def lin_func(a, b, c, x, y):
	return a*x + b*y + c
 
# Reading the thermodynamic data file
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]

# Main Program for Cubic Polynomial
temp, cp = read_file()
popt, pcov = curve_fit(cubic_func, temp, cp)
fit_cp = cubic_func(np.array(temp), *popt)


plt.plot(temp, cp, color = \'blue\', linewidth = 3)
plt.plot(temp, fit_cp, color = \'red\', linewidth = 3)
plt.title(\'Cubic Polynomial\')
plt.legend([\'Actual Data\', \'Curve Fit\'])
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'Cp\')

plt.show()

# Main Program for Linear Polynomial
temp, cp = read_file()
popt, pcov = curve_fit(lin_func, temp, cp)
fit_cp = lin_func(np.array(temp), *popt)

plt.plot(temp, cp, color = \'blue\', linewidth = 3)
plt.plot(temp, fit_cp, color = \'red\', linewidth = 3)
plt.title(\'Linear Polynomial\')
plt.legend([\'Actual Data\', \'Curve Fit\'])
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'Cp\')

plt.show()

\"LINEAR

\"CUBIC

5.In order to make the curvefit perfect, We have to increase the order of polynomial for curve,instead of selecting three we can select higher orders, and do trial and error method and the most appropriate curvefit can be found out which has least error percentage.For example a quadratic polynomial curvefit at^4+bt^3+ct^2+dt+e..will produce a much more accurate curvefit.

4.The cubic curvefit was better than the linear curvefit as the curvefit is more blended to the actual data.

refernces-

1.https://www.jonathangross.de/files/IPCS2015/Data%20Fitting%20with%20SciPy%20and%20NumPy.pdf

2.Course vedio,edxemgine projects

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.