Challenge

Week 5 - Curve fitting

1. What does popt and pcov mean?

popt is a variable used to store the values of the coefficients in the polynomial function which are calculated using the curve_fit() function.

pcov is the estimated covariance (a measure of joint variability) of popt.

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

np.array(temperature) is used to pass temperature as a numpy array.

3. What does the * in *popt mean?

The asterisk (*) in *popt is used to refer to the coefficients in the defined polynomial function which are stored in popt, calculated using the curve_fit() function.

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

Aim:

To write a python code to fit a linear and cubic polynomial for the given Cp data and explain the results.

Python code: (with explanation)

Curve fit is the process of constructing a curve or a mathematical function that has the best fit to a series of data points. Such a function will capture the trend in the data and aid in predicting the its behaviour in future. Two methods of curve fitting include linear and polynomial curve fitting.

#Title - Curve Fitiing

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

#Curve Fit Function

#Case 1: Linear polynomial
def func1(t1, a1, b1):
	return a1*t1 + b1

#Case 2: Cubic polynomial
def func2(t2, a2, b2, c2, d2):
	return a2*pow(t2,3) + b2*pow(t2,2) + c2*t2 + d2


#For higher order polynomials
def func3(t3, a3, b3, c3, d3, e3):
	return a3*pow(t3,4) + b3*pow(t3,3) + c3*pow(t3,2) + d3*t3 + e3


#Reading data file
def read_data():		#defining the read_data function
	temperature = []			#creating empty array for temperature
	cp = []						#creating empty array for specific heat (cp)
	for line in open('data', 'r'):				#opening the data file in read mode and reading its content line by line
		values = line.split(',')				#splitting each line by comma and producing an array with two entries
		temperature.append(float(values[0]))	#appending values to temperature
		cp.append(float(values[1]))				#appending values to cp

	return [temperature, cp]

#Main function
temperature, cp = read_data()		#calling the read_data function

#Case 1: Linear polynomial
popt1, pcov1 = curve_fit(func1, temperature, cp)		#using the curve_fit function in the scipy module for func1		#popt1 is the output which contains the values of coefficients a1 and b1 calculated by the curve_fit function.
fit_cp1 = func1(np.array(temperature), *popt1)			#passing temperature array and the coefficients into the func1
#print(pcov1)

#Case 2: Cubic polynomial
popt2, pcov2 = curve_fit(func2, temperature, cp)
fit_cp2 = func2(np.array(temperature), *popt2)

#For higher order polynomials
popt3, pcov3 = curve_fit(func3, temperature, cp)
fit_cp3 = func3(np.array(temperature), *popt3)

#Curve Fit Plot

#Case 1: Linear polynomial
plt.figure()
plt.plot(temperature, cp, color = "green", linewidth = 3)
plt.plot(temperature, fit_cp1, color = 'red', linewidth = 3)
plt.title('Linear polynomial - Curve fit')
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel('Temperature (K)')
plt.ylabel('Cp')
plt.savefig('W5_CurveFit1')
plt.show()

#Case 2: Cubic polynomial
plt.figure()
plt.plot(temperature, cp, color = "green", linewidth = 3)
plt.plot(temperature, fit_cp2, color = 'blue', linewidth = 3)
plt.title('Cubic polynomial -  Curve fit')
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel('Temperature (K)')
plt.ylabel('Cp')
plt.savefig('W5_CurveFit2')
plt.show()

#For higher order polynomials
plt.plot(temperature, cp, color = "green", linewidth = 3)
plt.plot(temperature, fit_cp3, color = 'yellow', linewidth = 3)
plt.title('Higher order polynomial - Curve fit')
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel('Temperature (K)')
plt.ylabel('Cp')
plt.show()

Results:

Linear polynomial Cubic polynomial

It is evident from the above plots and subsequent trials in python that higher order polynomials can be used to obtain a better curve fit.

5. What needs to be done in order to make the curve fit perfect?

In order to obtain a perfect curve fit, higher order polynomials (order > 4) must be used.

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.