Week 5 - Curve fitting
Objective :-
1. What does popt and pcov mean?
2. What does np.array(temperature) do?
3. What does the * in *popt mean?
4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
5. What needs to be done in order to make the curve fit perfect?
6. Show empirically as to how well your curve has been fit.
Defining popt and pcov:
i. popt:- The popt argument is the best-fit parameters for Coefficients defined in the function for the curve fit. The popt holds the Coefficients (unknowns).
ii. pcov:- The pcov variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters. This is most useful when the data has uncertainties.
Defining np.array(temperature):
To give power to each element of an array or to perform any mathematical operations (like addition or subtraction) either on every element of an array or between the arrays, we need to import the numpy library. Here we called the numpy module but to use it we are using (np) because we imported the numpy module is as "import numpy as np". The temperature is an array because it contains more than one value, so we need to define it first. hence using an array(temperature) here.
Defining popt:
popt contains the value a, b and c and using (*) to refer this values. (i.e calling value of a,b,c)
PROGRAM :-
1) code to fit linear and cubic polynomial .
(combined program)
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
def func_linear(t, a, b):
return a * t + b
def func_cubic(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 = []
c_p = []
for line in open('data', 'r'):
values = line.split(',')
temperature.append(float(values[0]))
c_p.append(float(values[1]))
return [temperature, c_p]
# Main program for linear polynomial
temperature, c_p = read_file()
popt, pcov = curve_fit(func_linear, temperature, c_p)
fit_c_p = func_linear(np.array(temperature), *popt)
#plotting for linear polynomial
plt.plot(temperature,c_p,color = [0.9100,0.4100,0.1700],linewidth=2.5)
plt.plot(temperature,fit_c_p,color='g',linewidth=2.5)
plt.title('Fig.1 Specific Heat Vs Temperature')
plt.legend(['Actual data','linear curve fit'])
plt.xlabel('Temperature (k)')
plt.ylabel('Specific heat (kj/kmol-k)')
plt.savefig('linear curve fit')
plt.show()
# Main program for cubic polynomial
temperature, c_p = read_file()
popt, pcov = curve_fit(func_cubic, temperature, c_p)
fit_c_p = func_cubic(np.array(temperature), *popt)
#plotting for linear polynomial
plt.plot(temperature,c_p,color = [0.9100,0.4100,0.1700],linewidth=2.5)
plt.plot(temperature,fit_c_p,color='g',linewidth=2.5)
plt.title('Fig.2 Specific Heat Vs Temperature')
plt.legend(['Actual data','cubic curve fit'])
plt.xlabel('Temperature (k)')
plt.ylabel('Specific heat (kj/kmol-k)')
plt.savefig('cubic curve fit')
plt.show()
Ouput :-
1) graph of linear polynomial

2) graph of cubic polynomial

Program :-
2) code to fit higher order polynomial
# code to fit a higher order polynomial
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
# curve fit Function for cubic polynomial
def func_high(t,a,b,c,d,e,f):
return a*pow(t,5) + b*pow(t,4) + c*pow(t,3) + d*pow(t,2) + e*t + f
#reading thermodynamic data file
def read_file():
temperature = []
c_p = []
for line in open('data','r'):
values = line.split(',')
temperature.append(float(values[0]))
c_p.append(float(values[1]))
return [temperature,c_p]
# main program
temperature, c_p = read_file()
popt, pcov = curve_fit(func_high, temperature, c_p)
fit_c_p = func_high(np.array(temperature), *popt)
plt.plot(temperature,c_p,color = [0.9100,0.4100,0.1700],linewidth=2.5)
plt.plot(temperature,fit_c_p,color='g',linewidth=2.5)
plt.title('Fig.2 Specific Heat Vs Temperature')
plt.legend(['Actual data','Higher order curve fit'])
plt.xlabel('Temperature (k)')
plt.ylabel('Specific heat (kj/kmol-k)')
plt.savefig('higher order curve fit')
plt.show()
Ouput :-
graph of higher order polynomial
