All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM- To write a PYTHON code that performs curve-fitting on the relationship between cp and Temperature and finding the PERFECT FIT for the following. THEORY- Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of…
Ayush Kumar
updated on 14 Jul 2021
AIM- To write a PYTHON code that performs curve-fitting on the relationship between cp and Temperature and finding the PERFECT FIT for the following.
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.
The accuracy of the curve fit can be improved by increasing the degree of the polynomial.
So, a 'Best fit' can be obtained by increasing the degree of the polynomial.
PERFECT FIT: The perfect fit is achieved when the geometric distances from the given points to the fitting curve are zero . This means that the curve fits perfectly on the points.
To get a perfect fit we can increase the order of the polynomials, this method increases the accuracy and reduces errors.
BEST FIT: The best fit is achieved when the geometric distances from the given points to the fitting curve are minimized, in the least-squares sense. Finding the best fit reduces to the minimization of the objective function where denotes the fitting curve.
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. There are,
The sum of squares due to error (SSE)
R-square
R-squareAdjusted
R-square-root mean squared error (RMSE)
What is the difference between a PERFECT FIT and BEST FIT
Best fit simply means that the differences between the actual measured Y values and the Y values predicted by the model equation are minimized. It does not mean a "perfect" fit; in most cases, a least-squares best fit does not go through all the points in the data set.
PERFECT FIT refers to the goodness of FIT, it is determined by the four methods mentioned above.
PYTHON CODE
#curve_fit using Python
#Importing the modules required
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
def fn_linear(t,a,b): #linear function to provide a linear Fit
return a*t+b
def fn_cubic(t,a,b,c,d): #cubic function to provide a cubic Fit
return a*pow(t,3)+b*pow(t,2)+c*t+d
def read_file(): #reading data file
temperature=[]
cp=[]
for line in open('data','r'): #(data,r)-->r here stands for read
values = line.split(',') #splitting the data into two
temperature.append(float(values[0])) # First value is temperature
cp.append(float(values[1])) #Second value is cp
return [temperature,cp] #The function will return a array of temperature and cp
temperature,cp = read_file() #Read file function will read the data file
popt, pcov =curve_fit (fn_linear,temperature,cp) #Curve Fit for linear
fit_cp_linear = fn_linear(np.array(temperature),*popt)
popt, pcov =curve_fit (fn_cubic,temperature,cp) #Curve Fit for cubic
fit_cp_cubic = fn_cubic(np.array(temperature),*popt)
plt.plot (temperature,cp,'r') #Plot for Linear
plt.plot (temperature,fit_cp_linear,'b')
plt.show()
plt.plot (temperature,cp,'r') #Plot for Cubic
plt.plot (temperature,fit_cp_cubic,'b')
plt.show()
#Emperical Calculation of R^2,SSE ,RMSE
MEAN = np.mean(cp)
ERROR1 = np.array (cp) - np.array (fit_cp_linear) #Calculation for linear
ERROR_SQ1=np.square(ERROR1)
SSE1 = sum (ERROR_SQ1)
SSR1 = [x - MEAN for x in fit_cp_linear]
SSR_SQ1 =np.square(SSR1)
SSRR1 = sum (SSR_SQ1)
R_SQ1 = 100*SSRR1/(SSRR1+SSE1)
RMSE1 = pow((SSE1/3200),0.5)
print (R_SQ1,'...R_SQ linear')
print (RMSE1,'...RMSE linear')
print ('------------------------------------')
ERROR2 = np.array (cp) - np.array (fit_cp_cubic) #calculation for cubic
ERROR_SQ2=np.square(ERROR2)
SSE2 = sum (ERROR_SQ2)
SSR2 = [x - MEAN for x in fit_cp_cubic]
SSR_SQ2 =np.square(SSR2)
SSRR2 = sum (SSR_SQ2)
R_SQ2 = 100*SSRR2/(SSRR2+SSE2)
RMSE2 = pow((SSE2/3200),0.5)
print (R_SQ2,'...R_SQ cubic')
print (RMSE2,'...RMSE cubic')
print ('------------------------------------')
RESULTS
-->Linear
--->Cubic
-->Emperical Calculation
Observations
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...
Week 6 - Data analysis
AIM :TO WRITE A PYTHON PROGRAM WHICH READS THE ENGINE OUTPUT PARAMETER DATA FILE AND PERFORM THE REQUIRED OBJECTIVES The programming language used is Python 2.OBJECTIVE : To read the data from a given data file. To take the file name as an input from the user and check whether the file is present or not. To plot a graph…
16 Aug 2021 10:01 AM IST
Week 3 - Solving second order ODEs
Aim: To write a program to simulate the transient behavior of a simple pendulum.(Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0) To create an animation of its motion. Theory: A pendulum is a weight suspended from a pivot so that it can swing freely. When…
14 Jul 2021 08:05 AM IST
Week 5 - Curve fitting
AIM- To write a PYTHON code that performs curve-fitting on the relationship between cp and Temperature and finding the PERFECT FIT for the following. THEORY- Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of…
14 Jul 2021 06:10 AM IST
Week 2 Air standard Cycle
AIM:To generate the PV diagram and to determine the thermal efficiency of the engine. The programming language used is Python THEORY: Otto Cycle is an idealized thermodynamic cycle that describes the working of a spark-ignition engine. The cycle consists of 4 processes as illustrated in the figure below: it consists…
12 Jul 2021 09:38 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.