Week 5 - Curve fitting
Aim:To get the best curve fit for the linear and the cubic fitting,by using the python.
Theory:The best curve can be found from the given datasets.The curve fitting is one we represent data sets by assigning best curve fit along the entire set.This curve fit helps to know the upcomming data series,to know how it acts in future.
Procedure:The python program required for the best curve fit is:
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):
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(func,temperature,cp)
fit_cp=func(np.array(temperature),*popt)
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp,color="red",linewidth=3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature [k]')
plt.ylabel('Cp')
plt.show()
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 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(func,temperature,cp)
fit_cp=func(np.array(temperature),*popt)
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp,color="red",linewidth=3)
plt.legend(['Actual data','Curve fit'])
plt.xlabel('Temperature [k]')
plt.ylabel('Cp')
plt.show()
The above python program is required to obtain the best curve fit for cubic and linear fitting.
Results:The graps obtained from the above program are:


Conclusion:The above graphs gives the best fitting curve for the cubic and linear fitting.