All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM : To plot a graph between temperature and cp(coefficients of pressure) from the provided thermal data file.And also to find the solution for the thermal data using curve fitting in python. OBJECTIVE : 1.To calculate which curve fits better for the given thermal data. 2.To calculate the goodness criteria of curve…
Praveen kumar
updated on 14 Jun 2021
AIM :
To plot a graph between temperature and cp(coefficients of pressure) from the provided thermal data file.And also to find the solution for the thermal data using curve fitting in python.
OBJECTIVE :
1.To calculate which curve fits better for the given thermal data.
2.To calculate the goodness criteria of curve fit values to find whether the curve fit is good or bad.
THEORY :
Curve fitting is the way we model or represent a data spread by assigning a 'best fit' function (curve) along the entire range. Ideally, it will capture the trend in the data and allow us to make predictions of how the data series will behave in the future.
Types of curve fitting include:
1.Interpolation
2.Smoothing
LINEAR CURVE FITTING AND POLYNOMIAL CURVE FITTING:
Linear curve fitting, or linear regression, is when the data is fit to a straight line. Although there might be some curve to your data, a straight line provides a reasonable enough fit to make predictions. Since the equation of a generic straight line is always given by f(x)= a x + b, the question becomes: what a and b will give us the best fit line for our data?
Considering the vertical distance from each point to a prospective line as an error, and summing them up over our range, gives us a concrete number that expresses how far from ‘best’ the prospective line is.
A line that provides a minimum error can be considered the best straight line.
Since it’s the distance from our points to the line we’re interested in—whether it is positive or negative distance is not relevant—we square the distance in our error calculations. This also allows us to weight greater errors more heavily. So this method is called the least square approach.
Polynomial curve fitting is when we fit our data to the graph of a polynomial function. The same least squares method can be used to find the polynomial, of a given degree, that has a minimum total error.
How to choose a best curve fit
After fitting data with one or more models, you should evaluate the goodness of fit. A visual examination of the fitted curve displayed in the Curve Fitting Tool should be your first step. Beyond that, the toolbox provides these goodness of fit measures for both linear and nonlinear parametric fits:
1.Residuals
2.Goodness of fit
3.Confidence and prediction bounds
Residuals
The residuals from a fitted model are defined as the differences between the response data and the fit to the response data at each predictor value.
Goodness of Fit Statistics
After using graphical methods to evaluate the goodness of fit, you should examine the goodness of fit statistics. The Curve Fitting Toolbox supports these goodness of fit statistics for parametric models:
EQUATIONS :
sum of squares due to error(SSE) = ∑ni=1(y(i)−f(x(i)))2
sum of the squared residuals(SSR) = ∑ni=1(f(x(i))−Mean)2
sum of the squared total(SST) = SSR + SSE
R_square(R2) = SSRSST
Root mean squared error (RMSE) = √SSEn
1. What does popt and pcov mean?
--> popt is the arguments/co-efficients of the linear equation or quadratic equation or the best fit parameters.
pcov variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters. This is mostly useful when the data has uncertainties.
2. What does np.array(temperature) do?
-->It stores all the values of temperature from the data file into the array using numpy module.
3. What does the * in *popt mean?
--> *popt passes all the coefficient values of an equation like a,b,c,d.
4. What needs to be done in order to make the curve fit perfect?
-->We get perfect curve fit when the value of SSE is low and value of R^2 is equal to 1.When we go on increase the order of equation then we get perfect fit.
PROCEDURE:
1.Import the required libraries.
2.Define the function for linear,quadratic and cubic with specific return values.
3.Now write a logic to read the data file and initiate the for loop to store the values of temperature and pressure.
4.Write the logic for curve fitting.
5.Plot the graphs.
6.Now initiate the for loop to calculate the goodness criteria values which shows the curve fit is perfect or bad.
PYTHON CODE:
#IMPORTING REQUIRES LIBRARIES
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
#FUNCTION DEFINATION
def linear(t,a,b):
return a*t + b
def quadratic(t,a,b,c):
return a*pow(t,2) + b*t + c
def cubic(t,a,b,c,d):
return a*pow(t,3) + b*pow(t,2) + c*t + d
def polynomial(t,a,b,c,d,e):
return a*pow(t,4) + b*pow(t,3) + c*pow(t,2) + d*t + e
#READING THE NOTEPAD DATA
def readfile():
temperature = []
cp = []
TEMPERATURE = []
#TO READ AND STORED EACH AND EVERY LINE IN THE DATA FILE
for i in open('data','r'):
values = i.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
temperature,cp = readfile()
#LINEAR CURVE FIT
popt, pcov = curve_fit(linear,temperature,cp)
#STORING THE COEFFICIENTS PARAMETERS OF THE EQUATION
fit_cp_1 = linear(np.array(temperature), *popt)
#PRINTING THE COEFFICIENT PARAMETERS
print("[a b] = ",popt)
#PLOTTING THE GRAPHS
plt.figure()
ax=plt.axes()
ax.set_facecolor('lightgrey')
plt.figure(1)
plt.plot(temperature,cp,'k--',label = "Actual data")
plt.plot(temperature,fit_cp_1,linewidth=3,color='red',label = 'Curve fit')
plt.title("LINEAR CURVE FITTING")
plt.legend(loc='best',edgecolor='black',framealpha=0.7)
plt.show()
#QUADRATIC CURVE FIT
popt, pcov = curve_fit(quadratic,temperature,cp)
fit_cp_2 = quadratic(np.array(temperature), *popt)
print("[a b c] = ",popt)
plt.figure()
ax=plt.axes()
ax.set_facecolor('lightgrey')
plt.figure(1)
plt.plot(temperature,cp,'k--',label = "Actual data")
plt.plot(temperature,fit_cp_2,linewidth=3,color='red',label = 'Curve fit')
plt.title("QUADRATIC CURVE FITTING")
plt.legend(loc='best',edgecolor='black',framealpha=0.7)
plt.show()
#CUBIC CURVE FIT
popt, pcov = curve_fit(cubic,temperature,cp)
fit_cp_3 = cubic(np.array(temperature), *popt)
print("[a b c d] = ",popt)
plt.figure()
ax=plt.axes()
ax.set_facecolor('lightgrey')
plt.figure(1)
plt.plot(temperature,cp,'k--',label = "Actual data")
plt.plot(temperature,fit_cp_3,linewidth=3,color='red',label = 'Curve fit')
plt.title("CUBIC CURVE FITTING")
plt.legend(loc='best',edgecolor='black',framealpha=0.7)
plt.show()
#POLYNOMIAL CURVE FIT
popt, pcov = curve_fit(polynomial,temperature,cp)
fit_cp_4 = polynomial(np.array(temperature), *popt)
print("[a b c d e] = ",popt)
plt.figure()
ax=plt.axes()
ax.set_facecolor('lightgrey')
plt.figure(1)
plt.plot(temperature,cp,'k--',label = "Actual data",linewidth=4)
plt.plot(temperature,fit_cp_4,linewidth=3,color='red',label = 'Curve fit')
plt.title("POLYNOMIAL CURVE FITTING")
plt.legend(loc='best',edgecolor='black',framealpha=0.7)
plt.show()
#CALCULATIONS TO KNOW WHETHER THE CURVE FIT IS GOOD OR BAD
s = np.sum(cp)
n = np.size(cp)
m = s/n
SSE_LINEAR = 0
SSR_LINEAR = 0
for i in range(n):
error = abs((np.sum((cp[i]-fit_cp_1[i]))))
SSE_LINEAR = SSE_LINEAR + pow(error,2)
SSR_LINEAR = SSR_LINEAR + (np.sum(pow((fit_cp_1[i]-m),2)))
SST = SSR_LINEAR + SSE_LINEAR
RSME = pow((SSE_LINEAR/n),0.5)
Rsq_LINEAR = SSR_LINEAR/SST
print("SST:",SST)
print("RSME:",RSME)
print("Rsq_LINEAR:",Rsq_LINEAR)
#CALCULATIONS TO KNOW WHETHER THE CURVE FIT IS GOOD OR BAD
s = np.sum(cp)
n = np.size(cp)
m = s/n
SSE_QUAD = 0
SSR_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 = SSR_QUAD + SSE_QUAD
RSME = pow((SSE_QUAD/n),0.5)
Rsq_QUAD = SSR_QUAD/SST
print("THE VALUES FOR QUADRATIC CURVE FIT ARE")
print("SST:",SST)
print("RSME:",RSME)
print("Rsq_QUAD:",Rsq_QUAD)
#CALCULATIONS TO KNOW WHETHER THE CURVE FIT IS GOOD OR BAD
s = np.sum(cp)
n = np.size(cp)
m = s/n
SSE_CUBIC = 0
SSR_CUBIC = 0
for i in range(n):
error = abs((np.sum((cp[i]-fit_cp_3[i]))))
SSE_CUBIC = SSE_CUBIC + pow(error,2)
SSR_CUBIC = SSR_CUBIC + (np.sum(pow((fit_cp_3[i]-m),2)))
SST = SSR_CUBIC + SSE_CUBIC
RSME = pow((SSE_CUBIC/n),0.5)
Rsq_CUBIC = SSR_CUBIC/SST
print("THE VALUES FOR CUBIC CURVE FIT ARE")
print("SST:",SST)
print("RSME:",RSME)
print("Rsq_QUAD:",Rsq_CUBIC)
#CALCULATIONS TO KNOW WHETHER THE CURVE FIT IS GOOD OR BAD
s = np.sum(cp)
n = np.size(cp)
m = s/n
SSE_POLYNOMIAL = 0
SSR_POLYNOMIAL = 0
for i in range(n):
error = abs((np.sum((cp[i]-fit_cp_4[i]))))
SSE_POLYNOMIAL = SSE_POLYNOMIAL + pow(error,2)
SSR_POLYNOMIAL = SSR_POLYNOMIAL + (np.sum(pow((fit_cp_4[i]-m),2)))
SST = SSR_POLYNOMIAL + SSE_POLYNOMIAL
RSME = pow((SSE_POLYNOMIAL/n),0.5)
Rsq_POLYNOMIAL = SSR_POLYNOMIAL/SST
print("THE VALUES FOR POLYNOMIAL CURVE FIT ARE")
print("SST:",SST)
print("RSME:",RSME)
print("Rsq_POLYNOMIAL:",Rsq_QUAD)
OUTPUT :
[a b] = [9.87543761e-02 9.95201748e+02]
[a b c] = [-2.95061706e-05 2.10848318e-01 9.13919123e+02]
[a b c d] = [ 1.90608268e-08 -1.38124292e-04 3.87891017e-01 8.38896565e+02]
[a b c d e] = [-1.02120104e-11 9.66516821e-08 -3.36791977e-04 5.82718384e-01
7.81066721e+02]
SST: 28793673.249584787
RSME: 25.999093068195325
Rsq_LINEAR: 0.9248776260524184
THE VALUES FOR QUADRATIC CURVE FIT ARE
SST: 28793673.250569038
RSME: 12.991674772728752
Rsq_QUAD: 0.9812421444745516
THE VALUES FOR CUBIC CURVE FIT ARE
SST: 28793673.256229643
RSME: 5.42770728878495
Rsq_QUAD: 0.996725946714689
THE VALUES FOR POLYNOMIAL CURVE FIT ARE
SST: 28793673.25249702
RSME: 1.8599626013389154
Rsq_POLYNOMIAL: 0.9812421444745516
PLOTS:
CONCLUSION :
Curve fit for the gives data has been produced and to know that the produced fit is perfect or not the values of goodness criterion has been obtained using python.
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...
Project 2 - Supply and Demand Gap Analysis
Aim: The aim of this analysis is to identify the supply and demand gap in the Uber network and provide recommendations to improve the efficiency of their service. Introduction: Uber is a ride-hailing platform that connects passengers with drivers through a mobile app. The company has revolutionized the transportation industry…
04 Apr 2023 05:08 AM IST
Project 1 - COVID-19 Vaccinations Trend Analysis
Aim The aim of this report is to analyze the vaccination data for various countries and vaccine types and provide insights and recommendations for improving vaccination efforts. Introduction Vaccination is an important tool in preventing the spread of infectious diseases and protecting public health. With the ongoing COVID-19…
31 Mar 2023 08:30 AM IST
Project 2 - Create a report using PowerQuery, Macro VBA, List Functions and Data Validation functions for Data Reporting of Supply Chain Management companies
Link to the Excel File is below: https://drive.google.com/file/d/1cQHJ0eiA47Tf9xFVaur1z6_5h0RdfVki/view?usp=share_link Project Report: Analysis of Loan Applicant Data at IJS Bank Limited Introduction: The purpose of this project is to analyze the data of loan applicants at IJS Bank Limited for the current…
13 Feb 2023 07:49 AM IST
Project 1 - Data Cleaning and Transformation using Pivot table and Charts for the Report on requirement of Team Hiring
PLEASE CHECK THE EXCEL FILE ATTACHED BELOW I HAVE ANSWERED ALL THE QUESTIONS. b. d.
04 Feb 2023 10:07 AM 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.