Week 5 - Curve fitting
AIM: Write a program in python to perform curve fitting and determine the fitness of the curve.
OBJECTIVES:
- To write codes to fit a linear and cubic polynomial for the Cp data.
- To plot the linear and cubic fit curves along with the raw data points.
- To measure the fitness characteristics for both curves.
THEORY:
Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to a series of data points.
In regression analysis, curve fitting is the process of specifying the model that provides the best fit to the specific curves in your dataset. Curved relationships between variables are not as straightforward to fit and interpret as linear relationships.
For linear relationships, as you increase the independent variable by one unit, the mean of the dependent variable always changes by a specific amount. This relationship holds true regardless of where you are in the observation space.

Interpolation is a method of estimating values between known data points.
Smoothing is a method of reducing the noise within a data set.
Linear Curve Fit is a linear approach to modeling the relationship between a scalar response (or dependent variable) and one or more explanatory variables (or independent variables). Although there might be some curve to your data, a straight line provides a reasonable enough fit to make predictions.
Nonlinear regression is a form of regression analysis in which observational data are modeled by a function which is a nonlinear combination of the model parameters and depends on one or more independent variables. The data are fitted by a method of successive approximations.
The goodness of fit of a statistical model describes how well it fits a set of observations. Measures of goodness of fit typically summarize the discrepancy between observed values and the values expected under the model in question.
To choose the best fit for the curve the following four parameters help us to measure the goodness of fit criteria or how well the equations are representing the given data points:
- The sum of squares due to error or simply sum of square error (SSE),
- R-Square (R^2),
- Adjusted R-Square,
- Root mean squared error (RMSE)
Overfitting is "the production of an analysis that corresponds too closely or exactly to a particular set of data, and may therefore fail to fit additional data or predict future observations reliably".
GOVERNING EQUATIONS :
for first-order polynomial
cp=a*T+b
for second-order polynomial
cp=a*T^2+b*T+c
for cubical polynomial
cp=a*T^3+b*T^2+c*T+d ,
#SSE = sum of squares due to error
#RMSE = Root mean square error
INPUTS: Cp vs Temperature data file
CODE:
# importing modules
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Linear Curve fit function
def function_linear_curve(t, a, b):
return a * t + b
# Cubic Curve fit function
def function_cubic_curve(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]
# read file data by calling function
temperature, cp = read_file()
# calculation of coefficient for linear curve
popt, pcov = curve_fit(function_linear_curve, temperature, cp)
fit_cp_l = function_linear_curve(np.array(temperature), *popt)
# plotting data for linear curve
plt.figure(1)
plt.plot(temperature, cp, 'b--')
plt.plot(temperature, fit_cp_l, color='red')
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel('Temperature (K)')
plt.ylabel('Cp')
plt.title('Linear Curve fitting')
plt.show()
# calculation of coefficient for cubic curve
popt, pcov = curve_fit(function_cubic_curve, temperature, cp)
fit_cp_c = function_cubic_curve(np.array(temperature), *popt)
# plotting data for cubic curve
plt.figure(2)
plt.plot(temperature, cp, 'b--')
plt.plot(temperature, fit_cp_c, color='red')
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel('Temperature (K)')
plt.ylabel('Cp')
plt.title('Cubic Curve fitting')
plt.show()
#Measuring the fitness characetristics of the curves
s = np.sum(cp)
l = np.size(cp)
# Finding mean of Cp data
m = s / l
print('Mean of all cp :', m,'\n')
# For Linear curve fit
linear_sse = 0
linear_ssr = 0
for i in range(l):
linear_error = abs((np.sum((cp[i] - fit_cp_l[i]))))
linear_sse = linear_sse + pow(linear_error, 2)
linear_ssr = linear_ssr + np.sum(pow((fit_cp_l[i] - m), 2))
print('For Linear Curve Fit')
linear_sst = linear_sse + linear_ssr
print('Linear SST :', linear_sst)
linear_R2 = linear_ssr / linear_sst
print('Linear R2 :', linear_R2)
linear_rmse = pow((linear_sse / l), 0.5)
print('Linear RMSE :', linear_rmse ,'\n')
# For Cubic curve fit
cubic_sse = 0
cubic_ssr = 0
for j in range(l):
cubic_error = abs(np.sum((cp[i] - fit_cp_c[i])))
cubic_sse = cubic_sse + (pow(cubic_error, 2))
cubic_ssr = cubic_ssr + np.sum(pow((fit_cp_c[i] - m), 2))
print('For Cubic Curve Fit')
cubic_sst = cubic_sse + cubic_ssr
print('Cubic SST :', cubic_sst)
cubic_R2 = cubic_ssr / cubic_sst
print('Cubic R2 :', cubic_R2)
cubic_rmse = pow((cubic_sse / l), 0.5)
print('Cubic RMSE :', cubic_rmse)
Conclusion :
Linear Curve Fit Plot

Cubic Curve Fit Plot

fit for linear and cubic curve

QUESTION-ANSWERS:
Q1. What does popt and pcov mean?
Ans. ‘popt’ : an array that stores the values of the coefficients that are being passed in a given function.
‘pcov’ : a two-dimensional array that stores the values of the estimated covariance of ‘popt’ i.e the coefficients.
Q2. What does np.array(temperature) do?
Ans. This command converts each value of temperature in the data file into an array using the NumPy module.
Q3. What does the * in *popt mean?
Ans. '*' in ‘*popt' indicates and returns each coefficient stored in popt array.
Q4 .What needs to be done in order to make the curve fit perfect?
Ans . In order to make the curve fit perfect we can increase the order of the polynomial or break the data set into different groups of certain range or both.
Ref:
Linear Regression: SST, SSR, SSE, R-squared and Standard Errors with Excel ANOVA (factorpad.com)