All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: To write the Python program to fit a linear and cubic polynomial for the Cp data. THEORY : 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. It is needed to predict…
Jayesh Keche
updated on 24 Jul 2021
AIM: To write the Python program to fit a linear and cubic polynomial for the Cp data.
THEORY :
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. It is needed to predict the future values comparing with current data.
Curve fitting examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a "best fit" model of the relationship.
The most common way to fit the curve is using polynomial terms, such as linear ,square , cubic equations. We increase the order to get to increase the bends to get fit the curve as close as previous data. Hence here also we use cubic polynomial. While using increased order polynomial it might happen that it gives warning . But keeping same degree we can eliminate the warning by doing centering and scaling so that he curve can be close to perfectively fit.
What do popt and pcov mean?
popt- It helps to minimize the sum of the squared error of function data by an array of optimal values of parameters
pcov-The estimated covariance of popt or in other words tells that how far the variables may change together. The diagonal
provides the variance of the parameter estimate.
What does np.array(temperature) do?
The temperature values will be taken and pass as a numpy array to perform certain mathematical operations.
What does the * in *popt mean?
This is used to tell that this is stored in a popt array. It contains the value of coefficients and refers to these values.
Program:-
1) For Linear Eq
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def linear(t, a, b): # Linear curve fit function
return a*t + b
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]
temperature, cp = read_file() # Reading original values of data
popt,pcov = curve_fit(linear, temperature, cp)
fit_cp = linear(np.array(temperature), *popt)
# Plot
plt.plot(temperature, cp, color="cyan",linewidth=3)
plt.plot(temperature, fit_cp, color="magenta",linewidth=3)
plt.legend(['Actual data', 'curve fit data'])
plt.xlabel('Temperature [k]')
plt.ylabel('cp')
plt.show()
Output graph:
2) For Cubic Eq
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(t,a,b,c,d):
return a*pow(t,3)+ b*pow(t,2) + c*t + d
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 programe
temperature, cp = read_file() # Reading original values of data
popt,pcov = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)
# Plot
plt.plot(temperature, cp, color="blue",linewidth=3)
plt.plot(temperature, fit_cp, color="magenta",linewidth=3)
plt.legend(['Actual data', 'curve fit data'])
plt.xlabel('Temperature [k]')
plt.ylabel('cp')
plt.show()
Output graph:
Explanation:-
In the programme, we call the function using read_file that contained specific heat of o2 as a function of temperature
When we execute the data file the values will be stored in temperature and cp value.
For that, we define a linear and cubic function.
We initialise the empty array for cp and temperature.
Now in for loop, we open a file data with 'r' means in reading mode line by line.
Separate the data with ','.
1st is the temperature value and the second is the cp.
As they are in a text file as a string so we have to convert them using 'float' to use in the calculation.
Append them and return to cp.
Now we use the curve_fit function in the scipy module with its parameters.
The first input is the mathematical model that is using to represent our data. The function is in this case linear and cubic.
With this, we get 1 output- popt (they are the coefficients a,b in linear function) and pcov.
Once we have new coefficients we take the function that we defined and pass the temperature array along with the
coefficients.
With curve fit, we calculate the cp again, that called fit_cp, and plot them.
Conclusion:
From both these above linear and cubic curve fit it can be easily seen that if we increase the order of polynomial we
observed and curve fit data curve will be closer that means we will have better results with the cubic fit.
CONCLUSION:
Based on the comparison with plot-1 and plot-2, the Cubic polynomial curve is better than the liner polynomial curve.
As empirically we can conclude that the order or the degree of equation increases, we get less number of errors, so that best
curve fit can be obtained.
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...
Roof challenge
Design of Roof Objectives- 1. For the Given Roof styling, Develop the essential flanges and reinforcements, provide appropriate dimensions and check forDraft analysis and also submit your section modulus report on the 0-Y section.2. Start with the creation of a roof ditch area the tool opening angle is only 3 degrees.3.…
30 Jul 2021 04:51 PM IST
Section Modulus calculation and optimization
AIM: Section modulus calculation and optimization OBJECTIVE: Use section of the Hood and calculate the section modulus.Vary the section in such a way that new section is an improved version of previous section. THEORY:Section modulus is to find the geometric property for a given cross-section used in the design.Calculate…
30 Jul 2021 04:51 PM IST
Fender Design Challenge
FENDER: The fender is the car body part that frames the wheel. Its purpose is to prevent sand, dirt, rocks, and other roads spray from beinthrown into the air by the rotating tire and close the engine component. When a vehicle is moving, tires pick up stones and otherand hurl them in all directions. Fenders are basically…
30 Jul 2021 04:51 PM IST
Hood design-Week 2
DESIGNING OF HOOD ASSEMBLY Objective To understand and study the design methodology and mechanisms that goes under designing and developing an AutomotivHood to achieve maximum safety for everyone in and around vehicle. HOOD DESIGN- CHALLANGE Introduction:- Hood is main component of a car at the front portion. It is used…
30 Jul 2021 04:50 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.