All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: TO PERFORM CURVE FITTING FOR THE GIVEN TEMPERATURE AND CP DATA IN PYTHON THEORY: 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. Curve fitting can involve either interpolation, where an exact fit…
Sidharth Kalra
updated on 09 Jul 2020
AIM: TO PERFORM CURVE FITTING FOR THE GIVEN TEMPERATURE AND CP DATA IN PYTHON
THEORY: 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. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data. It examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a "best fit" model of the relationship.
How to choose the best fit?
Upon finding a curve that satisfies the data points there are 4 quantities that help us measure the goodness of fit criteria or how well the equation is representing the data points. They are listed as: -
How to make a curve fit perfectly?
In order to have a curve to fit perfectly over the data points provided, the error should be negligible, i.e. the values of R-Square should be exactly ‘1’. Generally, the minimal error is taken into account for best fit but to obtain a perfect fit, all the errors including centering and scaling errors should be nulled.
How to get the best fit?
A line of best fit can be roughly determined using an eyeball method by drawing a straight line on a scatter plot so that the number of points above the line and below the line is about equal (and the line passes through as many points as possible).
Also, the Split Polynomial technique can be opted to obtain a curve fit closest to the data set. This is one of the most viable techniques to be performed for the best fit.
What could be done to improve the cubic fit?
GOVERNING EQUATIONS:
SSE=∑ni=1(Y(i)−f(x(i)))2
R2=SSRSST
where,
SSR=∑ni=1(f(x(i))−Mean)2
SST=SSR+SSE
R2adj=1−[(1−R2)n−1n−k−1]
where,
n = no. of datapoints
k = no. of independent variables
RMSE=√(SSEn)
OBJECTIVES:
INPUTS:
Data file (Temperature v/s CP): https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view
QUESTIONS & ANSWERS:
Q. What do 'popt' and 'pcov' mean?
A. 'popt': - It is an array of the coefficients of a polynomial specified at the beginning of the program.
'pcov': - It is a square matrix that contains the true variance and covariance of the parameters.
Q. What does np.array(temperature) do?
A. Creates an array of temperatures from the values stored in a data file using numpy module.
Q. What does the * in *popt mean?
A. It helps to refer to the coefficients of the polynomial into consideration.
Q. What needs to be done in order to make the curve fit perfect?
A. To make the curve fit perfect, the value of RMSE should be minimum and the value of R2 should be close to 1. It can be done by increasing the order of polynomial that we are trying to curve fit.
PYTHON PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Curve fit function
def func_1(t, a, b):
return a*t + b
def func_2(t, a, b, c):
return a*pow(t,2) + b*t + c
def func_3(t, a, b, c, d):
return a*pow(t,3) + b*pow(t,2) + c*t + d
def func_4(t, a, b, c, d, e):
return a*pow(t,4) + b*pow(t,3) + c*pow(t,2) + d*t + e
# 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 Curve Fitting
popt, pcov = curve_fit(func_1, temperature, cp)
fit_cp_1 = func_1(np.array(temperature), *popt)
plt.figure(1)
plt.plot(temperature, cp, 'k-.', linewidth=2)
plt.plot(temperature, fit_cp_1, color='red', linewidth=3)
plt.legend(['Actual data', 'Curve fit'])
plt.title('LINEAR CURVE FITTING')
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
# Quadratic Curve Fitting
popt, pcov = curve_fit(func_2, temperature, cp)
fit_cp_2 = func_2(np.array(temperature), *popt)
plt.figure(2)
plt.plot(temperature, cp, 'k-.', linewidth=2)
plt.plot(temperature, fit_cp_2, color='red', linewidth=3)
plt.legend(['Actual data', 'Curve fit'])
plt.title('QUADRATIC CURVE FITTING')
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
# Cubic Curve Fitting
popt, pcov = curve_fit(func_3, temperature, cp)
fit_cp_3 = func_3(np.array(temperature), *popt)
plt.figure(3)
plt.plot(temperature, cp, 'k-.', linewidth=2)
plt.plot(temperature, fit_cp_3, color='red', linewidth=3)
plt.legend(['Actual data', 'Curve fit'])
plt.title('CUBIC CURVE FITTING')
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
# Biquadratic Curve Fitting
popt, pcov = curve_fit(func_4, temperature, cp)
fit_cp_4 = func_4(np.array(temperature), *popt)
plt.figure(1)
plt.plot(temperature, cp, 'k-.', linewidth=2)
plt.plot(temperature, fit_cp_4, color='red', linewidth=3)
plt.legend(['Actual data', 'Curve fit'])
plt.title('BIQUADRATIC CURVE FITTING')
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
# Fitness Characteristics Calculation
s = np.sum(cp)
n = np.size(cp)
# Mean
m = s/n
# For Linear Curve Fit
SSR_lin = 0
SSE_lin = 0
for i in range(n):
error = abs((np.sum((cp[i] - fit_cp_1[i]))))
SSE_lin = SSE_lin + pow(error,2)
SSR_lin = SSR_lin + (np.sum(pow((fit_cp_1[i] - m),2)))
SST_lin = SSE_lin + SSR_lin
Rsq_lin = SSR_lin/SST_lin
RMSE_lin = pow((SSE_lin/n),0.5)
print('SST_lin:', SST_lin)
print('Rsq_lin:', Rsq_lin)
print('RMSE_lin:', RMSE_lin)
# For Quadratic Curve Fit
SSR_quad = 0
SSE_quad = 0
for i in range(n):
error = abs((np.sum((cp[i] - fit_cp_2[i]))))
SSE_quad = SSE_quad + pow(error,2)
SSR_quad = SSR_quad + (np.sum(pow((fit_cp_2[i] - m),2)))
SST_quad = SSE_quad + SSR_quad
Rsq_quad = SSR_quad/SST_quad
RMSE_quad = pow((SSE_quad/n),0.5)
print('SST_quad:', SST_quad)
print('Rsq_quad:', Rsq_quad)
print('RMSE_quad:', RMSE_quad)
# For Cubic Curve Fit
SSR_cub = 0
SSE_cub = 0
for i in range(n):
error = abs((np.sum((cp[i] - fit_cp_3[i]))))
SSE_cub = SSE_cub + pow(error,2)
SSR_cub = SSR_cub + (np.sum(pow((fit_cp_3[i] - m),2)))
SST_cub = SSE_cub + SSR_cub
Rsq_cub = SSR_cub/SST_cub
RMSE_cub = pow((SSE_cub/n),0.5)
print('SST_cub:', SST_cub)
print('Rsq_cub:', Rsq_cub)
print('RMSE_cub:', RMSE_cub)
# For Biquadratic Curve Fit
SSR_bquad = 0
SSE_bquad = 0
for i in range(n):
error = abs((np.sum((cp[i] - fit_cp_4[i]))))
SSE_bquad = SSE_bquad + pow(error,2)
SSR_bquad = SSR_bquad + (np.sum(pow((fit_cp_4[i] - m),2)))
SST_bquad = SSE_bquad + SSR_bquad
Rsq_bquad = SSR_bquad/SST_bquad
RMSE_bquad = pow((SSE_bquad/n),0.5)
print('SST_bquad:', SST_bquad)
print('Rsq_bquad:', Rsq_bquad)
print('RMSE_bquad:', RMSE_bquad)
EXPLANATION OF PROGRAM:
RESULTS:
The values of SST, RMSE & R2 are mentioned in the command window.
PLOTS:
The Linear fit doesn't satisfy the purpose and higher-order polynomial should be considered.
The blueprint comes out to be satisfactory but the accuracy of the fit is highly poor.
The above fit shows high accuracy but for a perfect fit, the order of the polynomial should be increased.
The results are highly accurate and the value of RMSE is least for Biquadratic Curve Fit. Also, the value of R2 is closest to 1.
CONCLUSION: Curve Fitting of the given data is done by suitable methods and techniques in order to produce the best fit. The goodness of fit measure is satisfied by calculating variables of interest. The Linear Fit Curve doesn’t satisfy the criterion whereas Cubic Fit Curve satisfies it.
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
DATA ANALYSIS (PYTHON)
AIM: DATA VISUALIZATION, COMPATIBILITY CHECK, AND BASIC PERFORMANCE CALCULATION USING PYTHON. INTRODUCTION: Data analysis is a process of inspecting, cleansing, transforming, and modeling data with the goal of discovering useful information, informing conclusions, and supporting decision-making. Data analysis has…
12 Jul 2020 03:37 PM IST
CURVE FITTING (PYTHON)
AIM: TO PERFORM CURVE FITTING FOR THE GIVEN TEMPERATURE AND CP DATA IN PYTHON THEORY: 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. Curve fitting can involve either interpolation, where an exact fit…
09 Jul 2020 08:37 PM IST
BREAKING ICE WITH AIR CUSHIONED VEHICLE - FINDING MINIMUM PRESSURE WITH NEWTON-RAPHSON METHOD
AIM: TO CALCULATE THE MINIMUM PRESSURE OF AN ICE BREAKING AIR CUSHION VEHICLE USING NEWTON-RAPHSON METHOD THEORY: A hovercraft, also known as an air-cushion vehicle or ACV, is an amphibious craft capable of traveling over land, water, mud, ice, and other surfaces. Hovercraft use blowers to produce a large volume…
09 Jul 2020 03:04 PM IST
SOLVING SECOND ORDER ODE'S
AIM: TO SIMULATE THE TRANSIENT BEHAVIOUR OF A SIMPLE PENDULUM AND TO CREATE AN ANIMATION OF IT’S MOTION USING PYTHON THEORY: In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum. The way the pendulum moves depends on Newton's second law. When this law is…
07 Jul 2020 09:03 PM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.