TEMPERATURE Vs SPECIFIC HEAT - LINEAR CUBIC CURVE FITTING USING PYTHON
1. What does popt and pcov mean?
Answer: curve fit calculates coefficients and output as popt. popt contains coefficients a,b,c,d.
pcov : 2d array. The estimated covariance of popt.
2. What does np.array(temperature) do?
numpy array for passing all temperature values as an array to function.
3. What does the * in *popt mean?
* refers to coefficients a,b,c,d contained in popt.
**************************************************************
CURVE FITTING PYTHON CODING
**************************************************************
# CURVE FITTING FOR LINEAR EQUATION (Y=AX+B)
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit function
def func_linear(t,a,b):
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]
# program linear
temperature,cp = read_file()
popt, pcov = curve_fit(func_linear,temperature,cp)
fit_cp = func_linear(np.array(temperature),*popt)
#GRAPH
plt.plot(temperature,cp,color=\"blue\",linewidth=3)
plt.plot(temperature,fit_cp,color=\"red\",linewidth=3)
plt.title(\'Specific Heat Vs Temperature\')
plt.legend([\'Actual data\',\'Curve fit\'])
plt.xlabel(\'Temperature[K]\')
plt.ylabel(\'cp\')
plt.show()
# CURVE FIT FOR CUBIC EQUATION (Y=AX^3+BX^2+CX+D)
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Curve fit function - a*t^3+b*t^2+c*t+d
# a, b, c, d are coefficients / constants
def func_cubic(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
# reading the data file (temperature and specific heat)
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]
# program cubic fit
temperature,cp=read_file()
popt,pcov=curve_fit(func_cubic,temperature,cp)
fit_cp=func_cubic(np.array(temperature),*popt)
#GRAPH
plt.plot(temperature,cp,color=\"blue\",linewidth=3)
plt.plot(temperature,fit_cp,color=\"red\",linewidth=3)
plt.title(\'Specific Heat Vs Temperature\')
plt.legend([\'Actual data\',\'Curve fit\'])
plt.xlabel(\'Temperature[K]\')
plt.ylabel(\'cp\')
plt.show()
******************************************
GRAPH-LINEAR CURVE FIT
GRAPH - CUBIC CURVE FIT
SCREENSHOT1 (LINEAR PLOT)
SCREENSHOT2 (CUBIC PLOT)