All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: The aim of this program is to write a Python code to fit a linear and cubic polynomial for the cp data. It aims to show the split wise method and explain all the parameters involved in the measurement of the fitness characteristics. OBJECTIVES: Know the importance of curve fitting in real life Learn how…
Kpooupadang Jacques ABOUZI
updated on 30 Apr 2022
AIM: The aim of this program is to write a Python code to fit a linear and cubic polynomial for the cp data.
It aims to show the split wise method and explain all the parameters involved in the measurement of the fitness characteristics.
OBJECTIVES:
THEORY:
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.
In order to find a curve which satisfies the data under question, there are four quantities which help us measure the goodness of fit criteria or how well the equation is representing the datapoints.
SSE(SumOfSquaredErrors)=∑ni=1(Y(i)−f(x(i)))2
Where y = f(x) is the fit curve
R2=SSRSST
Where
SSR = Sum of squares of the regression which is basically the square of the difference between the value of the fitting function at a given point and the arithmetic mean of the dataset.
And SST = SSR + SSE
R2adj=[(1−R2)(n−1)n−k−1]
Where
n is the number of datapoints
k is the number of independent variables
4. Root mean squared error (RMSE)
RMSE=√(∑ni=1(Y(i)−f(x(i)))2n)=√(SSEn)RMSE=√(∑ni=1(Y(i)−f(x(i)))2n)=√(SSEn)
where n is the total number of datapoints available.
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Linear Curve fit function
def func(t, a, b):
return a*t + b
#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()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)
plt.plot(temperature, cp, color = "blue", linewidth=3)
plt.plot(temperature, fit_cp, color="red", linewidth=3)
plt.title('Linear Curve fit')
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
#Computing the mean specific heat
sum_1 = 0 # Initialization
for i in range(0,len(cp)):
sum_1 = sum_1 + cp[i]
mean_cp = sum_1/len(cp)
print('The mean specific heat is',mean_cp)
#Computation of parameters such as SSE, SSR, SST and R^2
SSE = 0
SSR = 0
for i in range(0,len(cp)):
SSR = SSR + pow((cp[i]-mean_cp),2)
SSE = SSE + pow((fit_cp[i]-cp[i]),2)
SST = SSR + SSE
R_square = SSR/SST
RMSE = pow(SSE/len(cp),0.5)
print("SSR =",SSR)
print("SSE =",SSE)
print("SST =",SST)
print("R_square =",R_square)
print("RMSE =",RMSE)
OUTPUT:
b. Cubic curve fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Linear Curve fit function
def func(t, a, b, c, d):
return pow(t,3)*a + pow(t,2)*b + t*c + 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()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)
plt.plot(temperature, cp, color = "blue", linewidth=3)
plt.plot(temperature, fit_cp, color="red", linewidth=3)
plt.title('Cubic curve fit')
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
#Computing the mean specific heat
sum_1 = 0 # Initialization
for i in range(0,len(cp)):
sum_1 = sum_1 + cp[i]
mean_cp = sum_1/len(cp)
print('The mean specific heat is',mean_cp)
#Computation of parameters such as SSE, SSR, SST and R^2
SSE = 0
SSR = 0
for i in range(0,len(cp)):
SSR = SSR + pow((cp[i]-mean_cp),2)
SSE = SSE + pow((fit_cp[i]-cp[i]),2)
SST = SSR + SSE
R_square = SSR/SST
RMSE = pow(SSE/len(cp),0.5)
print("SSR =",SSR)
print("SSE =",SSE)
print("SST =",SST)
print("R_square =",R_square)
print("RMSE =",RMSE)
Output
PROGRAM TO SHOW THE SPLITWISE METYHOD
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Linear Curve fit function
def func1(t, a, b):
return a*t + b
def func2(t, a, b, c, d):
return pow(t,3)*a + pow(t,2)*b + t*c + 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()
a = 0
b = 799
fit_cp = []
fit_cp1 =[]
for i in range(0,4):
temp = temperature[a:b+1]
cp_1 = cp[a:b+1]
popt2, pcov2 = curve_fit(func2,temp,cp_1)
fit_cp2 = func2(np.array(temp), *popt2)
popt22, pcov22 = curve_fit(func1,temp,cp_1)
fit_cp22 = func1(np.array(temp), *popt22)
for j in fit_cp2:
fit_cp.append(j)
for k in fit_cp22:
fit_cp1.append(k)
a = b+1
b = b+800
#Plotting the different results
plt.figure(1)
plt.plot(temperature, cp, color = "blue", linewidth=3)
plt.plot(temperature, fit_cp1, color="red", linewidth=3)
plt.title('Linear Curve fit using split wise method')
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
plt.figure(2)
plt.plot(temperature, cp, color = "blue", linewidth=3)
plt.plot(temperature, fit_cp, color="red", linewidth=3)
plt.title('Cubic curve fit using split wise method')
plt.legend(['Actual data', 'Curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()
OUTPUT:
Answers
Discussion
According to the first two graphs, we can see that the cubic fit is the best fit to the given data. The R-Square for linear fit is less than that in case of cubic fit. The R-Square for cubic fit is close to 1 and this means that the curve fit could be perfect in this case
This fact can be observed in the second case with the split wise method. The data is split into 4 data sets and the curve fit is performed for linear polynomial and cubic polynomial respectively. From these last two graphs, we can see that the fitting in the case of cubic fit is the best fit. Considering all the above curve fits, we have to notice that as we increase the degree of the polynomial, the error between the curve fit and the original data is reduced and by using split wise method, the goodness of the curve fit is improved. In order to get a perfect fit, we have to interpolate the datasets which means that we have to divide the original data into multiple datasets by considering the errors .
CONCLUSION
Through our work, we can say that in order to get the best fit, we have the possibility to use split wise method, or increase the order of the polynomial or to use the four parameters mentioned above in the theory part. We realized that the cubic curve fit in the case of split wise method gets improved. Thus we can say that in order to improve the cubic curve fit, we have to use the split wise method.
REFERENCES:
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 - Multivariate Newton Rhapson Solver
AIM: To write a python program to solve the given system of equations using Multivariate Newton solver. Equations INTRODUCTION Life is nonlinear, and its many diverse occurrences are frequently related to one another. Because each equation depends on the variables that have been solved…
03 Jul 2022 04:22 PM IST
Week 5.2 - Literature review: ODE Stability
AIM: To carry out a literature review on ordinary differential equations stability. Introduction A stable problem is the one for which small changes in the initial conditions elicit only small changes in the solution. In other words, stability in mathematics, is the state in which a little change in a system…
26 Jun 2022 12:11 PM IST
Compact Notation Derivation for a simple Mechanism
Aim: To derive the reaction rate ODEs and production for each species in the0 given reaction mechanism. Objective The objective in this work is to determine the net reaction rate for all four reactions using reactant and product matrices. After that, the production of each species is determined with respect to net…
13 May 2022 04:22 PM IST
CURVE FITTING USING PYTHON
AIM: The aim of this program is to write a Python code to fit a linear and cubic polynomial for the cp data. It aims to show the split wise method and explain all the parameters involved in the measurement of the fitness characteristics. OBJECTIVES: Know the importance of curve fitting in real life Learn how…
30 Apr 2022 08: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.