All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Objective > A code to fit a linear and cubic polynomial for the Cp data. data - (https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view) > The above data file consists of two columns Temprature in K and Specific heat (cp) > Definition of > popt ,pcov >…
Jeffry Raakesh
updated on 24 Feb 2021
Objective
> A code to fit a linear and cubic polynomial for the Cp data.
data - (https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view)
> The above data file consists of two columns Temprature in K and Specific heat (cp)
> Definition of
> popt ,pcov
> np.array(temprature)
> *in*popt
> How to do perfect curve fit.
Program
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#Curve fit functions
def linear_func(t,a,b):
return a*t+b
def quadratic_func(t,a,b,c):
return a*pow(t,2)+b*t+c
def cubic_func(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
def quartic_func(t,a,b,c,d,e):
return a*pow(t,4)+b*pow(t,3)+c*pow(t,2)+d*t+e
#Function to read data file
def read_file():
temp = []
cp = []
for line in open('data','r'):
value = line.split(',')
temp.append(float(value[0]))
cp.append(float(value[1]))
return[temp,cp]
#Curve fit for linear equation
temp,cp = read_file()
popt,pcov = curve_fit(linear_func,temp,cp)
fit_cp = linear_func(np.array(temp),*popt)
plt.figure(1)
plt.plot(temp,cp,color='blue')
plt.plot(temp,fit_cp,color='red')
plt.legend(['Actual data','Curve fit'])
plt.title('Linear curve fit')
plt.xlabel('Temprature in [k]')
plt.ylabel('Cp')
#Curve fit for quadratic equation
popt,pcov = curve_fit(quadratic_func,temp,cp)
fit_cp = quadratic_func(np.array(temp),*popt)
plt.figure(2)
plt.plot(temp,cp,color='blue')
plt.plot(temp,fit_cp,color='red')
plt.legend(['Actual data','Curve fit'])
plt.title('Quadratic curve fit')
plt.xlabel('Temprature in [k]')
plt.ylabel('Cp')
#Curve fit for cubic equation
popt,pcov = curve_fit(cubic_func,temp,cp)
fit_cp = cubic_func(np.array(temp),*popt)
plt.figure(3)
plt.plot(temp,cp,color='blue')
plt.plot(temp,fit_cp,color='red')
plt.legend(['Actual data','Curve fit'])
plt.title('Cubic curve fit')
plt.xlabel('Temprature in [k]')
plt.ylabel('Cp')
#Curve fit for quartic equation
popt,pcov = curve_fit(quartic_func,temp,cp)
fit_cp = quartic_func(np.array(temp),*popt)
plt.figure(4)
plt.plot(temp,cp,color='blue')
plt.plot(temp,fit_cp,color='red')
plt.legend(['Actual data','Curve fit'])
plt.title('Quartic curve fit')
plt.xlabel('Temprature in [k]')
plt.ylabel('Cp')
plt.show()
Program explanation
> The required modules are imported.
> The equations used to fit are
Linear - a⋅t+b
Quadratic - a⋅t2+b⋅t+c
Cubic - a⋅t3+b⋅t2+c⋅t+d
Quartic - a⋅t4+b⋅t3+c⋅t2+d⋅t+e
> The above given equations are defined as functions.
> To read the data file and get the values a function is created.
> Two open arrays are created for temprature and specific heat to store the values from the data file respectively.
> A loop is made to run for every line of the data file using open command.
> Since the data file consists of two columns they are split using the (line.split(',')) command which is assigned to a variable named, value.
> Therefore, the zero index of value have the temprature data and the first index of value have cp data.
> The values are converted into float and are appended to the temp and cp array respectively.
> To curve fit the linear equation the temprature and cp values are assigned from the data file using the function.
> The curve_fit function from the scipy module is used with parameters such as linear function defined, temprature and cp.
> When using curve_fit function we get two outputs which are popt the coefficient values of the function and pcov which is the covariance matrix.
> Then the defined function is passed with the temperature values using the numpy module and the coefficients using *popt, which is then stored in the fit_cp variable.
> A figure with two plots actual curve and curve fit is generated using the plot commands.
> The above similar procedure is continued for the higher degree equations defined.
Terms explanation
POPT and PCOV
When the curve_fit function is used it gives two output popt and pcov
Popt: This variable stores the values of the coefficients of the function defined.
Pcov: It is the covariance matrix which have the estimated values of the co-variance in the parameters of the popt. It can be used to find the deviation of the parameters from the mean value
np.array(Temprature)
To pass all the temprature values as as array to the function, the numpy module is used.
*in*popt
Instead of passing all the coefficients of the function, the popt variable is used and by using the asterisk(*) helps the variable to refer the coefficients in the function.
Results of the program
Linear function
a⋅t+b
Quadratic function
a⋅t2+b⋅t+c
Cubic function
a⋅t3+b⋅t2+c⋅t+d
Quartic function
a⋅t4+b⋅t3+c⋅t2+d⋅t+e
> From the above plots it is seen that as the degree of the polynomial increases the curve fit gets better
How to do perfect curve fit.
> The data can be split into to partitions and can be plotted which can give perfect fit
> Increasing the degree of the polynomial gives perfect fit
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...
Solving 1D Linear convection equation using MATLAB
Objective > Write a Matlab code to simulate a One-dimensional Linear Convection equation. Conditions > The initial velocity profile is a step function. It is equal to 2m/s between x= 0.1 and 0.3 and 1m/s everywhere else > length is L = 1m > First-order forward differencing for the time derivative…
26 Aug 2021 12:45 PM IST
Deriving 4th order approximation of a 2nd order derivative by differencing schemes and evaluating them using MATLAB
Objective > Derive the following 4th order approximations of the second-order derivative. Central difference Skewed right-sided difference Skewed left-sided difference > To prove that the skewed schemes are fourth-order accurate. > Write a program in Matlab to evaluate the second-order derivative of the…
23 Aug 2021 07:21 AM IST
Curve fitting using Matlab
Objective > Write code to fit a linear and cubic polynomial for the given data. > To measure the fitness characteristics for both the curves. > Write code to fit a curve using splitwise method. > To explain best fit, perfect fit, and improvements for cuibc fit. Solution The code to fit a linear…
28 Mar 2021 04:00 PM IST
Breaking Ice with Air cushion Vehicle - Find minimum pressure with Newton-Raphson method in Python
Objective > To find out the value of pressure for h = 0.6 ft by the below given equation using Newton-Raphson method. p3(−β2)+(0.4hβ2−σh2r2)p2+(σ2h43r4)p−(σh23r2)3=0 p = Cushion pressure β = Width of the ice wedge …
28 Mar 2021 03:52 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.