Challenge

Week 5 - Curve fitting

1. popt and pcov are the arrays returned by the function 'curve_fit'.

popt is the optimum values of the parameters so that the sum of squared residuals is minimized.

pcov is the estimated covariance of the popt

2. np.array(temperature) converts the class of 'temperature' form 'list' to 'array'. We do that because arithmetic operations cannot be performed on list.

3. * in *popt refers to the values of a, b, c stored in the array 'popt' so that we will not have to assign the value of a, b, c individually from popt.

4.

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

# curve fit function
def func_linear(t, a, b):		# linear function
	return a * t + b
def func_cubic(t, a, b, c, d):	# cubic function
	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_linear, pcov_linear = curve_fit(func_linear, temperature, cp)	# co-efficients for linear function
popt_cubic, pcov_cubic = curve_fit(func_cubic, temperature, cp)		# co-efficients for cubic function
linear_fit_cp = func_linear(np.array(temperature), *popt_linear)	# linear fit cp values
cubic_fit_cp = func_cubic(np.array(temperature), *popt_cubic)		# cubic fit cp values

# Linear curve fitting

f1 = plt.figure()
plt.plot(temperature, cp, color = 'blue', linewidth = 3)			# plotting original cp data
plt.plot(temperature, linear_fit_cp, color = 'red', linewidth = 3)	# plotting linear fit cp data
plt.legend(['Actual Data', 'Linear Curve Fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.title('Linear Curve Fitting', loc='center')
plt.show()

# Cubic curve fitting

f2 = plt.figure()
plt.plot(temperature, cp, color = 'blue', linewidth = 3)			# plotting original cp data
plt.plot(temperature, cubic_fit_cp, color = 'lime', linewidth = 3)	# plotting cubic fit cp data
plt.legend(['Actual Data', 'Cubic Curve Fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.title('Cubic Curve Fitting', loc='center')
plt.show()

Results: The linear fit curve is less accurate than the cubic fit curve, so we can conclude that the higher the degree of polynomial the better fit. The plots are shown below:

5. To fit the curve perfectly we can either increase the degree of polynomial or we can devide the data in two parts and fit the first half with quardratic or cubic polynomial and the second half with a linear polynomial.

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.