Curve Fitting for a given set of Data
AIM
To fit a given set of data by linear and cubic polynomial
OBJECTIVES
- To plot linear and cubic fit curves along with raw data points
- To measure the goodness of the fit
THEORY
Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed data-set and parameter values, etc. It is highly effective in the mathematical modeling of some natural processes.
How to measure goodness of fit
The goodness of the fit is measured using the R-square method.
Assume that the data points are (x1,y1) , (x2,y2) , (x3,y3) , ... (x(n),y(n)) and the curve fit for this set of points will be y = f(x).
If each of these n points has an individual error(i.e difference in y-values), then in general the error can be given by
And a quantity SSE (Sum of squared errors) can be described as
If the mean is taken for all the n number of data
And the SSR (Sum of squares of Regression), where Regression is difference between the value of the function at a given point and the Mean.
Now, the term SST is defined as
The quantity R-square that measures how successful the fit is in explaining the variation of the data. R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.
In layman's terms is that the higher the value of R-square the better the fit, the lower it is the lousier the fit.
R-square is defined as
R-square ranges from a value of 0 to 1
- 0 for the worst possible fit for the data set
- 1 for the best possible fit for the data set
So, closer the value to 1,better the fit.
INPUT DATA
The Temperature-Specific Heat data is provided in the link
https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view
CODE
Programming Language used- MATLAB
%Code for linear and cubic fit for a specific heat data
clear all
close all
clc
%Preparing the data
%Tempertaure vs Specific heat data
sp_data=load('data');
%Extracting Temperature data
temperature=sp_data(:,1);
%Extracting Specific heat
sp=sp_data(:,2);
%Curve fitting
%taking the linear variation of specific heat and temperature
coef1=polyfit(temperature,sp,1);
%taking the cubic variation of specific heat and temperature
coef2=polyfit(temperature,sp,3);
%predicting the specific heat by taking linear variation
predicted_sp1=polyval(coef1,temperature);
%predicting the specific heat by taking cubic variation
predicted_sp2=polyval(coef2,temperature);
%Comparing curve fit and plotting it against original data
subplot(1,2,1)
hold on
plot(temperature,sp,'linewidth',3)
plot(temperature,predicted_sp1,'linewidth',3)
xlabel('Temperature(K)')
ylabel('Specific Heat(kJ-mol/K)')
legend('Original data','Predicted data')
title('Linear variation of predicted specific heat with temperature')
subplot(1,2,2)
hold on
plot(temperature,sp,'linewidth',3)
plot(temperature,predicted_sp2,'linewidth',3)
xlabel('Temperature(K)')
ylabel('Specific Heat(kJ-mol/K)')
legend('Original data','Predicted data')
title('Cubic variation of predicted specific heat with temperature')
%Using R-square method to find goodness of fit
%Mean value of the specific heat(sp) from data
M=mean(sp);
%For linear fit
%Sum of squares of errors
SSE_L=0;
%Sum of squares of regression
SSR_L=0;
for i=1:length(sp)
SSE_L=SSE_L+((sp(i)-predicted_sp1(i))^2);
SSR_L=SSR_L+((predicted_sp1(i)-M)^2);
end
%Total sum of squares
SST_L=SSE_L+SSR_L;
%Calculating R_square
R_square_L=SSR_L/SST_L;
fprintf('The value of R_square for the linear fit is %f \n',R_square_L);
%For cubic fit
%Sum of squares of errors
SSE_C=0;
%Sum of squares of regression
SSR_C=0;
for i=1:length(sp)
SSE_C=SSE_C+(sp(i)-predicted_sp2(i))^2;
SSR_C=SSR_C+(predicted_sp2(i)-M)^2;
end
%Total sum of squares
SST_C=SSE_C+SSR_C;
%Calculating R_square
R_square_C=SSR_C/SST_C;
fprintf('The value of R_square for the cubic fit is %f\n',R_square_C);
OUTPUT
Curve fit
Goodness of fit
EXPLANATION
Code-
The functions ‘polyfit’ and ‘polyval’ are used to develop linear and cubic functions for Specific heat against Temperature from the given data.
These functions were plotted with the given raw data values and the plots were obtained.
The goodness of curve fit is obtained by the R-square method.
Output-
The plots are obtained and curve fit is observed. The goodness of fit is printed. From the plot and the results/values of R-square it was found out that cubic fit is a better fit.
ERROR ENCOUNTERED
SSE_C and SSR_C were not initialised to 0.
CONCLUSION
So, the curve fitting of the given data was done and it was found that cubic fit is a better fit than linear fit as it has a value of R-square of 0.996726.
To improve the cubic fit, we can use other degree polynomial for curve fitting for different intervals of value of data.
REFERENCE
https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860