Week 5 - Curve fitting
Aim:- Programme to perform curve fiting in python
- What does popt and pcov mean?
popt- An array of optimal values for the parameters which minimizes the sum of squares of residuals.
pcov-2d array which contains the estimated covariance of popt. The diagonals provide the variance of the parameter estimate.
- What does np.array(temperature) do?
np.array- converts the python list of temperature to a numpy array since operations like pow are allowed on numpy arrays and not python lists.
- What does the * in *popt mean?
* in *popt reference to cofficient of given equation in current problem/assignment.
## Introduction :- Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data. A related topic is regression analysis, which focuses more on questions of statistical inference such as how much uncertainty is present in a curve that is fit to data observed with random errors. Fitted curves can be used as an aid for data visualization, to infer values of a function where no data are available, and to summarize the relationships among two or more variables. Extrapolation refers to the use of a fitted curve beyond the range of the observed data, and is subject to a degree of uncertainty since it may reflect the method used to construct the curve as much as it reflects the observed data.
##"""""""PROGRAM TO PERFORM CURVE FITTING """""""""""""
""" PROGRAM TO PERFORM CURVE FITTING """
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#function to read file
def read_file():
temp = []
c_p = []
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
c_p.append(float(values[1]))
return [temp,c_p]
temp,c_p = read_file() # calling the function read_file and storing the values into temp & cp
# order of equation for curve fit
i=[(lambda t,a,b:a*t + b),(lambda t,a,b,c:(a*t + b)*t+c),(lambda t,a,b,c,d:((a*t + b)*t+c)*t+d),
(lambda t,a,b,c,d,e:(((a*t + b)*t+c)*t+d)*t+e),(lambda t,a,b,c,d,e,f:((((a*t + b)*t+c)*t+d)*t+e)*t+f),
(lambda t,a,b,c,d,e,f,g:(((((a*t + b)*t+c)*t+d)*t+e)*t+f)*t+g),(lambda t,a,b,c,d,e,f,g,h:((((((a*t + b)*t+c)*t+d)*t+e)*t+f)*t+g)*t+h),
(lambda t,a,b,c,d,e,f,g,h,k:(((((((a*t + b)*t+c)*t+d)*t+e)*t+f)*t+g)*t+h)*t+k),(lambda t,a,b,c,d,e,f,g,h,k,l:((((((((a*t + b)*t+c)*t+d)*t+e)*t+f)*t+g)*t+h)*t+k)*t+l)]
j=0
for func in i:
popt,pcov = curve_fit(func,temp,c_p) # popt = coefficients of equation # pcov = estimated covarience of popt
fit_cp = func(np.array(temp), *popt) # passssing temp as numpy array
plt.plot(temp,c_p,color="black",linewidth=3)
plt.plot(temp,fit_cp,color="green",linewidth=3)
j=j+1
if j==1:
a='st'
elif j==2:
a='nd'
elif j==3:
a='rd'
else:
a='th'
b=str(j)+a+' order of curve fit data'
plt.legend(['actual data',b])
plt.xlabel('temperature [K]')
plt.ylabel('cp')
plt.show()
##Output Of Code :-
- 1st order of cur fit
- 2nd order of curve fit
- 3rd order of curve fit
- 4th order of curve fit
- 5th order of curve fit
- 6th order of curve fit
- 7th order of curve fit
- 8th order of curve fit
- 9th order of curve fit
##Conclusion in order to make the curve fit perfect :-
- The results are good because the cubic fit is better curve fitt than linear curve fit as we can see from the output plots.
- we can see from the above programme splitting the entire data into some would fit the curve better rather than using one higher order polynomial for the entire data. And for the lesser values of temperature we can observe that lower order polynomials ts , and as the data size and the values increases the order of polynomial should increase to get the best fit.
- By increasing the degree of the curve we can make the best curve fit. If we further increase the degree of the curve to 4th oorder, then the curve will be more accurate than the cubic polynomial. Further increasing the degree of polynomial will increase the calculating complexity and time required.