Week 5 - Curve fitting
aim :- program to write curve fitting.
Import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit fucntion
def func(a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
#reading data_file
#filecontains two columns namely temperature and specific heat
def read_file():
temperature=[]
cp=[]
for line in open('data','r') #readingeachlineof file
temperature.append(float(line.split(',')[0]))
cp.append(float(line.split(',')[1]))
return[temperature,cp]
#main program
temperature,cp=read_file()
popt,pcov=curve_fit(func,temperature,cp)
print(popt)
print(pcov)
fit_cp=func(np.array(temperature),*popt)
plt.plot(temperature,cp,linewidth=3,color='blue')
plt.plot(temperature,fit_cp,linewidth=3,color='green')
plt.legend(['actual data','curve fit'])
plt.xlabel('Temperature[k]')
plt.ylabel('cp')
plt.shown()