Week 5 - Curve fitting
PROJECT REPORT
CURVE FITTING USING PYTHON
AIM :
To find the best fit for the curve which is plotted using the given data having two parameters, temperature and specefic heat at constant pressure.
THEORY :
Curve fitting is the way we model or represent a data spread by assigning a ‘best fit‘ function (curve) along the entire range. Ideally, it will capture the trend in the data and allow us to make predictions of how the data series will behave in the future.
Types of curve fitting include:
- Interpolation, where you discover a function that is an exact fit to the data points. Since this assumes no measurement error, it has limited applicability to real life scenarios.
- Smoothing, is when we find a function that is an approximate fit to the data points, but we give room for error and we allow our actual points to be near, but not necessarily on the line; given the error is minimized overall.
EXPLANATIONS :
Q1. What does popt and pcov mean?
A1. popt - An array of optimal values for the parameters which minimizes the sum of squares of residuals.
pcov - A 2D array which contains the estimated co-variance of popt. The diagonals provide the variance of the parameter estimate.
Q2. What does np.array(temperature) do?
A2. np.array - It converts the python list of temperature to a numpy array since operations like pow are allowed on numpy arrays not on python lists.
Q3. What does the * in *popt mean?
A3. The * in *popt is a reference to popt/the values stored in array popt.
Q4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
A4. PYTHON CODE :
For Linear Polynomial
#Linear_Curve_fitting
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit function
def func(t,a,b,c,d,e):
return a*t+b
#reading thermodynamic data file
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]
#main programme
temp,cp = read_file()
popt,pcov = curve_fit(func,temp,cp)
fit_cp = func(np.array(temp),*popt)
#plot results
plt.figure(1)
plt.plot(temp,cp,color="blue",linewidth = 3)
plt.plot(temp,fit_cp,color="red",linewidth = 3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('Cp')
plt.show()
Output Plot
For Cubic Polynomial
#Cubic_Curve_fitting
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit function
def func(t,a,b,c,d,e):
return a*pow(t,3)+b*pow(t,2)+c*t+d
#reading thermodynamic data file
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]
#main programme
temp,cp = read_file()
popt,pcov = curve_fit(func,temp,cp)
fit_cp = func(np.array(temp),*popt)
#plot results
plt.figure(1)
plt.plot(temp,cp,color="blue",linewidth = 3)
plt.plot(temp,fit_cp,color="red",linewidth = 3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature[K]')
plt.ylabel('Cp')
plt.show()
Output Plot
Q5. What needs to be done in order to make the curve fit perfect?
A5. By increasing the degree of the cuve, we can make the best curve fit. Also, we can use split-wise polynomials to get a perfect fit between the original values and fitted values.
PROJECT LINK : https://skill-lync.com/myprojects/edit/clJQNy9zTk1PVXNZUVhZS3VRekhQQT09Ojo1VFN1dUNxMkJzQnNKbm9E