Curve fitting
4. To perform a curve fitting between the temperature and specific heat vlaues given in the data file.
Theory:
Curve fitting is the process of constructing a curve or mathematical function that has a best fit to series of data points possibly subjected to constraints.
Curve fitting can involves 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.
The accuracy of curve fit can be improved by increasing the degree of polynomial.So a \'best fit\' can be obtained by increaing the degree of the polynomial.
Main program:
#Importing modules
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#Linear function definition
def linear_func(t,a,b):
return a*t+b
#Cubic function definition
def cubic_func(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
#Reading of data file
def file():
temperature=[]
cp=[]
for line in open(\'data\',\'r\'):
a=line.split(\',\')
temperature.append(float(a[0]))
cp.append(float(a[1]))
return[temperature,cp]
#Main program
temperature,cp=file()
#Solving equation to find constants
popt1,pcov1=curve_fit(linear_func,temperature,cp)
popt2,pcov2=curve_fit(cubic_func,temperature,cp)
#Calculating for equation
fitcp1=linear_func(np.array(temperature),*popt1)
fitcp2=cubic_func(np.array(temperature),*popt2)
#plotting the result
plt.plot(temperature,cp,color=\"blue\",linewidth=2)
plt.plot(temperature,fitcp1,color=\"red\",linewidth=2)
plt.plot(temperature,fitcp2,color=\"green\",linewidth=2)
plt.legend([\'Actual data\',\'Linear curvefit\',\'Cubic curvefit\'])
plt.xlabel(\'Temperature\')
plt.ylabel(\'Specific heat\')
plt.show()
Explanation of code:
- Initailly, required modules are imported.
- We difined two equations, such as linear and cubic equation.
- Using the for loop, we are reading the data values from given data file.
- In main program, we are calling the function file.
- Using \'curve_fit\' from module scipy we solve both the equation and get constants values.
- Then we use the values to solve for temperature and Cp values.
- Finally we plot both linear and cubic curve fit equation with original curve.
Result:
a)
Above graph shows that cubic fitting curve was close to the actual data curve as opposed to the linear curve. So we can say by increasing the order of polynomial we can increase the goodness of fit.
b)
Above result can be obtained when we have polynomial of degree 5.we can obseve goodness of curve is improved in above graph.
1.What does popt and pcov means ?
popt is array stores the constants values of the equation. In above case it stores the values of a,b,c,d,e,f of higher order polynomial.
pcov array contains the values of true variance and covariance of the parameters returned in popt array.
2. Significance of np.array
A numpy array is a grid of values, all of the same type and is indexed by a record of nonnegative integers. The numper of dimension is the rank of the array the shape of an array is a record of integer giving the size of the array along each dimension.
3.Meaning of \'*\' in \'*popt\'
The * in *popt is used to pass the values of popt array (parameters including a,b in case of linear ) as arguments to the function being called on (in the program linear_func and cubic_func)
5.What needs to e done in order to make the curve fit perfect?
we can make the curve fit better by either using a higher-order polynomial or breaking the temperature into two different ranges or both. Following can be done to make a curve fit perfectly,
By increasing the order of the polynomial or by increasing the center and scaling the curve.
By using cross validation techniques where the data is split into folds.Then stepwise the told is used to validate the model in the model in the end choose a lower error