Project

Python code to fit a linear and cubic polynomial for the Cp data with respect to the temperature.

Curve fit :-

Curve fitting is the process of constructing a curve, or mathematical function that has the best fit to a series of data points. The objective of curve fitting is to find the parameters of a mathematical model that describes a set of data in a way that minimizes the difference between the model and the data. The most common approach is the "linear least squares" method, also called "polynomial least squares", a well-known mathematical procedure for finding the coefficients of polynomial equations that are a "best fit" to a set of X,Y data. A polynomial equation expresses the dependent variable Y as a weighted sum of a series of single-valued functions of the independent variable X, most commonly as a straight line (Y = a + bX, where a is the intercept and b is the slope), or a quadratic (Y = a + bX + cX2), or a cubic (Y = a + bX + cX2 + dX3), or higher-order polynomial. Those coefficients (a, b, c, etc) can be used to predict values of Y for each X. In all these cases, Y is a linear function of the parameters a, b, c, and/or d. This is why we call it a "linear" least-squares fit, not because the plot of X vs Y is linear. Only for the first-order polynomial Y = a + bX is the plot of X vs. Y linear.

 What does popt and pcov mean? 

eg:- def func1 (t, a, b):
             return a*t + b

The curve_fit function returns two items, which we can be stored in popt and pcov. The popt argument are the best-fit paramters for function inputs (a,b).

The pcov variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters. This is mostly useful when the data has uncertainties.

coveriance:- The mean value of the product of the deviations of two variates from their respective means.

 What does np.array(temperature) do?

A numpy array is a grid of values, all of the same type, and is indexed by a data structure consisting of multiple parts of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a data structure consisting of multiple parts of integers giving the size of the array along each dimension.

thus in the program the np.temperature create the array of values with rank-1 which helps the program to select the particular temperature and calculate the cp value for the cure.

 What does the * in *popt mean?

in the program * is pointer which store the address of the variables in an array. *popt gives out the integer value of constants.

A python code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.

"""
A python program to obtain the best curve fit for the available 
thermodynamic data file
by Rupesh
"""
import math
import matplotlib.pyplot as plt 
import numpy as np 
from scipy.optimize import curve_fit

# Curve fit function
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


#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()
popt1,pcov1 = curve_fit (func1, temperature, cp)
popt2,pcov2 = curve_fit (func2, temperature, cp)
fit_cp1 = func1 (np.array (temperature), *popt1)
fit_cp2 = func2 (np.array (temperature), *popt2)

# plotting the actual and estimated curves
plt.figure()
plt.plot(temperature, cp , color = 'blue', lw = 3)
plt.plot(temperature, fit_cp1, color = 'red', lw = 3)
plt.legend(['Actual data', 'linear cure fit'])
plt.title(' Linear curve fit')
plt.xlabel('Temperature[k]')
plt.ylabel('cp')

plt.figure()
plt.plot(temperature, cp , color = 'blue', lw = 3)
plt.plot(temperature, fit_cp2, color = 'green', lw = 3)
plt.legend(['Actual data', 'cubical cure fit'])
plt.title(' Cubical curve fit')
plt.xlabel('Temperature[k]')
plt.ylabel('cp')
plt.show()

the results are represented by the curve as shown below.

if we further increase the power of the polynomial we get the exact curve fit. From the above graph we can say that the cubical curve fit is the best fit.

What needs to be done in order to make the curve fit perfect?

By observation it is noted that the increase in degree of polynomial gives us the prefect curve fit. Thus in the given problem cubic curves can be made as best fits.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.