MATLAB program for curve fitting the data of specific heat within a temperature range
AIM: Write a MATLAB Program to perform curve fitting and understand the parameters affecting the curve fit
OBJECTIVES:
- Write code to fit a linear and cubic polynomial for the Cp data.
- Plot the linear and cubic fit curves along with the raw data points.
- Understand the parameters used to measure the fitness characteristics for both the curves
- Explain the need of curve fitting
- Calculation of goodness of fit of given curve characteristics
GOVERNING EQUATIONS AND LAWS:
- Suppose each of these n points has an individual error (difference in y co-ordinate values)
The error for a general "i" point can be given by the function:
ERROR = `| y ( i ) − f ( x ( i ) ) |` , where `y = f(x)`
- To find Sum of Square of Errors:
`SSE = sum_(i=0)^N Err(i)^2`
- Form of the equation:
`f ( x ) = A 1 + A 2 ⋅ x + A 3 ⋅ x ^2 + ... + A n ⋅ x^(n − 1)`
- To calculate Sum of Square of Regression:
`SSR = sum_(i=0)^N ( f ( x ( i ) ) − M e a n ) ^2`
- To calculate Sum of Square of Total Error:
`S S T = S S R + S S E`
- R-square
This quantity that measures how successful the fit is in explaining the variation of the data. Put another way, 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
`R^2 = (SSR)/{SST)`
- To Calculate Root Mean Squared Error:
`RMSE = sqrt((SSE)/n`, Where n in number of data points
INPUT:
https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view
BRIEF EXPLAINATION:
- What is Curve Fitting ?
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 dataset and parameter values, etc. It is highly effective in the mathematical modelling of some natural processes
- Need of Curve Fitting:
Curve fitting, also known as regression analysis, is used to find the "best fit" line or curve for a series of data points. Most of the time, the curve fit will produce an equation that can be used to find points anywhere along the curve. In some cases, you may not be concerned about finding an equation. Instead, you may just want
to use a curve fit to smooth the data and improve the appearance of your plot
- Polynomial Selection:
This function fits a curve through your data, of the form `y=m0 + m1 * x + m2 * x^2 + m3*x^3 +...+ m9 * x^9`.
The more complex the curvature of the data, the higher the polynomial order required to fit it. There are nodata restrictions associated with this curve fit.
In this program we use third order polynomial for curve fitting to obtain the best fir possible and to avoid much deviation
- Curve-fit functions used:
1) Polyfit :
- How to choose best fit:
Upon finding a curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equation is a representing the datapoints. There are,
- The sum of squares due to error (SSE)
- R-square
- Adjusted R-square
- Root mean squared error (RMSE)
%% CURVE-FITTING
clear all; % to clear the workspace
close all; % to close all plots and diagrams
clc % to clear command window
%% preparing the data into 4 sets
cp_data = load('data'); % this loads the data into given variable
cp = cp_data(:,2); % all rows and second column is alloted to cp
temp = cp_data(:,1); % all rews and first column is alloted to temp
temp1 = temp(1:800); % first 800 elements are alloted to this variable
cp1 = cp(1:800); % first 800 elements are alloted to given variable
temp2 = temp(801:1600); % next 800 elements are alloted to this variable
cp2 = cp(801:1600); % next 800 elements are alloted to this variable
temp3 = temp(1600:2400);
cp3 = cp(1600:2400);
temp4 = temp(2401:3200);
cp4 = cp(2401:3200);
%% curve fit for single set ( linear)
coeff_lin = polyfit(temp,cp,1); % polyfit helps to obtain polynimial coefficients(1)
pred_cp_lin = polyval(coeff_lin,temp); % gives value of above polynomials
%% curve fir for single ser (cubic)
coeff_cub= polyfit(temp,cp,3); % perfoms same as above but it has three degrees
pred_cp_cub = polyval(coeff_cub,temp); % gives value of above polynomials
%% Curve Fit for polynomial curve
% c_p = aT^2 + bT +c
coeff1 = polyfit(temp1,cp1,3); % perfoms the same function of obtaining coef.
predicted_cp1 = polyval(coeff1,temp1);
coeff2 = polyfit(temp2,cp2,3);
predicted_cp2 = polyval(coeff2,temp2); % we obtain the coeffs and value for all data
coeff3 = polyfit(temp3,cp3,3); % sets of the gievn data
predicted_cp3 = polyval(coeff3,temp3);
coeff4 = polyfit(temp4,cp4,3);
predicted_cp4 = polyval(coeff4,temp4);
%% Comparing with original data-set ( cubic)
figure(1)
plot(temp,cp,'LineWidth',3,'color','k'); hold on % plots the raw data on the graph
plot(temp1,predicted_cp1,'Linewidth',2,'color','r') % on same plot different plots
plot(temp2,predicted_cp2,'Linewidth',2,'color','b') % are marked by using different
plot(temp3,predicted_cp3,'Linewidth',2,'color','g') % colors to check to if the curve
plot(temp4,predicted_cp4,'Linewidth',2,'color','y') % good to fit or not with given
title('Comparing using cubic polynimoal function') % width of the curve
xlabel('Temperatire in K') % gives name to x axis
ylabel('Specific Heat in KJ/kg-mol') % gives name to y axis
legend('raw data','curve fit') % gives legend to the plot
%% Curve fit for Linear Curve
coeff1_L = polyfit(temp1,cp1,1); % the same set of procedure id followed as above
predicted_cp1 = polyval(coeff1,temp1);
coeff2 = polyfit(temp2,cp2,1); % but the the degree of curve is 1 in this case
predicted_cp2 = polyval(coeff2,temp2);
coeff3 = polyfit(temp3,cp3,1); % done for all 4 pasrts of data set
predicted_cp3 = polyval(coeff3,temp3);
coeff4 = polyfit(temp4,cp4,1);
predicted_cp4 = polyval(coeff4,temp4);
%% Comparing with original data-set (Linear)
figure(2) % creates a new plot
plot(temp,cp,'LineWidth',3,'color','k'); hold on % these functions do same as
plot(temp1,predicted_cp1,'Linewidth',2,'color','r') % above but in this case
plot(temp2,predicted_cp2,'Linewidth',2,'color','b') % the linear data data is plot
plot(temp3,predicted_cp3,'Linewidth',2,'color','g') % instead of cubic
plot(temp4,predicted_cp4,'Linewidth',2,'color','y')
title('Comparing using linear polynomial function')
xlabel('Temperatire in K')
ylabel('Specific Heat in KJ/kg-mol')
legend('raw data','curve fit')
%% Calculating error and choosing best fit
% for linear data
M = mean(cp);
% Sum of Square error ( is taken 0 initially)
SSE_L = 0;
% Sum of square of regression ( is taken 0 initially)
SSR_L = 0;
for i = length(temp) % iterates through each value of the data
SSE_L = SSE_L + (cp(i) - pred_cp_lin(i))^2; % increment of SSE by given value
SSR_L = SSR_L + (pred_cp_lin(i) - M)^2; % increment of SSR by given value
end
% Sum of Square of total:
SST_L = SSE_L + SSR_L;
R_L = (SSR_L/SST_L)^0.5; % the refer above equations to calculate r-square
fprintf(' the R-square for linear data is %f ',R_L); % displays the value calculated
%% root mean square error for linear data is
RMSE_L = (SSE_L/length(cp))^0.5;
fprintf('and its root mean square error is %f',RMSE_L); % displays the error
%% Calculating error and choosing best fit
% for Cubic data
SSE_C = 0; % SAME STEPS AS ABOVE BUT FOR CUBIC DATA
SSR_C = 0;
for i = length(temp)
SSE_C = SSE_C + (cp(i) - pred_cp_cub(i))^2;
SSR_C = SSR_C + (pred_cp_cub(i) - M)^2;
end
SST_C = SSE_C + SSR_C; % heance from result we could see that error for
R_C = (SSR_C/SST_C)^0.5; % linear data is more than that of cubic fit
fprintf(' The R-square for Cubic data is %f',R_C);
% root mean square error
RMSE_C = (SSE_C/length(cp))^0.5;
fprintf(' and its root mean square error is %f',RMSE_C);
- Failing to allocate the right data in the function may lead to mismatched results:
SOLUTION: It was solved by manual debugging
- Some syntax errors and not declaring the variables:
SOLUTION: by correcting the syntax and taking help of MATLAB docs to rectify the mistakes
OUTPUT:
The output plot for the best fit and to obtain the value of R-square and errors:
CASE 1:-
CASE 2:-
CONCLUSION:
- Hence from above output we could conclude the value of R-square for 'Cubic' is greater than that of the 'Linear' fit. Hence the Cubic fit is more accurate.
- The value of RMSE for Cubic fir is less than the RMSE for Linear Fit.
- We also conclude that the accuracy of the fit could be increased by dividing the data into more and more data-sets.
REFERENCES:
https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
https://in.mathworks.com/help/curvefit/evaluating-goodness-of-fit.html