All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Curve fit :- Curve fitting is the process of constructing a curve, or mathematical function that has the best fit to a series of data points. The objective of curve fitting is to find the parameters of a mathematical model that describes a set of data in a way that minimizes the difference between the model and the data.…
Chichula Rupesh
updated on 15 May 2020
Curve fit :-
Curve fitting is the process of constructing a curve, or mathematical function that has the best fit to a series of data points. The objective of curve fitting is to find the parameters of a mathematical model that describes a set of data in a way that minimizes the difference between the model and the data. The most common approach is the "linear least squares" method, also called "polynomial least squares", a well-known mathematical procedure for finding the coefficients of polynomial equations that are a "best fit" to a set of X,Y data. A polynomial equation expresses the dependent variable Y as a weighted sum of a series of single-valued functions of the independent variable X, most commonly as a straight line (Y = a + bX, where a is the intercept and b is the slope), or a quadratic (Y = a + bX + cX2), or a cubic (Y = a + bX + cX2 + dX3), or higher-order polynomial. Those coefficients (a, b, c, etc) can be used to predict values of Y for each X. In all these cases, Y is a linear function of the parameters a, b, c, and/or d. This is why we call it a "linear" least-squares fit, not because the plot of X vs Y is linear. Only for the first-order polynomial Y = a + bX is the plot of X vs. Y linear.
What does popt and pcov mean?
eg:- def func1 (t, a, b):
return a*t + b
The curve_fit
function returns two items, which we can be stored in popt
and pcov
. The popt
argument are the best-fit paramters for function inputs (a,b).
The pcov
variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters. This is mostly useful when the data has uncertainties.
coveriance:- The mean value of the product of the deviations of two variates from their respective means.
What does np.array(temperature) do?
A numpy array is a grid of values, all of the same type, and is indexed by a data structure consisting of multiple parts of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a data structure consisting of multiple parts of integers giving the size of the array along each dimension.
thus in the program the np.temperature create the array of values with rank-1 which helps the program to select the particular temperature and calculate the cp value for the cure.
What does the * in *popt mean?
in the program * is pointer which store the address of the variables in an array. *popt gives out the integer value of constants.
A python code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
"""
A python program to obtain the best curve fit for the available
thermodynamic data file
by Rupesh
"""
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
# Curve fit function
def func1 (t, a, b):
return a*t + b
def func2 (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()
popt1,pcov1 = curve_fit (func1, temperature, cp)
popt2,pcov2 = curve_fit (func2, temperature, cp)
fit_cp1 = func1 (np.array (temperature), *popt1)
fit_cp2 = func2 (np.array (temperature), *popt2)
# plotting the actual and estimated curves
plt.figure()
plt.plot(temperature, cp , color = 'blue', lw = 3)
plt.plot(temperature, fit_cp1, color = 'red', lw = 3)
plt.legend(['Actual data', 'linear cure fit'])
plt.title(' Linear curve fit')
plt.xlabel('Temperature[k]')
plt.ylabel('cp')
plt.figure()
plt.plot(temperature, cp , color = 'blue', lw = 3)
plt.plot(temperature, fit_cp2, color = 'green', lw = 3)
plt.legend(['Actual data', 'cubical cure fit'])
plt.title(' Cubical curve fit')
plt.xlabel('Temperature[k]')
plt.ylabel('cp')
plt.show()
the results are represented by the curve as shown below.
if we further increase the power of the polynomial we get the exact curve fit. From the above graph we can say that the cubical curve fit is the best fit.
What needs to be done in order to make the curve fit perfect?
By observation it is noted that the increase in degree of polynomial gives us the prefect curve fit. Thus in the given problem cubic curves can be made as best fits.
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...
Design of automotive Hood/Bonnet using _NX_CAD
Design of Automotive Hood/Bonnet Hood/Bonnet: Hood or Bonnet is a part of the automotive vehicle which gives the aesthetic look to the automobile and also helps as an enclosure to the various engine components and sensors and it is an airtight cover which protect the internal components. The bonnet system is an access…
07 Jul 2022 01:18 PM IST
Section Modulus calculation and optimization
Section Modulus of Car Hood MODULUS OF ELASTICITY: The ratio of stress to strain or the stiffness of the material of a structural member (resistance to deformation). Essentially, the modulus of elasticity is a more general term regarding…
07 Jul 2022 06:23 AM IST
Fender Design Challenge
Design of Car Front fender Fenders/ Quarter Panel:- Fender is a word derived from the American English. Automobile fender is a rigid vehicle panel that houses the wheel well. It forms an arch that is designed to protect the wheel and prevent road debris such as rocks, sand, and mud from being thrown into the air…
23 Nov 2020 01:29 AM IST
Underbody Coating
Body in white (BIW) Body in white (BIW) is the stage in the automobile manufacturing in which a car body's frame has been joined together, that is before painting and before the motor, chassis sub-assemblies, or trim (glass, door locks/handles, seats, upholstery, electronics, etc.) have been integrated into the structure.…
28 Sep 2020 04:00 PM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.