Week 5 - Curve fitting
AIM: To write a Python code to fit a linear and cubic polynomial for the specific heat at constant pressure(Cp) data.
THEORY:
Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values, etc. It is highly effective in the mathematical modelling of some natural processes.
Evaluation of Goodness of Fit:-
R-square-
R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.
R-square ranges from a value of 0 to 1
0 for the worst possible fit for the data set
1 for the best possible fit for the data set
GOVERNING EQUATIONS:
Linear Fit: `c_p = a*T + b`
Cubic Fit: `c_p = a*T^3 + b*T^2 + c*T + d`
for n order fit: `c_p = a_n*T^n + a_(n-1)*T^(n-1) + .... + a_1*T + a_0`
where,
T is the temperature in Kelvin.
cp is the specific heat in kJ/kmol-K.
a, b, c and d are constant coefficients.
R-square-
we have,
`c_p = f(T)`
`SSR=sum_(i=1)^n(f(T(i))−μ(c_p))`
`SSE=sum_(i=1)^n(c_p(i)−f(T(i)))`
`SST=SSR+SSE`
`R^2=(SSR)/(SST)`
OBJECTIVES:
1. To fit the specific heat as a linear function of temperature and plot the curve.
2. To fit the specific heat as a cubic function of temperature and plot the curve.
3. To compare the data obtained from the above fits with actual data of specific heat.
4. To measure the goodness of the fits.
PYTHON CODE:
# Curve Fitting
import numpy as np
import math
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Curve Fit Function linear polynomial
def func_lin(t,a,b):
return a*t + b
# Curve Fit Function cubic polynomial
def func_cub(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()
# Linear Fit
popt, pcov = curve_fit(func_lin, temperature, cp)
fit_cp = func_lin(np.array(temperature), *popt)
plt.figure(1)
plt.plot(temperature, cp, color = "blue", linewidth = 3)
plt.plot(temperature, fit_cp, color = "red", linewidth = 3)
plt.legend(['Actual Data','Curve Fit Data'])
plt.title('Linear Fit', fontsize = 16)
plt.xlabel('Temperature [K]')
plt.ylabel('Specific Heat [kJ/kmol-K]')
plt.show()
# R-square Error
SSR1 = np.sum(pow((fit_cp - np.mean(cp)),2));
SSE1 = np.sum(pow((cp - fit_cp),2));
SST1 = SSR1 + SSE1;
R_sq1 = SSR1 / SST1;
print('R-square value for linear fit is ' + str(R_sq1))
# Cubic Fit
popt, pcov = curve_fit(func_cub, temperature, cp)
fit_cp = func_cub(np.array(temperature), *popt)
plt.figure(2)
plt.plot(temperature, cp, color = "blue", linewidth = 3)
plt.plot(temperature, fit_cp, color = "red", linewidth = 3)
plt.legend(['Actual Data','Curve Fit Data'])
plt.title('Cubic Fit', fontsize = 16)
plt.xlabel('Temperature [K]')
plt.ylabel('Specific Heat [kJ/kmol-K]')
plt.show()
# R-square Error
SSR3 = np.sum(pow((fit_cp - np.mean(cp)),2));
SSE3 = np.sum(pow((cp - fit_cp),2));
SST3 = SSR3 + SSE3;
R_sq3 = SSR3 / SST3;
print('R-square value for cubic fit is ' + str(R_sq3))
GRAPHS:
Linear Fit
Cubic Fit
OUTPUT:
ANSWERS:
popt:- it is an array
Optimal values for the parameters(a, b, c, d for cubic polynomial) so that the sum of the squared residuals of curve fit data(fit_cp) and original data(cp) is minimized.
pcov:- it is a 2-D array
It contains the estimated covariance of popt. The diagonals provide the variance of the parameter estimate.
np.array(temperature):- It creates a 1-D array containing all temperature values.
*popt:-
the * indicates that all the necessary parameters which are the argument of a function are present in the array popt.
A regression curve is said to be good if the least square error is minimum or R-square error value is near to 1.
From our observation cubic fit is better than linear fit.
In order to make curve fit perfect, we need to increase the degree of the polynomial.
REFERENCES:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html
https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860