Week 5 - Curve fitting
ANSWERS:
1. What does popt and pcov mean?
- popt
- an array of optimal values for the parameters which minimizes the sum of squares of residual.
- popt is considered as the unknown value of the fuction
- * (star) help to refrers the values.
- pcov
- 2d aarray which contains the estimated covarience of popt. the diagonals provide the varience of the parameters estimate.
- The returned parameter covariance matrix pcov is based on scaling sigma by a constant factor. This constant is set by demanding that the reduced chisq for the optimal parameters popt when using the scaled sigma equals unity. In other words, sigma is scaled to match the sample variance of the residuals after the fit.
2. What does np.array(temperature) do?
- it converts python list of temperature to a numpy array since operations like pow are allowed on numpy arrays and not python lists.
3. What does * in *popt mean?
- * popt contains the value of unknown fuction.
- * (star) value it helps to refers the value
- *- refrence to popt /the value stored in the array popt
4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# linear 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()
# polynomial curve fit function
def func(t, a, b, c, d):
return a*pow(t,3) + b*pow(t,2) + c*t+d
# 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()
RESULTS:
FIG 1. LINEAR POLYNOMIAL

FIG 1. CUBIC POLYNOMIAL

THE RESULTS ARE GOOD BECAUSE THE CUBIC FIT IS BETTER CURVE THAN THE LINEAR ONE AS WE CAN SEE FROM THE OUTPUT PLOTS.
5.what need to be done in order to make the curve fit better?
- by increasing the degree of the curve we can make the best curve fit
- if we further increase the degree of the curve to 4 then the curve will be more accurate than the cubic polynomial.
- further increase the degree of polynomial will increase the calculating complexity and time required, hence 4rth order polynomial can be taken as a sufficient curve fit.
6. Show empirically as to how well your curve has been fit ?
- By observation, a cubic predicted curve has been almost fit into original data curve with some tolerance compared to linear predicted curve. If curve fit overlapped to original data with comes under the defined some tolerances, it is called best fit.