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.

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.

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 fit.

4.Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
 
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()

Result:
The results are good . It is because the cubic fit is better curve fit than the linear one.
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.