Curve fitting Using MATLAB
Aim: To write code to fit a linear and cubic polynomial for the cp data using MATLAB.
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.
After fitting data with one or more models, we should evaluate the goodness of fit. A visual examination of the fitted curve displayed in Curve Fitting app should be our first step. Beyond that, the toolbox provides these methods to assess goodness of fit for both linear and nonlinear parametric fits:
- Goodness-of-Fit Statistics
- Residual Analysis
- Confidence and Prediction Bounds
We can group these measures into two types: graphical and numerical. The residuals and prediction bounds are graphical measures, while the goodness of fit statistics and confidence bounds are numerical measures.
Generally speaking, graphical measures are more beneficial than numerical measures because they allow us to view the entire data set at once, and they can easily display a wide range of relationships between the model and the data. The numerical measures are more narrowly focused on a particular aspect of the data and often try to compress that information into a single number. In practice, depending on our data and analysis requirements, we might need to use both types to determine the best fit.
Note that it is possible that none of our fits can be considered the best one. In this case, it might be that we need to select a different model. Conversely, it is also possible that all the goodness of fit measures indicate that a particular fit is the best one. However, if our goal is to extract fitted coefficients that have physical meaning, but our model does not reflect the physics of the data, the resulting coefficients are useless. In this case, understanding what our data represents and how it was measured is just as important as evaluating the goodness of fit.
Residuals
The residuals from a fitted model are defined as the differences between the response data and the fit to the response data at each predictor value.
`residual = data - fit`
Goodness of Fit Statistics
After using graphical methods to evaluate the goodness of fit, we should examine the goodness of fit statistics. The Curve Fitting Toolbox supports the following goodness of fit statistics for parametric models:
- The sum of squares due to error (SSE): A value closer to 0 indicates that the model has a smaller random error component, and that the fit will be more useful for prediction.
- R-square: R-square can take on any value between 0 and 1, with a value closer to 1 indicating that a greater proportion of variance is accounted for by the model. For example, an R-square value of 0.8234 means that the fit explains 82.34% of the total variation in the data about the average.
- Adjusted R-square: The adjusted R-square statistic can take on any value less than or equal to 1, with a value closer to 1 indicating a better fit. Negative values can occur when the model contains terms that do not help to predict the response.
- 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 =s=sqrt(MSE)` where MSE is the mean square error or the residual mean square `MSE = (SSE)/v`
Where v is the residual degrees of freedom and is defined as the number of response values n minus the number of fitted coefficients m estimated from the response values.`v=n-m`
Just as with SSE, an MSE value closer to 0 indicates a fit that is more useful for prediction.
For more details please refer the following link.
Problem Statement
Given a file named data that consists of temperature and specific values of a fluid. Write a MATLAB program to get the best fit using linear and cubic polynomial fits.
Stepwise Explanation of Code
- Starting with clearing all the previous commands and variables in the workspace.
- Loading the file named ‘data’ in MATLAB using load(‘data’) (Please make sure that the file should be present in the working directory before running this command).
- Extracting temperature and cp values as column vectors.
- Using fit command to plot the data. To know more about fit command, type help fit or doc fit in command prompt. This function is used to get two arguments which are polynomial coefficients and gof which is the goodness of fit statistics.
- Then the graphs are compared and an error is generated while performing cubic fit which states as follows “Warning: Equation is badly conditioned. Remove repeated data points or try centering and scaling.”
- In order to remove the warning or in other words improving the cubic curve, we use normalisation and robust command from curvefit toolbox.
- Finally, the goodness of curve statistics are compared as follows:
Program Code
clear all
close all
clc
%IMPORTING DATA FROM FILE
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%LINEAR FIT
% Here gof contains the goodness of fit statistics
[coeff1,gof1] = fit(temperature,cp,'poly1');
figure(1);
plot(coeff1,temperature,cp);
title('Variation of cp with temperature')
xlabel('Temperature [K]')
ylabel('cp [kJ/kmole-K]')
legend('temperature v/s cp','Linear Fit','Location','NorthWest');
%CUBIC FIT
[coeff3,gof2] = fit(temperature,cp,'poly3');
figure(2);
plot(coeff3,temperature,cp);
title('Variation of cp with temperature')
xlabel('Temperature [K]')
ylabel('cp [kJ/kmole-K]')
legend('temperature v/s cp','Cubic Fit','Location','NorthWest');
%IMPROVING THE CUBIC CURVE
% Using matlab curve fit toolbox fit command to normalize the curve and
% make it robust
[fit_poly,gof3] = fit(temperature,cp,'poly4','Normalize','on','Robust','on');
figure(3);
plot(fit_poly,temperature,cp)
title('Variation of cp with temperature')
xlabel('Temperature [K]')
ylabel('cp [kJ/kmole-K]')
legend('temperature v/s cp','Improved Cubic Fit','Location','NorthWest');
%Displaying the errors occuring in all the above three cases
%the Errors discussed here are SSE and RMSE
disp( 'The Sum of Square Error (SSE) is shown in the below figure')
disp( 'in the order "Linear fit","Cubic Fit" and "Best Cubic Fit" respectively')
table(gof1.sse,gof2.sse,gof3.sse)
disp( 'The Sum of Root Mean Square Error (RMSE) is shown in the below figure')
disp( 'in the order "Linear fit","Cubic Fit" and "Best Cubic Fit" respectively')
table(gof1.rmse, gof2.rmse, gof3.rmse)
disp( 'The R Square value is shown in the below figure')
disp( 'in the order "Linear fit","Cubic Fit" and "Best Cubic Fit" respectively')
table(gof1.rsquare, gof2.rsquare, gof3.rsquare)
Output of Program
Command Window Prompt
Conclusion
Following table compares the goodness of curve statistics:
|
sse |
rsquare |
adjrsquare |
rmse |
|
|
Linear Fit |
2163049.08917979 |
0.924877626055183 |
0.92485413563181 |
26.0072215952247 |
|
Cubic Fit |
94272.0205207338 |
0.996725946714054 |
0.996722873447515 |
5.43110278945412 |
|
Improved Cubic Fit |
7202.79125298158 |
0.999749848128431 |
0.99974953494925 |
1.50146420482364 |
- How to make a curve fit perfectly?
- Based upon the type of curve generated, we can use curve fitting toolbox to vary the various types of curves until we get the pefectly represented curve.
- As stated above, we can use following ways:
- Goodness-of-Fit Statistics
- Residual Analysis
- Confidence and Prediction Bounds
- How to get the best fit?
-
A “good fit” might be a model
-
that our data could reasonably have come from, given the assumptions of least-squares fitting
-
in which the model coefficients can be estimated with little uncertainty
-
that explains a high proportion of the variability in your data, and is able to predict new observations with high certainty
-
-
- We can also vary the order of polynomial or increase the intervals to get a good fit.
- What could be done to improve the cubic fit?
- we can use normalise function or robust function in order to improve the cubic fit
References