Week 5 - Curve fitting
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#Curve fit funtion for linear polynomial
def func(t,a,b):
return a*t +b
#reading the thermodynamic data flie
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)
#with the cuve fit we have to calculate cp again thus the Fit_cp does
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')
plt.ylabel('Cp')
plt.show()
##############Code for Cubic Equation#########################
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#Curve fit funtion for cubic polynomial
def func(t,a,b,c,d):
return a*pow(t,3) + b*pow(t,2) + c*t +d
#reading the thermodynamic data flie
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)
#with the cuve fit we have to calculate cp again thus the Fit_cp does
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')
plt.ylabel('Cp')
plt.show()