CURVE FITTING
ABSTRACT:
Curve fitting means to find a mathematical expression which represents a line or curve that fit the data the best. The best means with the minimum error or we can say it represents the best approximation for the data. Interpolation and least square methods are two of the most widely used techniques
One of the most interesting problems in supervised machine learning is to find the best fit for the existing data which called regression. Suppose you have three points. Using a polynomial with degree two you can fit the curve to your point. Generally it is not a proper way. It may cause over-fitting. It means that the generalization is not considered here. In other words the error of the chosen model will be high when the model is applied for a new data. Therefore, validation techniques such as cross validation are used to solve this problem. In cross validation, the data is split into k folds (e.g. 10 folds). Then, the first fold is used to validate the model. After that, the next fold is used for validation and so on. Finally the mean of errors is calculated. The model with lower mean error is chosen.
The best curve fit is an interpolation. The error will be zero. There are an infinite number of such exact interpolatory models. So the phrase "the best curve fit" is meaningless.
THE CODE:
clear all
close all
clc
% loading the data set
cp_data = load('data');
temperature = cp_data(:,1);
specific_heat = cp_data(:,2);
% Curve fit
% specific_heat = a*T + b linear polynomial
co_effs1 = polyfit(temperature,specific_heat,1);
predicted_cp_data1 = polyval(co_effs1,temperature);
% specific_heat = a*T^2 + b*T + c cubic polynomial
co_effs2 = polyfit(temperature,specific_heat,3);
predicted_cp_data2 = polyval(co_effs2,temperature);
% specific_heat for 5th degree polynomial
co_effs3 = polyfit(temperature,specific_heat,5);
predicted_cp_data3 = polyval(co_effs3,temperature);
% Curve fit comparison
figure(1)
plot(temperature,specific_heat,'linewidth',5)
hold on
plot(temperature,predicted_cp_data1,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original Dataset', 'Curve Fit')
figure(2)
plot(temperature,specific_heat,'linewidth',5)
hold on
plot(temperature,predicted_cp_data2,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original Dataset', 'Curve Fit')
figure(3)
plot(temperature,specific_heat,'linewidth',5)
hold on
plot(temperature,predicted_cp_data3,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original Dataset', 'Curve Fit')
hold off
OBSERVATIONS:
THE GRAPH:
I have not evaluated the goodness of fit (by coding not using the inbuilt function)
Also, I have not estimated errors, in curve fitting, the error is estimated in two ways (by coding not using the inbuilt function).
Those are
The sum of squares due to error (SSE)
Root mean squared error (RMSE)
Also, blindly increasing the degree of the polynomial will not result in a better fit in all cases. The best way to proceed then is to divide the data points in subregions and curve fit for each region separately.
Also, exploring on the fit( ) command and curve fitting toolbox in matlab is interesting, this provides us with a plethora of options in optimizing a fit without using multiple equations and also calculates the fit characteristics.