Week 5 - Curve fitting
Project title: Curve fitting programme
Objective: 1. generate the best curve fitting MatLab code for the given data.
2. Study the different regression analysis methods.
3. Find the best optimise fit curve.
Theory:
The 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,[14] 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.
Best fit curve:
The best fit is defined as how good the predicted curve fit with the existing data curve. for getting the best fit we can use RMS and R^2 methods where we will get very small deviation between the curves, and also by increasing the order of the polynomial til the maximum value of 10
Perfect Curve:
AS we have seen above beat is obtained curves symmetrical to each other, but using of methods like increasing the order of the polynomial may lead error, so a perfect fit is obtaining the best fit without any errors.
Problem statement:
1. What does popt and pcov mean? (Watch the video to get some context)
2. What does np.array(temperature) do?
3. What does the * in *popt mean?
4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
5. What needs to be done in order to make the curve fit perfect?
popt : array
Optimal values for the parameters so that the sum of the squared error of f(xdata, *popt) - ydata is minimized
pcov : 2d array
The estimated covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters use perr = np.sqrt(np.diag(pcov)).
How the sigma parameter affects the estimated covariance depends on absolute_sigma argument, as described above.
np.array
A NumPy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.
5. What needs to be done in order to make the curve fit perfect?
How to make perfect fit:
In order to make a perfect fit, we must consider error estimates as well. A perfect fit means, the curve should fit the original curve without showing any errors
(such as centring and scaling errors) in that particular degree of a polynomial. A perfect fit can always be a best fit but best fit can not be a perfect fit. So, in the 3rd
order polynomial I have eliminated the errors, that means curve fit we got using 'Linear regression witherror estimates' is perfect fit for 3rd order polynomial. But
it is still not the best fit. To calculate best fit, we must increase the order or use splitwise method.
How to get the best fit:
For this, we can use RMS(Root Mean square) or simple linear regression. We can change the order of polynomial to get the perfect fit. ANd the best way is, split
the data in two or more data folds and do the curve fit. I have shown how to make a best fit using 3rd order polynomial in the last part of the project.
If we use, changing od polynomial order method to get the curve fit, then 9th order is the best fit.
But, in split-wise method that I have shwon at the bottom, even the 3rd order can give the best fit.
Code:
Linear polynomial
"""
Programme for Curve fit
By Rahul Date
"""
from math import *
from numpy import *
from matplotlib.pyplot import *
from scipy.optimize import *
# Curve fit function
def func(t,a,b,c,d,e):
return a*t+b
#return a*(t**3)+b*(t**2)+c*t+d
# Reading Thermodynamic Date File
def read_file():
temperature = []
cp =[]
for line in open('data','r'):
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
# Main Function
temperature,cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature), *popt)
# Ploting
plot(temperature,cp,color="blue",linewidth=2)
plot(temperature,fit_cp,color="red",linewidth=2)
legend(['Actual Date','Curve Fit'])
xlabel('temperature [K]')
ylabel('Cp')
show()
Cubic polynomial
"""
Programme for Curve fit
By Rahul Date
"""
from math import *
from numpy import *
from matplotlib.pyplot import *
from scipy.optimize import *
# Curve fit function
def func(t,a,b,c,d,e):
#return a*t+b
return a*(t**3)+b*(t**2)+c*t+d
# Reading Thermodynamic Date File
def read_file():
temperature = []
cp =[]
for line in open('data','r'):
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
# Main Function
temperature,cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature), *popt)
# Ploting
plot(temperature,cp,color="blue",linewidth=2)
plot(temperature,fit_cp,color="red",linewidth=2)
legend(['Actual Date','Curve Fit'])
xlabel('temperature [K]')
ylabel('Cp')
show()
Code description
"""
Programme for Curve fit
By Rahul Date
"""
from math import * ................................................................Import math library for math operations
from numpy import * .............................................................Import bumpy library for trignometric and array operation
from matplotlib.pyplot import * ................................................Import matplotlib library for ploting command
from scipy.optimize import * ....................................................Import scipy.optimize library for cure fitting
# Curve fit function
def func(t,a,b,c,d,e): ..............................................................Define curve fit function
#return a*t+b ....................................................................... Linear polynomial function
return a*(t**3)+b*(t**2)+c*t+d ............................................ Cubic polynomial function
# Reading Thermodynamic Date File
def read_file(): ......................................................................Define function for read file
temperature = [] ...................................................................Create empty temperature array
cp =[] ..................................................................................Create empty cp array
for line in open('data','r'): .......................................................Open data file and read it
values = line.split(',') .............................................................Split line at ,
temperature.append(float(values[0])) ......................................Append temperature values
cp.append(float(values[1])) ....................................................Append cp values
return [temperature,cp] .........................................................written temperature and cp value
# Main Function
temperature,cp = read_file()
popt, pcov = curve_fit(func,temperature,cp) ..............................Curve fit function
fit_cp = func(np.array(temperature), *popt) ............................... Find new value of cp using temperature and popt
# Ploting
plot(temperature,cp,color="blue",linewidth=2) ............................ Ploting Actual curve with blue line
plot(temperature,fit_cp,color="red",linewidth=2) ......................... Ploting Curve fit with red line
legend(['Actual Date','Curve Fit'])
xlabel('temperature [K]')
ylabel('Cp')
show()
Output
Linear polynomial
The linear polynomial is not good for curve fitting.
the error associated with linear polynomial is more than any other polynomial
Cubic polynomial
A cubic polynomial is better than linear polynomial
error in the cubic polynomial is less than linear polynomial.