Project

Curve fitting

1. What does popt and pcov mean? (Watch the video to get some context)

\"\"

popt calculates the coefficient of the equations and pcov is a 2d array with the estimated covariance of the parameters in popt. It can be used to calculate the standard deviation errors of popt

2. What does np.array(temperature) do?

np.array stores all the numerical values in a matrix form assigning all values a particular index

3. What does the * in *popt mean?

it takes the respective values of the coefficient from popt

Code

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

# Curve fit function
def func(t, a, b, c):
	return a*pow(t,2) + b*t +c

def funclinear(t,a1,b1):
	return a1*t + b1

def funcpoly(t, a2, b2, c2, d2, e2):
	return a2*pow(t,4) + b2*pow(t,3) + c2*pow(t,2) + d2*t + e2



# Reading thermodynamic data file
def read_file():
	temperature=[]
	cp=[]
	for line in open(\'data\',\'r\'):
		values = line.split(\',\')
		temperature.append(float(values[0]))
		cp.append(float(values[1]))
	return [temperature,cp]


# Main program
temperature,cp = read_file()

popt,pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)


popt,pcov = curve_fit(funclinear,temperature,cp)
fit_cp_linear = funclinear(np.array(temperature),*popt)

popt,pcov = curve_fit(funcpoly,temperature,cp)
fit_cp_poly = funcpoly(np.array(temperature),*popt)


plt.plot(temperature, cp, color=\'blue\', linewidth=3)
plt.plot(temperature, fit_cp, color=\'red\', linewidth=3)
plt.plot(temperature, fit_cp_linear, color=\'yellow\', linewidth=3)
plt.plot(temperature, fit_cp_poly, color=\'purple\', linewidth=3)



plt.legend([\'Actual data\', \'Quadratic\', \'Linear\',\'Polynomial\'])
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'Cp\')

plt.show()	


\"\"

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.