Curve Fitting using MATLAB
Curve-Fitting using MATLAB
Objective: To define a MATLAB script that performs linear and cubic curve fits for a given data series. To compute the associated errors in the predicted values of the dependent variable and finally, to explore means to improve the accuracy and precision of the curve fit to attain perfect and best fits.
Method:
- ‘Load’ command is utilized to load the data series on to the MATLAB working directory
- Subsequently, data series values are assigned to the independent and dependent variables of temperature and specific heat, respectively.
- Next, the original data series is plotted and ‘polyfit’ & ‘polyval’ commands were utilized to define data sets for a curve fit.
- These are then plotted on the same figure. This is performed for linear and cubic curve fits.
- Herein the function is utilized to determine the mean and estimated standard error in predicted values for the curve fit as well.
- Next, the mathematical relationships for computing sum of squared errors and the sum of square of regression are defined.
- Subsequently, the values for goodness of fit parameters, SSE, R-square, and root mean squared error (RMSE) are defined.
- Thereafter, these values are compared with values generated through the ‘polyfit’ and ‘polyval’ commands.
- Finally, MATLAB toolbox curve fitting commands 'fit' is utilized to improve the cubic fit by normalizing a higher order Quartic -function.
- Goodness of fit parameters are also generated to showcase the improvement in the curve fit.
Data & Analysis:
Table 1: Goodness of Fit Parameters
|
Curve Fit |
Sum of Squared Error (SSE) |
R-Square |
RMSE |
|
|
Computed |
|From Function |
|||
|
Linear |
2.1630e+06 |
0.1944 |
25.9991 |
26.0153 |
|
Cubic |
9.4272e+04 |
0.8472 |
5.4277 |
5.4345 |
|
Improved Polynomial (Quartic) |
7.2028e+03 |
0.9997 |
1.5015 |
|
From the figure above, it is clear to be seen that the cubic fit curve is a more of a perfect fit encompassing more of the data set for the original curve fit, while mimicking the trend. The Cubic curve also scores better with the goodness of fit parameters as depicted in table 1, boasting a Sum of Squared Errors (SSE) less by a magnitude of two when compared to a linear fit. The table depicts that the cubic fit is the better fit in predicting the dependent variables by being better potentiated in encompassing the variability the predicted or extrapolated values within a reasonable degree of accuracy. This can be substantiated in the R-square value of 0.8472 for the cubic fit, in comparison to the value of 0.1944 for the linear fit which is obviously incapable of correcting accurate prediction. The RSME value of the two re-affirm the notion of cubic being the better fit with values of 25.9991 and 5.4277, linear and cubic, respectively.
Curve Improvement:
Higher order polynomial fits, though better at encompassing the original data set with the apparently perfect fit to original data series, however outside the range of the data set, the higher order polynomial can be quite oscillatory and potentially misleading by magnitudes of error that are prominent from a mathematical point of view. To tackle said issues, a means of fit improvement for the cubic function would be to normalize the curve to a mean value of zero and utilizing a higher order quartic function which would be better poised to mimic the transitions or oscillation in magnitude. Furthermore, the robust least mean square commands are utilized by employing bi-square setting using the robust-on commands. This makes the curve fit more robust in capturing the variability of the data and thus better fits for predicting the values of the dependent variable with robustness. The outcome of said methodology can be seen in the figure above which boasts a more perfect fit to the original data set. The goodness of fit parameters, as shown below, also re-affirm this claim with vastly improved values for SSE, R-square, and RSME values. Thus, boasting a more accurate and precise fit to the original data set. With, SSE dropping by another magnitude, R-square value approaching near perfect-fit, and the RSME dropping roughly by magnitude 3.5 when compared to the cubic-fit.
An alternate and more robust methodology (not employed in this report) could be to utilize split-wise curve fit to individual sections of the original data set and subsequently employing spline smoothness. However, the best methodology would be to utilize the MATLAB in-built Curve-Fitting toolbox which allows for the manipulation of the curve fit with respect to the degree of freedom, normalization, robustness, spline smoothing, and non-linear regression within the curve-fit app.
Code:
%% MATLAB for M.E.
% Week 4: Curve Fitting
% Authored By Amandeep Singh
clc
close all
clear all
%% PURPOSE
% To define a MATLAB script that performs the task of curve-fitting for a
% given data series [Cp(T)] for specfic heat constanto of pressure as a funtion of temperature.
% To compute the Error associated with the variability in the predicted and
% tabulated values of the specific heat constant of pressure.
% Explore means to improve the curve fit for a cubic funtion
%% APPROACH
% Use Load command to load the data series on to the MATLAB working
% directory. Subsequently, assign the data series values to variables of
% temperature (x) and specific heat (y).
% Next, plot the original data series and use the polyfit and polyvals
% commands to define data set for a curve fit and plot on the same figure.
% This is performed for linear and cubic curve fits. Herein the function is
% utilized to determine the mean and estimated standard error in predicted
% values for the curve fit as well.
% Next, define the mathematical relationship for computing sum of squared
% errors and the sum of square of regression. Subsequently, computed are
% the values for goodness of fit parameters, SSE, R-square, and root mean
% squared error (RMSE).
% These values are compared with values generated by the polyfit and
% polyval commands.
% Finally, MATLAB toolbox curve fitting commands 'fit' is utilized to
% improve the cubic fit by normalizing a higher order quatic function.
% Goodness of fit parameters are also generated to showcase the improvement
% in the curve fit.
%% LOADING FILE
% Loading Data file into Work Space
cp_data = load('data');
temp = cp_data(:,1); %assiging temperature values from data file
cp = cp_data(:,2);
%% CURVE FITTING
%LINEAR
[coeff_linear,SL,MUL] = polyfit(temp,cp,1); %MUL hold values of mean and STD of data series
[cp_linear,DL] = polyval(coeff_linear,temp,SL,MUL); %DL here is an estimation of STD of predicted values
mean_linear = MUL(1);
% CUBIC
[coeff_cubic,SC,MUC] = polyfit(temp,cp,3);
[cp_cubic,DC] = polyval(coeff_cubic,temp,SC,MUC);
mean_cubic = MUC(1);
%% PLOTTING
figure (1)
hold on
plot(temp,cp,'linewidth',2)
plot(temp,cp_linear,'linewidth',2)
plot(temp,cp_cubic, 'linewidth', 2, 'color','g')
title(' Cp(temperature) ')
xlabel(' Temperature (K) ')
ylabel(' Specific Heat (kJ/kmole-K)')
legend( 'original data-set','Linear Fit','Cubic Fit')
%% DETERMINING ERROR
%SUM OF SQUARE ERROR (SSE)
Error_L = (cp-cp_linear).^2;
SSE_L = sum(Error_L); % Sum of Error square linear
Error_C = (cp - cp_cubic).^2;
SSE_C = sum(Error_C); % Sum of Error square cubic
disp('The computed sum of squared Error for Linear fit is :')
disp(SSE_L)
disp('The computed sum of squared Error for Cubic fit is :')
disp(SSE_C)
% Calculating R^2 R^2 = sum(SSR)/sum(SST)
SSR_L = (cp_linear - mean_linear).^2; %Sum of square of regression for Linear
SST_L = SSR_L + SSE_L;
disp(' The R-Square value for the Linear Fit is :')
R_square_L = sum(SSR_L)/sum(SST_L)
SSR_C = (cp_cubic - mean_cubic).^2; %Sum of square of regression for Cubic
SST_C = SSR_C + SSE_C;
disp(' The R-Square value for the Cubic Fit is :')
R_square_C = sum(SSR_C)/sum(SST_C)
%Determining Standard Deviation or Root Mean Squared Error (RSME)
% RSME = sqrt(SSE/n)
%Linear
disp( 'The Root Mean Squared Error for Linear fit is :')
RMSE_L = sqrt(SSE_L/length(temp))
%Cubic
disp( 'The Root Mean Squared Error for Cubic fit is :')
RMSE_C = sqrt(SSE_C/3200)
%% IMPROVING THE CUBIC CURVE
% Using matlab curve fit toolbox fit command to normalize the curve and
% make it robust
[fit_poly,gof] = fit(temp,cp,'poly4','Normalize','on','Robust','on')
figure (2)
plot(fit_poly,temp,cp)
References:
Thanuj Singaravelan. (2018). Curve Fitting and Criterion to Choose Best fit? Available: https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860. Last accessed 13th Jul 2019.