Week 5 - Curve fitting
Aim:
- To write a code to fit a linear and cubic polynomial for the given datset comprising of values of temparature and specific heat at constant pressure.
- Show emperically as how the curve has fit
- Plot all the curves obtained.
Explain the reasons and process used for choosing best fit.
Theory
Curve fitting is the process of plotting acurve for a givemn mathematical function which has the best fit to the series of original values of the given dataset.
In other terms, weplot 2 curves one from the datset values and the otherfromthe values obtained from the considered function and calculate accuracy between original and calculated values.
Procedure
The code for curve fitting is
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit functions
def func_0(t,a,b):
return a*t+b
def func_1(t,a,b,c):
return a*pow(t,2)+b*t+c
def func_2(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
#reading thermodynamic datafile
def read_file():
temparature=[]
cp=[]
for line in open('data','r'):
values=line.split(',')
temparature.append(float(values[0]))
cp.append(float(values[1]))
return [temparature,cp]
# curve fitting for linear fit
temparature,cp=read_file()
popt_0,pcov_0=curve_fit(func_0,temparature,cp)
fit_cp=func_0(np.array(temparature),*popt_0)
plt.plot(temparature,cp,color="blue")
plt.plot(temparature,fit_cp,color='red')
plt.title('linear curve fitting')
plt.legend(['Actualcurvefit','curvefit'])
plt.xlabel('temparature')
plt.ylabel('cp')
plt.savefig('linear fit')
#calculating Rsquare value for linear fit
sum_cp=np.sum(cp)
size_cp=np.size(cp)
mean_cp=sum_cp/size_cp
sse_0=0
err_0=0
ssr_0=0
sst_0=0
for i in range(size_cp):
err_0=(cp[i]-fit_cp[i])
sse_0=sse_0+pow(err_0,2)
ssr_0=ssr_0+pow((fit_cp[i]-mean_cp),2)
sst_0=sse_0+ssr_0
Rsquare_0=ssr_0/sst_0
print('Rsquare value for linear fit:')
print(Rsquare_0)
#curve fitting for quadratic fit
popt_1,pcov_1=curve_fit(func_1,temparature,cp)
fit_cp1=func_1(np.array(temparature),*popt_1)
plt.figure(2)
plt.plot(temparature,cp,color="blue")
plt.plot(temparature,fit_cp1,color='red')
plt.title('quadratic curve fitting')
plt.legend(['Actualcurvefit','curvefit'])
plt.xlabel('temparature')
plt.ylabel('cp')
plt.savefig('quadratic fit')
#calculating Rsquare value for cubicfit
sse_1=0
err_1=0
ssr_1=0
sst_1=0
for i in range(size_cp):
err_1=(cp[i]-fit_cp1[i])
sse_1=sse_1+pow(err_1,2)
ssr_1=ssr_1+pow((fit_cp1[i]-mean_cp),2)
sst_1=sse_1+ssr_1
Rsquare_1=ssr_1/sst_1
print('Rsquare value for quadratic fit:')
print(Rsquare_1)
#curve fitting for cubic fit
popt_2,pcov_2=curve_fit(func_2,temparature,cp)
fit_cp2=func_2(np.array(temparature),*popt_2)
plt.figure(3)
plt.plot(temparature,cp,color="blue")
plt.plot(temparature,fit_cp2,color='red')
plt.title('cubic curve fitting')
plt.legend(['Actualcurvefit','curvefit'])
plt.xlabel('temparature')
plt.ylabel('cp')
plt.savefig('cubic fit')
#calculating Rsquare value for cubic fit
sse_2=0
err_2=0
ssr_2=0
sst_2=0
for i in range(size_cp):
err_2=(cp[i]-fit_cp2[i])
sse_2=sse_2+pow(err_2,2)
ssr_2=ssr_2+pow((fit_cp2[i]-mean_cp),2)
sst_2=sse_2+ssr_2
Rsquare_2=ssr_2/sst_2
print('Rsquare value for cubic fit:')
print(Rsquare_2)
In this code we create three functions linear ,quadratic as well as cubic polynamial.Then we import dataset and open and read the file and then we append the values of temperature andspecific heats.so by using the curvefit module we can calculate coefficients and coariances for the individual functions.
Common queries arised from the code:
1.What does popt and pcov mean in the above code?
A: popt is an array consisting of optimum values of the coefficients for which the sum of squares of residuals is minimised.
pcov is is basically a multidimensional array created with the values obtained by estimating the covariance obtained from the above values of popt i.e coefficients.we take diagonal elements in the array as the varinace of each parameter in popt.the dimension of array depends on no of coefficients.
2.What does np.array(temperature) do?
A:It creates an array with the values of temaprature by maintaining same type and inexing them with a tuple of non-negative integers.
3.What does the *popt mean?
A:It is used to call the best fit pararmeters calculated from the curve fit method into the function we declared above i.e the equations.'*' notation will exapnd them into arguments of the function.
- Now going into the code , we then plot 2 graphs taking temparature as X-axis and specific heat as Y-axis ,one from the values we have from the dataset and the other values obtained by using the curve fit method.
The graph of linear fit is given below:

the graph for quadratic curve fit is given below

and the graph for cubic curve fitting is given below

What things are needed to be done in order to make the curve fit perfect
1) By chaging the order of the polynomial, we can try to get best fit at any state.
2) By using splitwise curve fit i.e by splitting the data set into small sets and finding the best fits for them.in this method we can get best fit may be in lower order polynomials itself.
How to show empirically as to how well your curve has been fit?
A:Basically we consider the goodness of fit value for that.
if `R^2`=1 then curve is perfectly fit
if `R^2`<0.5 then curve is badly fit
if `R^2` is inbetween 0.98 and 0.99 then the curve is best fit
More `R^2` value ,the more likely the curve is considered as best fit.
Final conclusion
- By considering the `R^2` values of linear,quadratic and cubic fits we observe that cubic fit as good value.
- For more accurate fits we can also go for higher order polynomials for fitting or use splitwise fitting method.