Curve fitting
1. linear and cubic polynomial fit
clear all
close all
clc
% Load the dataset
cp_dataset = load(\'data\');
temp = cp_dataset(:,1);
cp = cp_dataset(:,2);
% Curve fit
% Linear fit
figure(1)
ft = fittype( {\'x\', \'2\', \'1\'}, \'independent\', \'x\', \'dependent\', \'y\', \'coefficients\', {\'a\', \'b\', \'c\'} );
[cp_linfit,gof] = fit(temp,cp,ft)
h=plot(cp_linfit)
set(h,\'linewidth\',3,\'color\',\'k\')
hold on
plot(temp,cp,\'linewidth\',3)
xlabel(\'T [K]\')
ylabel(\'cp [KJ/Kmol-K]\')
legend(\'original dataset\',\'linear fit\')
% Cubic polynomial fit
figure(2)
ft= \'poly3\'
[cp_poly3,gof] = fit(temp,cp,ft,\'Normalize\',\'off\')
h=plot(cp_poly3)
set(h,\'linewidth\',3,\'color\',\'c\')
hold on
plot(temp,cp,\'linewidth\',3)
xlabel(\'T [K]\')
ylabel(\'cp [KJ/Kmol-K]\')
legend(\'original dataset\',\'cubic fit\')
2. To make perfect fit
% continuation of the above code
%fit techniques
% 1. centering and scaling of cubic polynomial
figure(3)
ft= \'poly3\'
[cp_poly3,gof] = fit(temp,cp,ft,\'Normalize\',\'on\')
h=plot(cp_poly3)
set(h,\'linewidth\',3,\'color\',\'m\')
hold on
plot(temp,cp,\'linewidth\',3)
xlabel(\'T [K]\')
ylabel(\'cp [KJ/Kmol-K]\')
legend(\'original dataset\',\'normalized cubic fit\')
% 2. cubic interpolation
figure(4)
ft= fittype(\'splineinterp\')
[cp_splineinterp,gof] = fit(temp,cp,ft,\'Normalize\',\'off\')
plot(temp,cp,\'b\',\'linewidth\',6)
hold on
h=plot(cp_splineinterp)
set(h,\'linewidth\',3,\'color\',\'r\')
xlabel(\'T [K]\')
ylabel(\'cp [KJ/Kmol-K]\')
legend(\'original dataset\',\'cubic interpolation\')
In this case I have used two methods,
a) normalized cubic polynomial
b) cubic interpolation method
To determine the best fit we have to estimate the goodness of the fit(gof)
The following values were obtained for the fits I have plotted
1. linear fit-gof
sse: 2.1701e+06
rsquare: 0.9246
dfe: 3197
adjrsquare: 0.9246
rmse: 26.0537
2. cubic fit-gof
sse: 9.4272e+04
rsquare: 0.9967
dfe: 3196
adjrsquare: 0.9967
rmse: 5.4311
3. normalized cubic fit-gof
sse: 9.4272e+04
rsquare: 0.9967
dfe: 3196
adjrsquare: 0.9967
rmse: 5.4311
4. cubic interpolation-gof
sse: 0
rsquare: 1
dfe: 0
adjrsquare: NaN
rmse: NaN
Criteria for the best fit:
-The sum of squares due to error(SSE) must be equal to 0
-R square should lie between 0 and 1 (best fit=1)
-adjusted R squared(adjrsquare) should be less than 1(best fit=1)
-root mean squared error(rmse) should be equal to 0 for best fit
Considering the above criterias and comparing with it my gof values of plots, we can see that \'cubic interpolation\' is the best fit in this case.
*The other alternate easy way is to use the curve fitting tool(cftool) to try various fitting methods and estimate the best one using the gof statistics.