Week 5 - Curve fitting
Week 5 - Curve fitting
OBJECTIVE:
Write code to fit curve perfectly by using diffrent degree polynomial equation.
PROCEDURE:
1. Import numpy, matplotlib.pyplot and curve_fit librarires.
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
2. Create 4th degree polynomial equation as function of t and a,b,c,d,e as constant
#4th degree Curve fit function
def func_1(t,a,b,c,d,e):
return a*pow(t,4)+b*t*t*t+c*t*t+d*t+e
3. Create 3rd degree polynomial equation as function of t and a,b,c,d as constant
#3rd degree(Cubic Polynomial) Curve fit function
def func_2(t,a,b,c,d):
return a*pow(t,3)+b*t*t+c*t+d
4. Create linear equation as function of t and a,b as constant
#linear curve fit function
def func_3(t,a,b,):
return a*t+b
5. Reading temperature and cp value from data file.
#Reading data file
def readfile():
temperature=[]
cp=[]
for line in open('data','r'):
value=line.split(',')
temperature.append(float(value[0]))
cp.append(float(value[1]))
return [temperature,cp]
6. By using curve_fit optimize 4th degree polynomial equation.
#curve fit optimization for 4th degree equation
temperature,cp=readfile()
popt_1,pcov_1=curve_fit(func_1,temperature,cp)
fit_cp_1=func_1(np.array(temperature),*popt_1)
7. By using curve_fit optimize 3rd degree polynomial equation.
#curve fit optimization for 3rd degree(Cubic Polynomial) equation
temperature,cp=readfile()
popt_2,pcov_2=curve_fit(func_2,temperature,cp)
fit_cp_2=func_2(np.array(temperature),*popt_2)
8. By using curve_fit optimize linear equation.
#curve fit optimization for linear equation
temperature,cp=readfile()
popt_3,pcov_3=curve_fit(func_3,temperature,cp)
fit_cp_3=func_3(np.array(temperature),*popt_3)
9. Plot graph for actual data and curve fit value by using 4th degree polynomial equation.
#plot graph for 4th degree equation
plt.plot(temperature,cp)
plt.plot(temperature,fit_cp_1)
plt.legend(['Actual Data','curve fit'])
plt.xlabel('temperature')
plt.ylabel('cp')
plt.title('4th degree equation curve fit')
plt.savefig('4th degree equation curve fit')
plt.show()
10. Plot graph for actual data and curve fit value by using 3rd degree polynomial equation.
#plot graph for 3rd degree(cubic polynomial) equation
plt.plot(temperature,cp)
plt.plot(temperature,fit_cp_2)
plt.legend(['Actual Data','curve fit'])
plt.xlabel('temperature')
plt.ylabel('cp')
plt.title('3rd degree equation curve fit')
plt.savefig('3rd degree equation curve fit')
plt.show()
11. Plot graph for actual data and curve fit value by using linear equation.
#plot graph for linear equation
plt.plot(temperature,cp)
plt.plot(temperature,fit_cp_3)
plt.legend(['Actual Data','curve fit'])
plt.xlabel('temperature')
plt.ylabel('cp')
plt.title('linear equation curve fit')
plt.savefig('linear equation curve fit')
plt.show()
RESULT:
Complete program:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#4th degree Curve fit function
def func_1(t,a,b,c,d,e):
return a*pow(t,4)+b*t*t*t+c*t*t+d*t+e
#3rd degree(Cubic Polynomial) Curve fit function
def func_2(t,a,b,c,d):
return a*pow(t,3)+b*t*t+c*t+d
#linear curve fit function
def func_3(t,a,b,):
return a*t+b
#Reading data file
def readfile():
temperature=[]
cp=[]
for line in open('data','r'):
value=line.split(',')
temperature.append(float(value[0]))
cp.append(float(value[1]))
return [temperature,cp]
#curve fit optimization for 4th degree equation
temperature,cp=readfile()
popt_1,pcov_1=curve_fit(func_1,temperature,cp)
fit_cp_1=func_1(np.array(temperature),*popt_1)
#curve fit optimization for 3rd degree(Cubic Polynomial) equation
temperature,cp=readfile()
popt_2,pcov_2=curve_fit(func_2,temperature,cp)
fit_cp_2=func_2(np.array(temperature),*popt_2)
#curve fit optimization for linear equation
temperature,cp=readfile()
popt_3,pcov_3=curve_fit(func_3,temperature,cp)
fit_cp_3=func_3(np.array(temperature),*popt_3)
#plot graph for 4th degree equation
plt.plot(temperature,cp)
plt.plot(temperature,fit_cp_1)
plt.legend(['Actual Data','curve fit'])
plt.xlabel('temperature')
plt.ylabel('cp')
plt.title('4th degree equation curve fit')
plt.savefig('4th degree equation curve fit')
plt.show()
#plot graph for 3rd degree(cubic polynomial) equation
plt.plot(temperature,cp)
plt.plot(temperature,fit_cp_2)
plt.legend(['Actual Data','curve fit'])
plt.xlabel('temperature')
plt.ylabel('cp')
plt.title('3rd degree equation curve fit')
plt.savefig('3rd degree equation curve fit')
plt.show()
#plot graph for linear equation
plt.plot(temperature,cp)
plt.plot(temperature,fit_cp_3)
plt.legend(['Actual Data','curve fit'])
plt.xlabel('temperature')
plt.ylabel('cp')
plt.title('linear equation curve fit')
plt.savefig('linear equation curve fit')
plt.show()
1. What does popt and pcov mean?
a) popt: popt use to get optimize parameter for fitted function.
b) pcov: pcov use to get estimated covariance of popt. It is 2D array.
2. What does np.array(temperature) do?
By using np.array convert temperature data into array formate.
3. What does the * in *popt mean?
popt store output value of coefficient a,b,c,d,e from cureve_fit function. Use of * (star) is refer these coefficient values.
4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
Refer above code. For cubic polynomial having better curve fit compare to linear equation, and for 4th degree polynomial got best result.
5. What needs to be done in order to make the curve fit perfect?
I have increase degree of polynomial equation in order to make curve fit perfect. For 4th degree polynomial equation got better curve fit compare to 3rd degree polynomial and linear equation.
Below are curves for diffrent degree of equation