PRODUCING CURVE FIT FOR SPECIFIC HEAT(Cp)
AIM - To fit a curve for the given 'data' of Cp using MATLAB
OBJECTIVE:-
- Write code to fit a linear and cubic polynomial for the Cp data.
- Plot the linear and cubic fit curves along with the raw data points.
- Explain the parameters used to measure the fitness characteristics for both the curves.
-
Answer the following questions:
- How to make a curve fit perfectly?
- How to get the best fit?
- What could be done to improve the cubic fit?
THEORY:-
- 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.
- Curve fitting is also known as regression analysis. It is used to find the "best fit" line or curve for a series of data points. Most of the time, the curve fit will produce an equation that can be used to find points anywhere along the curve.
- Line of best fit refers to a line through a scatter plot of data points that best expresses the relationship between those points. It matches with the original dataset, which is nearly the exact match of the original dataset.We can also say that the error between both the curves is minimum.
For curve fitting, there are basically four parameters to measure the goodness of fit criteria or how well the curve matches the original dataset :-
1. The sum of squares due to error (SSE) :
- Assume data points (x1,y1) , (x2,y2) , (x3,y3) , ... (xn,yn) and assume the best fit for these set of points will be y = f(x), the criterion under which the equation of the curve will be the best fit possible is as follows,
- The error for a general "i" point can be given by the function,
Error=|y(i)−value of f(x) at x(i)|=|y(i)−f(x(i))|
- To remove the modulus sign, it is effective if we square the error term, when we square the error terms and add the errors for all the 'n' points we get,
SSE(sum of squared errors)=∑[Error(i)]^2
2. R-square :
- It is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.
-
The value of R-square can be calculated using the following formula:
R^2=SSR/SST
- SSR=∑[(f(x(i))−Mean)^2]
- SSE=∑[(Y(i)−f(x(i)))^2]
- SST=SSR+SSE
3. Adjusted R-square :
- It disregards the infinitesimally small increments to R-square, and only considers the variables which actually contribute to the output of the system in a drastic way.
-
R-square is formulated as shown below,
- If a third variable is added, there can be 2 possibilities
- The newly added independent variable is significant
- The newly added independent variable is insignificant
-
For case 1,
For case 2,
4. Root mean squared error (RMSE) :
- This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data, and is defined as,
RMSE=√[∑(Y(i)−f(x(i)))^2/n]=√[SSE/n]
where, n is the total number of datapoints available.
- In MATLAB, we get these values directly by a tool called 'Curve Fitting Toolbox'. We have to just name the x-axis and y-axis from the codes originated before.
1) How to make a curve fit perfectly?
- In order to make perfect fit, error estimates have to be considered. Perfect fit means, the curve should fit the original curve without showing any errors (such as centering and scaling erros) in that particular degree of polynomial.
2) How to get the best fit?
- A perfect fit is the one that has zero error between the curve fit and the original dataset. However, a best fit is the one that shows minimum error between the curves. Perfect fit can always be a best fit but best fit can not be a perfect fit.
- To get the best fit, 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.
- In split-wise method, even the 3rd order can give the best fit. However, if we use, changing polynomial order method to get the curve fit, then 9th order is the best fit.
3) What could be done to improve the cubic fit?
- If we split data into several datafolds, cubic fit can be improved.
- There will be no errors in command window. We dont have to go to the higher order if we use this method.
CODES:-
1) LINEAR FIT :-
clear all
close all
clc
%Loading the data
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);
%Curve Fit
%cp = a*T + b
co_effs = polyfit(temp,cp,1);
predicted_cp = polyval(co_effs,temp);
%Plotting
plot(temp,cp,'linewidth',3)
hold on
plot(temp,predicted_cp,'linewidth',3,'color','r')
title('Curve Fitting of Specific Heat')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/K-mole*K]')
legend('Original dataset','curve fit')
2) CUBIC FIT :-
clear all
close all
clc
%Loading the data
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);
%Curve Fit
%cp = a*T^3 + b*T^2 + c*T + d
co_effs = polyfit(temp,cp,1);
predicted_cp = polyval(co_effs,temp);
%Plotting
plot(temp,cp,'linewidth',3)
hold on
plot(temp,predicted_cp,'linewidth',3,'color','r')
title('Curve Fitting of Specific Heat')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/K-mole*K]')
legend('Original dataset','curve fit')
CODE EXPLANATION:-
- We started with loading the 'data' provided. It includes two columns; one with values of Cp and other with temperature values corresponding to Cp. Same thing is defined in the codes.
- For curve fitting process, the dependent variable and independent variable are related with a polynomial which is linear in case of linear fit and cubic in case of cubic fit.
- In MATLAB, for curve fitting, there are two functions; 1) 'polyfit' and 2) 'polyval'.
- 'polyfit' provides the co-efficients of the polynomial suitable for maintaining the relation.
- With 'polyval' command, we can make use of the coefficients obtained to find value of the polynomial, which is basically the curve fit. By changing the value of degree 'n' in the 'polyval' command one can change the degree of the curve fit.
- n = 1 means it is a linear fit and n = 3 means it is a cubic fit.
- After peforming the curve fitting process, both the original 'data' and the 'predicted' data or the curve fit are plotted with suitable titles, x & y labels and legends(labels for both curves).
- Both the fits provided different results as shown below:
1) LINEAR FIT:
2) CUBIC FIT:
- It can be easily observed that linear fit provides a straight line which doesn't fit with the original dataset.
- Cubic fit properly matches with the dataset but the condition of the polynomial seems bad.
- MATLAB has an instrument known as 'Curve Fitting Toolbox' which helps in reducing the errors between the two curves by providing values such as SSE, R-Square, Adjusted R-Square and RMSE. It also provides a method to improve the condition of cubic fit known as 'Center and Scale'.
- The error reducing parameters can be found directly by this 'curve fitting toolbox' without performing any calculations as shown above.
ERRORS ENCOUNTERED :-
1)
- Here, the error occured due to missing of inverted commas.
- Correct code is cp_data = load('data');
2)
- It is the error in writing the column vector in line 7.
- It should be, temp = cp_data(:,1)
3)
- It is the error due to missing of 'hold on' command below line 16, which made MATLAB to consider only the last plot instead of two plots. Hence, it showed error in 'legend' command, which is basically used for more than one curves.
CONCLUSION - Thus, we have performed Curve Fitting in MATLAB and created LINEAR & CUBIC FITS.