All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Aim:To perform curve fitting with Python and obtain various parameters Governing Equations:The respective cubic and polynomial equations. WhereRSS is the Sum of Squares of Residuals,TSS is the total sum of squares,R^2 is the Coefficient of Determination. Wheren is the number of observations,y is a value in the sample,Yi…
Amol Anandrao Kumbhar
updated on 24 Jun 2021
Aim:
To perform curve fitting with Python and obtain various parameters
Governing Equations:
The respective cubic and polynomial equations.
Where
RSS is the Sum of Squares of Residuals,
TSS is the total sum of squares,
R^2 is the Coefficient of Determination.
Where
n is the number of observations,
y is a value in the sample,
Yi is the mean of the sample.
n is the upper limit of summation,
Yi is the value of the variable to be predicted
f(xi) is the predicted value of Yi.
Error = Yi - f(x(i))
SSE = Σ Error(i)^2
Root Mean Squared Error =√ SSE/n
Explanation:
Curve fitting is a vital process in many industries including product analysis, marketing, risk management, etc. If there is some data associated with a subject, we can plot graphs, fit a curve, and predict the future characteristics of the curve.
There are multiple types of curve fittings.
Linear Curve Fitting: In linear curve fitting, the data is fit in a straight line. However, the data need not be in a straight line. There is a high margin of error to this method, but the predictions can be made easily.
Polynomial Curve Fitting: Polynomial curve fitting is a bit more difficult to analyze than a linear curve fitting. However, accuracyis pretty high in this method.
Objectives:
The main objectives of the project is
1.To find what popt and pcov mean?
2.To identify the reason for using np.array(temperature)
3.To identify the use of the * in *popt mean?
4.To write a code to fit a linear and cubic polynomial for the Cp data.
5.To find a way in order to make the curve fit perfectly?
6.To Show empirically as to how well the curve has been fit.
Steps to follow -
1. The first step in the solution is to download the required data provided in the question.
2. After downloading, we’ll have to place the data in the same folder as our code for easier access.
3. Then we have to define a linear and a cubic polynomial as mentionedin the question.
4. We then have to process the data and make it workable with our code (the necessary operations such as separating, storing).
5. Then, we can use the scipy library to make the required curve fit and plot them using matplotlib.
6. After plotting, we are checking the validity of our curve using the Adjusted R-square, R-square (R2), The sum of squares due to error (SSE), Root mean squared error (RMSE), The sum ofsquares due to error (SSE).
Body:
1. To find what popt and pcov mean?
The variables popt and pcov are used in the code to store the value returned by the curve fit. From reading the scipy docs, I found that popt is an array of optimal values for the parameters. pcov is a 2d array and is the estimated covarinace of popt.
2. The reason for using np.array(temperature)
np.array(temperature) converts all the
3. The use of the * in *popt
Since popt is an array, * is used to select all the values in the array.
import numpy as np
from scipy.optimize import curve_fit
import math
import matplotlib.pyplot as plt
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
def read_file():
temp =[]
cp = []
for line in open('data', 'r'):
values = line.split(',')
temp.append(float(values[0]))
cp.append(float(values[1]))
return [temp, cp]
temp, cp = read_file()
popt, pcov = curve_fit(func1, temp, cp)
popt2, pcov2 = curve_fit(func2, temp, cp)
fit_cp = func1(np.array(temp), *popt)
fit_cp1 = func2(np.array(temp), *popt2)
#plotting the curves
plt.plot(temp, cp, color= "blue", linewidth=3)
plt.plot(temp, fit_cp, color= "red", linewidth=3)
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.legend(['Actual data', 'Linear Curve Fit'])
plt.savefig('Linearfit')
plt.show()
plt.clf()
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.plot(temp, cp, color= "blue", linewidth=3)
plt.plot(temp, fit_cp1, color ="green", linewidth=3)
plt.legend(['Actual data', 'Cubic Curve Fit'])
plt.savefig('CubicFit')
plt.show()
plt.plot(temp, cp, color= "blue", linewidth=3)
plt.plot(temp, fit_cp1, color ="green", linewidth=3)
plt.plot(temp, fit_cp, color= "red", linewidth=3)
plt.legend(['Actual data', 'Cubic Curve Fit', 'Linear Curve Fit'])
plt.savefig('BothPlots')
plt.show()
#Curve Vaidity using the governing equations#constants
numberofpts= np.size(cp)
total = np.sum(cp)
mean = total/numberofpts
#first curve
lsse = 0
lssr = 0
for i in range(numberofpts):
lerr = abs((np.sum((cp[i] - fit_cp[i]))))
lsse = lsse+pow(lerr,2)
lssr = lssr+ np.sum(pow((fit_cp[i] - mean),2))
lsst = lsse + lssr
lrmse = pow((lsse/numberofpts),0.5)
lsqr = lssr/lsst
print('The SST for the Linear Curve Fit is :',lsst)
print('The RMSE for the Linear Curve Fit is :',lrmse)
print('The R.Sq for the Linear Curve Fit is :',lsqr)
print('------------------------------------------------------------------------------')
# Second Curve
csse = 0
cssr = 0
for j in range(numberofpts):
cerr = abs(np.sum((cp[i] - fit_cp1[i])))
csse = csse+(pow(cerr,2))
cssr = cssr+np.sum(pow((fit_cp1[i] - mean),2))
csst = csse + cssr
crmse = pow((csse/numberofpts),0.5)
crsq= cssr/csst
print('The SST for the Cubic Curve Fit is :',csst)
print('The RMSE Value for the Cubic Curve Fit is:',crmse)
print('The R.Sq for the Cubic Curve Fit is :',crsq)
Code Explanation:
The required modules are imported in the first few lines. Then the linear and cubic equations are defined using functions asasked in the question. After that, a function is defined to read the data file provided in the problem statement. Since it is acomma-separated file, we are splitting each line at commas and storing the values in their respective arrays.
After this, the curve_fit function from the scipy module is called to give us a curve fit. The values returned by the function arestored in two arrays. The parameters are then passed to the function we defined earlier to obtain the curve.
The curves are then plotted using matplotlib commands with all the legends, axes defined. We are storing these three plots in our local system for future reference.
Now, we are calculating the curve characteristics as per the governing equations. The Error, SSE, SSR, RMSE, SST, and R^2 arecalculated for both the curves and are printed.
Output:
Results: (Also the 6th Objective)
From the plots, it is clear that the linear curve is not the best fit for the given data. Meanwhile, the cubic curve is a decent fit. The values of the required parameters are also calculated in the output.
From the output, it is clear that the curves are a 92% fit. However, in the second case, the curves are 99.07% more alike. Thisconclusively proves that with an increase in the polynomial degree, the curves will resemble each other more.
We also know that the lower is the RMSE values, the better the curves fit. It is also clear from the RMSE values that the secondcurve is a better fit than the first curve.
The R square and the RMSE values suggest that the curves we obtained are good curves since there is a 90%+ resemblance forthe linear curve and 99% for the cubic curve.
5. What to do to make the curves fit perfectly?
To make the curve fit perfectly, we should use a higher-order polynomial. It is clear from the above results that the higher is thepolynomial's order, the better will be the fit.
let us just change the linear equation into a bi-qudratic equation.
def func1(t, a, b, c, d, e):
return a*pow(t, 4) + b*pow(t, 3) + c*pow(t, 2) + d*t + e
Just changing function 1 and the word 'linear' to 'bi-quadratic' from the code in the 4th objective, we get the following outputs.
From the graphs and the R^2 and RMSE values, we can conclude that the higher-order polynomials fit the curve better.
Conclusion:
All the objectives are attained and the graphs are plotted.
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 - CHT Analysis on a Graphics card
Objective: To perform a steady state conjugate heat transfer analysis on a model of graphics card. Introduction :The term conjugate heat transfer (CHT) describes the process which involves variation of temperature within solids and fluids, due to thermal interaction between the solids and fluids, the exchange of thermal…
23 Mar 2022 04:17 AM IST
Week 5 - Rayleigh Taylor Instability
Rayleigh Taylor Instability INTRODUCTIONThe Rayleigh–Taylor instability, is the instability of an interface between two fluids of different densities which occurs when the lighter fluid is pushing the heavier fluid with the effect of gravity. Here we have prepared a model with two surfaces on over the above and considered…
07 Mar 2022 05:33 PM IST
Week 3 - External flow simulation over an Ahmed body.
External flow simulation over an Ahmed body. AIM:To simulate flow over ahmed body & to address the below given tasks.1. Describe Ahmed body & its importance.2. Explain the negative pressure in wake.3. Explain significance of the point of seperation. Expected results:1. Velocity & pressure contour of the Ahmed…
07 Mar 2022 02:44 PM IST
Week 4 - CHT Analysis on Exhaust port
CHT Analysis on Exhaust port Objective :In this challenge,1. brief description of why and where a CHT analysis is used.2. simulate both fluid flow and also the heat transfer to the solid i.e., CHT Analysis on an Exhaust port3.calculate the wall heat transfer coefficient on the internal solid surface & show the velocity…
27 Feb 2022 03:12 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.