1)popt: It gives an array with the optimal values for the parameters so that the sum of the squared residuals of data is minimized
pcov:It is a 2d array with the estimated covariance of the parameters in popt. It can be used to calculate the the standard deviation errors of popt.
2) This function stores the values of temperature in the form of array and passes it to the function numpy when applied.
3)It stores the values of the constant parameters in the array popt.
# Curve Fit By Rajeeh Rasheed
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 data_file
def read_file():
temperature=[]
cp=[]
for line in open(\'data\',\'r\'):
temperature.append(float(line.split(\',\')[0]))
cp.append(float(line.split(\',\')[1]))
return [temperature,cp]
#main program
temperature,cp=read_file()
popt,pcov=curve_fit(func,temperature,cp)
print(popt)
print(pcov)
fit_cp=func(np.array(temperature),*popt)
plt.plot(temperature,cp,linewidth=3,color=\'blue\')
plt.plot(temperature,fit_cp,linewidth=3,color=\'green\')
plt.legend([\'actual data\',\'curve fit\'])
plt.xlabel(\'Temperature[k]\')
plt.ylabel(\'cp\')
plt.show()
5) To get the best curve fit , It needs to increase the degree of polynomial as the error between actual data & curve fit becomes less.