Week 5 - Curve fitting
1. What does popt and pcov mean?
popt: The output is an array with the values for the parameters used above so that the sum of the
squadred residuals of data is minimized.
pcov: 2nd array with the estimated covariance of the parameters in popt.Used to calculate the
standard deviation errors pf popt.
2. What does np.array(temperature) do?
This function stores the values of temperature in the form of array and passes it to the function numpy.
3. What does the *in* popt mean?
It stores the values of the constant parameters (a,b.c) in the array popt.
4. Python Code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def function(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
def read_file():
temperature=[]
cp=[]
for line in open('data','r'):
values=line.split(',') #split the lines
temperature.append(float(values[0]))
cp.append(format(values[1]))
return [temperature,cp]
temperature,cp=read_file() #calling the function for temperature and specific values
popt,pcov=curve_fit(function,temperature,cp)
fit_cp=function(np.array(temperature),*popt)
#plotting the results
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')
plt.ylabel('Cp')
plt.show()
RESULT FOR CUBIC POLYNOMIAL:
5.What needs to be done in order to make the curve fit perfect?
In order to make perfect fit, we must consider error estimates as well also
curvefit by using splitwise polynomial.
