All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Objective: Write a Python Program to curve fit a linear and cubic polynomial for the given Cp data and explain the results. 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…
Giridhar Lingutla
updated on 01 Mar 2021
Objective:
Write a Python Program to curve fit a linear and cubic polynomial for the given Cp data and explain the results.
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. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data.
Governing Equations:
Linear or first order polynomial: y=ax+b
Quadratic or second order polynomial: y=ax2+bx+c
Cubic or third order polynomial: y=ax3+bx2+cx+d
Curve Fitting for Linear and Cubic Polynomial:
Python Code:
'''
Curve fit for Linear Polynomial by Giridhar
'''
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
# Linear Curve fit Function
def fun(t,a,b,):
return a*t + b
# Reading Thermodynamics 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(fun, temperature, cp)
fit_cp = fun(np.array(temperature),*popt)
plt.plot(temperature, cp,color='blue',linewidth=2.5)
plt.plot(temperature, fit_cp,color='red',linewidth=2.5)
plt.title('Specific heat(cp) Vs Temperature (Linear Polynomial)')
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('Specific heat(cp)')
plt.grid()
plt.savefig('case_1')
plt.show()
'''
Curve fit for Cubic Polynomial by Giridhar
'''
# Cubic Curve fit Function
def fun(t,a,b,c,d):
return a*pow(t,3) + b*pow(t,2) + c*t + d
# Reading Thermodynamics 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(fun, temperature, cp)
fit_cp = fun(np.array(temperature),*popt)
plt.plot(temperature, cp,color='blue',linewidth=2.5)
plt.plot(temperature, fit_cp,color='red',linewidth=2.5)
plt.title('Specific heat(cp) Vs Temperature (Cubic Polynomial)')
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('Specific heat(cp)')
plt.grid()
plt.savefig('case_2')
plt.show()
Specific heat(cp) Vs Temperature (Linear Polynomial):
Specific heat(cp) Vs Temperature (Cubic Polynomial):
With an increase in order of Polynomial, the curve fit gets better.
Python Code Explanation:
What does popt and pcov mean?
popt and pocv are the outputs of the function curve_fit. popt is the array that contains the optimal parameters or the optimum values of the coefficients of the cubic equation. pcov is a 2D array that contains the estimated covariance of the parameters in popt.
What does np.array(temperature) do?
It generates an array and stores the different values of the temperature and passes the generated value to the NumPy for numerical calculation in python.
What does the * in *popt mean?
*popt is used to expand the list of values stored in the array 'popt' into the arguments of the function.
Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
The results for the linear polynomial is not good as the actual data does not obey the linear equation.
The results for the Cubic polynomial is good but not perfect as both the actual plot and the curve fit plot flows the same plot.
What needs to be done in order to make the curve fit perfect?
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
https://skill-lync.com/projects/data-analysis-using-python-48
02 Mar 2021 07:35 PM IST
Data Analysis using Python
Objective: Extract a plot between any two quantities from the given data file automatically. Compatibility check: Code should exit gracefully, if a non-compatible file is provided as an input. Calculate the area under P-V diagram. Calculate the power output of an engine running at 1500 RPM. Calculate the specific fuel…
02 Mar 2021 07:35 PM IST
Curve Fitting using Python
Objective: Write a Python Program to curve fit a linear and cubic polynomial for the given Cp data and explain the results. 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…
01 Mar 2021 12:41 PM IST
Find Minimum Cushion Pressure with Newton-Raphson method of an Air Cushion Vehicle to break Ice in Python
Objective: Write a Python program to find minimum cushion pressure of an Air Cushion Vehicle to Break Ice with Newton-Raphson method. Governing Equation: The factors involved are: p - cushion pressure h - thickness of the ice field r - size of the air cushion σ - tensile strength of the ice β -…
27 Feb 2021 05:20 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.