Challenge

Week 5 - Curve fitting

1. popt contains the values for the coefficients of our function, that is, a, b and c as shown in the video. The pcov contains the estimated covariance of the values for constants present in popt.

The covariance shows the extent of how much two random variables vary together.

2. Here, "np" stands for NumPy module which is the core library for scientific computing in Python.

It provides a high-performance multidimensional array object and tools for working with these arrays. A NumPy array is a grid of values, of all the same type and is indexed by a tuple of non-negative integers.

3. The * means that is referring to values present in the popt which are coefficient of the function or constant of the function.

4. Code to fit a linear polynomial for the Cp data:

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

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

#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, pocv = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)

plt.plot(temperature,cp, color = 'blue', linewidth = 3)
plt.plot(temperature, fit_cp, color = 'red', linewidth = 3)
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel( 'Temperature [K]')
plt.ylabel('Cp')

plt.show()

Output:

Code to fit a cubic polynomial for Cp data:

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

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

#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, pocv = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)

plt.plot(temperature,cp, color = 'blue', linewidth = 3)
plt.plot(temperature, fit_cp, color = 'red', linewidth = 3)
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel( 'Temperature [K]')
plt.ylabel('Cp')

plt.show()

Output:

As we can see, the results for the linear polynomial are not good primarily because the actual data does not follow a linear equation. There is also some variance in the curve fit plot near the origin.

Now, as for the results obtained from the plots of the cubic polynomial, they are good but of course, not perfect. I think that if it were, indeed a fourth-degree polynomial equation, the curve fit would indeed match the actual data plot.

Code for the fourth order polynomial:

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

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

#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, pocv = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)

plt.plot(temperature,cp, color = 'blue', linewidth = 3)
plt.plot(temperature, fit_cp, color = 'red', linewidth = 3)
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel( 'Temperature [K]')
plt.ylabel('Cp')

plt.show()

Output:

5. In order to make a perfect fit, we simply need to increase the order of the equation and on top of that, we need to estimate the errors and then eliminate them so that the actual data can overlap to the curve fit. For this, simple linear regression, or the Root Mean Square (RMS) method to obtain the perfect fit.

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.