Challenge

Week 5 - Curve fitting

1. What does popt and pcov mean?

popt- An array of optimal values for the parameters which minimizes the sum of squares of residuals.

pcov-2d array which contains the estimated covariance of popt. The diagonals provide the variance of the parameter estimate.

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

np.array- converts the python list of temperature to a numpy array since operations like pow are allowed on numpy arrays and not python lists.

3. What does the * in *popt mean?

*-reference to popt / the values stored in the array popt.

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

For linear 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*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,pcov=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

For cubic 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):
 	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,pcov=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

Explaination on Result.

The results are good because the cubic fit is better curve fit than the linear one as we can see from the output plots.

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

  • By increasing the degree of the curve we can make the best curve fit.
  • If we further increase the degree of the curve to 4 then the curve will be more accurate than the cubic polynomial.
  • Further increasing the degree of polynomial will increase the calculating complexity and time required, hence 4th order polynomial can be taken as a sufficient curve fi
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.