Challenge

Week 5 - Curve fitting

Aim: To perform curve fit using python.

Objective: Answer the following questions and perform code in Python based on curve fitting.

Figure: Flow of the project

Questions to Answer

  1. What does popt and pcov mean?

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

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

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

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

  1. What does the * in *popt mean?

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

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

Code:

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()

Code for Cubic fit

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit
functiondef
func(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
#reading thermodynamic
data filedef 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 programtemperature,
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

Linear

Cubic

Conclusions

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

  1. What needs to be done in order to make the curve fit perfectly?
  • By increasing the order of equation used for curve fit.
  • By minimizing the error in any regression method.
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.