Curve Fitting
Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points. By curve fitting we can mathematically construct the functional relationship between the observed dataset and the parameter values.
Assume data points (x1,y1),(x2,y2),....,(xn,yn) and assume best fit for set of these points as y = f(x) under which th equation of the curve will be the best is possible as follows :
Now,
Code to fit a linear and cubic polynomial for the Cp data with step wise explaination of the code.
%The coding started with the usual functions
clc
clear all
close all
%Preperation of the data/the inputs were,
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);
%Curve fit for the original data points
co_effs = polyfit(temp,cp,2);
predicted_cp = polyval(co_effs, temp);
%For linear curve
co_effs1 = polyfit(temp,cp,1);
predicted_cp1 = polyval(co_effs1, temp);
%For cubic curve
co_effs2 = polyfit(temp,cp,3);
predicted_cp2 = polyval(co_effs2, temp);
%Comparing the linear and polynomial curves with the original curve
plot(temp, cp, 'linewidth',3)
hold on
plot(temp, predicted_cp1, 'linewidth',3)
plot(temp, predicted_cp2, 'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original Dataset','Linear Curve','Cubic Curve')
The results and the graph for the above code was,
Now, for the above graph, it is seen that the curve is not perfectly fit.
So, upon finding a curve which satisfies the data points, the four steps that must be taken in consideration are,
- Sum of squares due to error (SSE)
- R-Square
- Adjusted R-Square
- Root mean square error (RMSE)
If the R-Square value is between 0.9500 to 1.0000, then it is considered as best fit.
R-Square :- It is a quantity that measures how successfully the fit is explaining the variation of the data when it seems that the higher the value of R-Square we obtain the better fit and vice versa.
It ranges from a value of 0 to 1.
The value of R-square can be calculated by,
If y = f(x) is the fit curve then SSR is defined as follows,
SST = SSR + SSE
Thus, we get,
Where, 'n' is the total no. of data points available.
Thus in order to do so, i.e. finding the best fit, we do splitting of the polynomial.
Thus, the program for linear and cubic polynomial with splitting and step wise explaination is as follows,
%The code initially begins with the parameters,
clc
clear all
close all
%Preperation of the data/ the inputs factors are,
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);
x = mean(cp);
n = 8;
%For linear curve fit,
co_effs1 = polyfit(temp,cp,1);
predicted_cp1 = polyval(co_effs1, temp);
%split
a = 1;
b = 3200/n;
for i = 1:n
co_effs1_split = polyfit(temp(a:b),cp(a:b),1);
predicted_cp1_split(a:b) = polyval(co_effs1_split,temp(a:b));
a = b+1;
b = b + (3200/n);
end
plot(temp,cp,'linewidth',3,'color','b')
hold on
plot(temp,predicted_cp1_split,'linewidth',3,'color','r')
%calculating R square for linear fit for the split
SSR1 = 0;
SSE1 = 0;
for j = 1:length(cp)/n
SSR1 = SSR1 + (predicted_cp1_split(j)-x).^2;
SSE1 = SSE1 + (cp(j)-predicted_cp1_split(j)).^2;
Rsquare1 = SSR1/(SSR1 + SSE1);
RMSE1 = (SSE1/length(cp)).^0.5;
end
xlabel('Temperature [K]');
ylabel('Specific Heat [KJ/Kmol-K]');
%For cubic curve fit,
co_effs2 = polyfit(temp,cp,3);
predicted_cp2 = polyval(co_effs2, temp);
%split
a = 1;
b = 3200/n;
for i = 1:n
co_effs2_split = polyfit(temp(a:b),cp(a:b),3);
predicted_cp2_split(a:b) = polyval(co_effs2_split,temp(a:b));
a = b+1;
b = b + (3200/n);
end
plot(temp,predicted_cp2_split,'linewidth',3,'color','g')
%calculating R square for linear fit for the split
SSR2 = 0;
SSE2 = 0;
for j = 1:length(cp)/n
SSR2 = SSR2 + (predicted_cp2_split(j)-x).^2;
SSE2 = SSE2 + (cp(j)-predicted_cp2_split(j)).^2;
Rsquare2 = SSR2/(SSR2 + SSE2);
RMSE2 = (SSE2/length(cp)).^0.5;
end
xlabel('Temperature [K]');
ylabel('Specific Heat [KJ/Kmol-K]');
legend('original data','linear curve fit','cubic curve fit')
The results and the graph for the above code was,
As from the above result, we can see that the R-square obtained for both linear and cubic ploynomials are 0.9999 and 1.0000 resp. Thus the above code results into the best curve fit.
How to make a curve fit perfectly :
We can obtain a perfect fit by:
- Breaking a continuous curve into series of line segments
- Using mathematical polynomials of 1st or 2nd degree
In order to make the perfect fit, we must consider the error estimates as well. Perfect fit means, the curve should fit the original curve without showing any error. Perfect fit can always be a best fit but not vice versa. To obtain the perfect fit, we can use splitwise method.
How to get the best fit :
In order to obtain the best fit of the curve, we should firstly go for the geometric fitting and then after analyzing the data, we should opt for the numerical values of the polynomial equations.
What could be done to improve the cubic fit :
To improve the cubic fit, we can decrease the order of the polynomial from 3 to 2 and use the split method to improve the fit. Centring and scaling transformation improves the numerical properties of both the ploynomial and fitting algorithm.
Errors faced during programming :
1. Unrecognized function or variable 'predicted_cp_split' :
There was an unrecognized function or variable i.e. 'predicted_cp_split' as in the split method code it was mentioned as 'predicted_cp1_split' for the linear curve. Thus, to clear the error the mistake was rechecked and corrected.
2. Unrecognized function or variable 'predicted_cp_split' :
Again the same error was faced while finding the SSR1. Thus, again it was rechecked and corrected.
3. Error using load :
An error was occured as the function cp_data was called by load('datacurve') and the original file was stored by the name 'data'. So, in order to fix the error, 'datacurve was replaced with 'data'.
4. Warining that polynomial is badly conditioned :
The warning was occured as the degree of the polynomial was increased from 2 to 3. If needed to remove this warning, we can reduce the degree back to 2.
References :
- www.wikipedia.org
- https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
- Matlab Mathworks Help