All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
CURVE FITTING – Python OBJECTIVE How to change the experimental data into a mathematical equation. Ways to measure the goodness of fit To fit Cp data according to the given Cp vs temperature data file THEORY Curve Fitting It is one of the techniques of data analysis to validate and find the mathematical relation…
Vidu Bansal
updated on 25 Dec 2022
CURVE FITTING – Python
OBJECTIVE
THEORY
Curve Fitting
It is one of the techniques of data analysis to validate and find the mathematical relation between the experimental data (x and y). The curve fitting process is used to relate the x and y data by assuming the polynomial equation.
Any experimental data includes,
Example:
Considering a linear polynomial, y = ax + b
Where,
y – Specific heat (‘dependent variable’ since it depends on temperature)
x – Temperature (‘independent variable’)
a & b – Coefficients
Cubic Polynomial Fitting
Curve fitting is done by using a cubic polynomial equation.
Cp = aT3 + bT2 + cT + d
It forms the cubic fitting. It is suitable for non-linear data.
CODE EXPLANATION
Loading and extracting the data
The data extraction is done using the ‘open’ command which gets the lines in read mode. The values extracted are stored in the arrays as ‘temperature’ and ‘cp’.
Calculation of coefficients
‘curve_fit’ – It is an inbuilt command which helps to find the coefficients of the polynomial equation for the given degree. If the degree of the polynomial is ‘n’, then the ‘curve_fit’ command will return ‘n+1’ coefficients.
Output from the Main code statement
‘popt’ – The coefficients are stored in the ‘popt’.
‘pcov’ – It is a statistical term which contains a covariance matrix.
Finding the predicted values
New predicted cp is further calculated by putting the values of coefficients, and temperature in the polynomial equation.
Here ‘func’ can be linear or cubic for the type of polynomial equation we are examining.
np.array(temperature) depicts all the values of the temperature which were extracted from the data file are passed in the function.
*popt passes all the coefficients of the polynomial equation in the function.
CODE
# CURVE FITTING
# importing useful modules
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# 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]
# Curve fit function (Linear Polynomial)
def linear(t, a, b):
return a*t + b
# Main Program
# function statement to call and read the data file
temperature, cp = read_file()
# function to curve fit data
popt, pcov = curve_fit(linear, temperature, cp)
predicted_cp = linear(np.array(temperature), *popt)
# Plot
plt.figure(1)
plt.plot(temperature, cp, color = "blue", linewidth = 3)
plt.plot(temperature, predicted_cp, color="red", linewidth = 3)
plt.legend(['Actual data', 'Linear Curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.grid()
plt.show()
# Curve fit function (Cubic Polynomial)
def cubic(t, a ,b, c, d):
return a*pow(t,3) + b*pow(t,2) + c*t + d
# Main Program
# function statement to call and read the data file
temperature, cp = read_file()
# function to curve fit data
popt, pcov = curve_fit(cubic, temperature, cp)
predicted_cp = cubic(np.array(temperature), *popt)
# Plot
plt.figure(2)
plt.plot(temperature, cp, color = "blue", linewidth = 3)
plt.plot(temperature, predicted_cp, color="red", linewidth = 3)
plt.legend(['Actual data', 'Cubic Curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.grid()
plt.show()
OUTPUT
Linear Curve Fit
We can notice that the linear curve fit does not fit in the data provided. So this method can here be regarded as a bad fit.
Cubic Curve Fit
The cubic polynomial approach fits much better with the actual data curve. This method is a good fit.
GOODNESS OF FIT
It is a measure of the fitness of the curve for the given data and there are four different methods to calculate the goodness of fit as shown,
Among the four methods of fitness calculation, the R_square value is the best method of finding the goodness of fit. Since the R_square value is an absolute quantity (ranges between 0 to 1), the remaining SSE and RMSE are relative quantities.
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
Data Analysis OBJECTIVE Data Visualizer Compatibility check Basic performance calculation THEORY Data analysis Data analysis is a process of inspecting, cleansing, transforming and modelling data with the goal of discovering useful information, informing conclusions, and supporting decision-making. This deals with the…
29 Dec 2022 05:23 PM IST
Week 2 Air standard Cycle
Otto cycle - Python OBJECTIVE Introduction to IC engine and air standard cycle Plotting PV graph of Otto cycle using Python THEORY IC Engine It is a heat engine that converts the chemical energy of fuel into mechanical energy. The chemical energy of the fuel gets converted to thermal energy through the combustion of an…
26 Dec 2022 06:52 PM IST
Week 5 - Curve fitting
CURVE FITTING – Python OBJECTIVE How to change the experimental data into a mathematical equation. Ways to measure the goodness of fit To fit Cp data according to the given Cp vs temperature data file THEORY Curve Fitting It is one of the techniques of data analysis to validate and find the mathematical relation…
25 Dec 2022 04:57 PM IST
Week 3 - Solving second order ODEs
Solving Second Order ODE Using PYTHON OBJECTIVE To create a simulation of simple pendulum with python as a solution for second-order ODE with damping. THEORY ODE is used to describe the transient behavior of a system. Example; PENDULUM The path of pendulum depends on the Newton’s second law. ODE representing the…
25 Dec 2022 07:30 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.