Week 5 - Curve fitting
REPORT ON CURVE FIT USING PYTHON
Created by Shahbaaz Khan Deoda
On 20 may 2020
AIM: - Write a code to obtain linear and cubical cur fit.
EQUATION USED FOR FITS: -
1. for linear fit: - a.t + b
2. for cubical fit: - a.t2 + b.t + c
a, b, c are the coefficients of the equations.
OBJECTIVE: - 1. To code the curve fit.
- to obtain the curve fit.
TERMINOLOGY: -
- What does popt and pcov mean?
Ans. popt = array of cofficients
Pcov = 2d array of covariance of popt
- What does np.array(temperature) do?
Ans. it takes the value of temprature from the data file to do mathematical opration like square and multiplication
- What does the * in *popt mean?
Ans. * used for take all value of popt in consideration
- What needs to be done in order to make the curve fit perfect?
Ans. for different ranges different polynomial fits are used
CODE FOR LINEAR FIT: -
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# curve fit function
def func(t,a,b,):
return a*t + b
# reading thermodynamic data file
def read_file():
temprature = []
cp = []
for line in open('data','r'):
values = line.split(',')
temprature.append(float(values[0]))
cp.append(float(values[1]))
return [temprature, cp]
#main programe
temprature, cp = read_file()
popt, pcov = curve_fit(func, temprature, cp)
fit_cp=func(np.array(temprature),*popt)
# curve fit plot
plt.plot(temprature,cp,color="blue",linewidth=3)
plt.plot(temprature,fit_cp,color="red",linewidth=3)
plt.legend(['actual data','curve fit'])
plt.xlabel('Temprature[K]')
plt.ylabel('Cp[kJ/kg.K]')
plt.title('linear curve fit')
plt.show()
PLOT FOR LINEAR CURVE FIT: -
CODE FOR CUBICAL FIT: -
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# curve fit function
def func(t,a,b,c):
return pow(a,3)*t + b*t + c
# reading thermodynamic data file
def read_file():
temprature = []
cp = []
for line in open('data','r'):
values = line.split(',')
temprature.append(float(values[0]))
cp.append(float(values[1]))
return [temprature, cp]
#main programe
temprature, cp = read_file()
popt, pcov = curve_fit(func, temprature, cp)
fit_cp=func(np.array(temprature),*popt)
# curve fit plot
plt.plot(temprature,cp,color="blue",linewidth=3)
plt.plot(temprature,fit_cp,color="red",linewidth=3)
plt.legend(['actual data','curve fit'])
plt.xlabel('Temprature[K]')
plt.ylabel('Cp[kJ/kg.K]')
plt.title('cubical curve fit')
plt.show()
PLOT FOR CUBICAL FIT: -
CONCLUSION: -we have succesfully code the program for linear and cubical fit. And also evaluate the curve for linear and cubical fit.