Week 5 - Curve fitting
AIM : Program to perform curve Fitting in PYTHON.
THEORY : Curve tting is the process of construction of a curve , or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and the parameter values, etc. It is highly effective in the mathematical modeling of some natural processes.
1.What does popt and pcov mean?
Ans.
" popt " is an array that stores the values of the coecients that are being passed in a given function .
" pcov " is an two dimensional array that stores the values of the estimated covarience of popt i.e the coecients.
2.What does np.array(temperature) do?
Ans.
The above command is used for passing the vales of temperature as a numpy array.
3.What does the * in *popt mean?
Ans.
" * " in *popt helps us to refer to the coecients in a given equation.
4. Write Codes to fit a linear and cubic polynomial for the Cp data: Explain if your Result are Good or Bad.
Ans.
Curve Fitting for a Linear Function.
""" PROGRAM TO PERFORM CURVE FITTING FOR A LINEAR FUNCTION """
# Program By Dakshkumar Jadav
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(t,a,b):
return a*t + b
def read_file():
temp = []
cp = []
for line in open ('data','r'): # opening a file and reading the data
values = line.split(',') # splitting line using comma
temp .append(float(values[0])) # appending or adding data into the temp file
cp.append(float(values[1]))
return [temp,cp]
temp,cp = read_file() # calling the function read_file and storing the values into temp & cp
popt,pcov = curve_fit(func,temp,cp) # popt = coefficients i.e a & b , pcov = estimated covarience of popt
fit_cp = func(np.array(temp), *popt) # we are passing temp as numpy array
plt.plot(temp,cp,color="red",linewidth=3)
plt.plot(temp,fit_cp,color="blue",linewidth=3)
plt.legend(['actual data','curve fit data'])
plt.xlabel('temperature [K]')
plt.ylabel('cp')
plt.show()
Graph For a Linear :-
#Curve Fitting for a Cubic Polynomial :-
""" PROGRAM TO PERFORM CURVE FITTING FOR CUBIC POLYNOMIAL """
#Program by Dakshkumar Jadav
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(t,a,b,c,d):
return a*pow(t,3)+ b*pow(t,2) + c*t + d
def read_file():
temp = []
cp = []
for line in open ('data','r'): # opening a file and reading the data
values = line.split(',') # splitting line using comma
temp .append(float(values[0])) # appending or adding data into the temp file
cp.append(float(values[1]))
return [temp,cp]
temp,cp = read_file()
popt,pcov = curve_fit(func,temp,cp) # popt = coefficients i.e a , b , c & d , pcov = estimated covarience of popt
fit_cp = func(np.array(temp), *popt) # we are passing temp as numpy array
plt.plot(temp,cp,color="red",linewidth=3)
plt.plot(temp,fit_cp,color="blue",linewidth=3)
plt.legend(['actual data','curve fit data'])
plt.xlabel('temperature [K]')
plt.ylabel('cp')
plt.show()
Graph for a Cubic Polynomial :-
Thus;
From the above two codes we see that as the order of the polynomial gets higher the better Fit is obtained . So the results obtained for linear are bad when
compared to cubic.
5.What needs to be done in order to make the curve Fit perfect?
Ans.
We can make the curve t better by either using a higher order polynomial or by breaking the temperature into two different ranges or both .