Curve fitting- python
1. What does popt and pcov mean? (Watch the video to get some context)
Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints
The first degree polynomial equation
is a line with slope a. A line will connect any two points, so a first degree polynomial equation is an exact fit through any two points with distinct x coordinates.
If the order of the equation is increased to a second degree polynomial, the following results:
This will exactly fit a simple curve to three points.
If the order of the equation is increased to a third degree polynomial, the following is obtained:
- {\displaystyle y=ax^{3}+bx^{2}+cx+d\;.}

This will exactly fit four points.
popt is the coeficient , i,e values of a,b,c,d
pcov is a 2d array witht the estimated covariance of the parameters in popt. it can be used to calculate standard deviation.
The curva fit returns to two items popt and pocv . The popt retuens the best fit parameters os a abd b.
2. What does np.array(temperature) do?
It is used to convert all the temperature into numpy function
3. What does the * in *popt mean?
The * notation will expand a list of values into the argements of the function.
4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(t,a,b):
return a*t + b
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]
temperature,cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp= func(np.array(temperature), *popt)
print(popt)
plt.plot(temperature , cp ,color="blue", linewidth=3)
plt.plot(temperature , fit_cp ,color="red", linewidth=3)
plt.legend(['Actual data','Qyadratic curve_fit'])
plt.xlabel('temperature [K]')
plt.ylabel(' specific heat @ Constant pressure Cp')
plt.savefig('plot1')
plt.show()
def func(t, a, b ,c ,d):
return a*pow(t,3) + b * pow(t,2) + c*t + d
temperature , cp = read_file()
popt, pcov = curve_fit(func, temperature , cp)
fit_cp = func(np.array(temperature), *popt)
print(popt)
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp,color='red',linewidth=3)
plt.legend(['Actual data','Cubic curve_fit'])
plt.xlabel('temperature [K]')
plt.ylabel(' specific heat @ Constant pressure Cp')
plt.savefig('plot2')
plt.show()
OUTPUT PAGE:
[9.87543761e-02 9.95201748e+02]
[ 1.90608268e-08 -1.38124292e-04 3.87891017e-01 8.38896565e+02]
[Finished in 48.5s]
Use for loop to read the data and split it using line.split then append temperature values in temperture array and specific heat in cp array
Later popt and pcov is used with curve fit function to find the coefficient
and later graph was plotted to fit to the equation.
5. What needs to be done in order to make the curve fit perfect?
We need to use higher order equation. we will get accurate result with tha data we have

