Challenge

Week 5 - Curve fitting

1. What does popt and pcov mean?

The curve_fit function returns two items, which we call popt and pcov.The popt argument are the best fit for 'a' and 'b'. The pcov variable contains the covariance matrix which indicates the uncertainties and corelation between parameters. This is mostly useful when the data has uncertainties.

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

This function stores the value of the temperature in the form of an array passes it to the function numpy.

3. What does the * in *popt mean?

popt contains the value for 'a' 'b' and 'c' and using the * we are referring to these value.

4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.

We will start the code with importing the modules.

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

A function is defined consisting of linear equation

#Curve fit function
def func(t, a, b):
	return a*t + b
#Reading thermodynamics 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()
a = 0
b = 399

fit_cp = []
for i in range(0,8):

	c = temperature[a:b+1]
	d = cp[a:b+1]

	popt, pcov = curve_fit(func, c, d)
	z = func(np.array(c), *popt)


	for j in z:

		fit_cp.append(j)



	a = b + 1
	b = b + 400
plt.figure()
plt.plot(temperature, cp, color = 'green')
plt.plot(temperature, fit_cp, color = 'red')
plt.legend(['Actual data','curve fit'])
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.