All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Curve fitting: 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…
Saravanan S
updated on 13 Sep 2021
Curve fitting:
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. A related topic is regression analysis, which focuses more on questions of statistical inference such as how much uncertainty is present in a curve that is fit to data observed with random errors. Fitted curves can be used as an aid for data visualization, to infer values of a function where no data are available, and to summarize the relationships among two or more variables. Extrapolation refers to the use of a fitted curve beyond the range of the observed data, and is subject to a degree of uncertainty since it may reflect the method used to construct the curve as much as it reflects the observed data.
The first degree polynomial equation
is a line with slope a. A line will connect any two points, so a first degree polynomial equation is an exact fit through any two points with distinct x coordinates.
If the order of the equation is increased to a second degree polynomial, the following results:
This will exactly fit a simple curve to three points.
If the order of the equation is increased to a third degree polynomial, the following is obtained:
Best fit:
Line of best fit refers to a line through a scatter plot of data points that best expresses the relationship between those points. Statisticians typically use the least squares method to arrive at the geometric equation for the line, either though manual calculations or regression analysis software. A straight line will result from a simple linear regression analysis of two or more independent variables. A regression involving multiple related variables can produce a curved line in some cases.
Program:
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#linear function
def linear_function(t,a,b):
return a*t + b
#cubic function
def cubic_function(t,a,b,c,d):
return a*pow(t,3) + b*pow(t,2) + c*t + d
def file_read():
temperature = []
cp = []
for line in open('data','r'):
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return[temperature,cp]
temperature, cp = file_read()
popt, pcov = curve_fit(linear_function, temperature, cp)
fit_cp_l = linear_function(np.array(temperature), *popt)
plt.figure(1)
plt.plot(temperature,cp,color = 'g')
plt.plot(temperature,fit_cp_l, color = 'b')
plt.legend(['Actual data','curve fit'])
plt.xlabel('Temperature')
plt.ylabel('cp')
plt.title('Linear curve fitting')
popt, pcov = curve_fit(cubic_function, temperature, cp)
fit_cp_c = cubic_function(np.array(temperature), *popt)
plt.figure(2)
plt.plot(temperature,cp,color = 'g')
plt.plot(temperature,fit_cp_c, color = 'b')
plt.legend(['Actual data','curve fit'])
plt.xlabel('Temperature')
plt.ylabel('cp')
plt.title('Cubic curve fitting')
plt.show()
linear fit graph:
Cubic fit graph:
1. What does popt and pcov mean?
popt is a one dimensional array of the best estimates for the parameter values, each entry matches the order in the function definition. pcov is the covariance matrix showing the uncertainty and interdependence of each parameter in popt. We take the diagonal elements as pvar for the variance of each parameter in popt. The above error didn’t consider the errors in the individual data points correctly.
2. What does np.array(temperature) do?
The function of np.array(temperature) is to pass the program as well as the popt. The value of temperature through the function which is defined in the program as well as the popt. The value of temperature are saved in the data file which is copied in the same program folder.
3. What does the * in *popt mean?
The star in *popt unpacks the popt array so the two optimized parameter values become the second and third arguments to the function.
5. What needs to be done in order to make the curve fit perfect?
To make the cubic fit better, you need to split the data into smaller datasets and fit them separately.
6. Show empirically as to how well your curve has been fit.
To make a curve fit perfect the error estimates should be considered. The perfect fit is a fit where it should match the original curve and shouldn't have any scaling and centering errors in that particular degree of the polynomial. Line of best fit refers to a line through a scatter plot of data points that best expresses the relationship between those points. Statisticians typically use the least squares method to arrive at the geometric equation for the line, either though manual calculations or regression analysis software. A straight line will result from a simple linear regression analysis of two or more independent variables. A regression involving multiple related variables can produce a curved line in some cases.
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 - Rankine cycle Simulator
Understand the concept of Rankine cycle by referring to standard thermal engineering text book. Create a Rankine Cycle Simulator using OCTAVE/MATLAB. Your code should calculate the state points of the Rankine Cycle (any type of your choice) based on user inputs. Then, plot the corresponding T-s and h-s plots for…
26 Sep 2021 04:41 PM IST
Week 6 - Data analysis
Take a look at the data analysis video before you attempt this challenge. Your task is to write a script that does the following. Data visualizer (100 points) Your script should take column numbers as the input and plot the respective columns as separate images Each file should be saved by the name of the column The plot…
19 Sep 2021 09:36 AM IST
Week 5 - Curve fitting
Curve fitting: 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…
13 Sep 2021 03:20 PM IST
Week 3 - Solving second order ODEs
Equation: n the above equation, g = gravity in m/s2, L = length of the pendulum in m, m = mass of the ball in kg, b=damping coefficient. Write a program in Python that will simulate the pendulum motion, just like the one shown in the start of this challenge. use, L=1 metre, m=1 kg, b=0.05. g=9.81 m/s2. Simulate the motion…
29 Aug 2021 11:39 AM 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.