Curve Fitting using Matlab
AIM: To write code in MATLAB for the linear curve _t and cubic curve _t for the Cp data.
DESCRIPTION:
Curve Fitting
In Regression analysis, curve fitting is the process of specifying the model that provides the best fit to the specific curves in your dataset. Curved relationships between variables are not as straightforward to fit and interpret as linear relationships.
How to choose the best fit?
Upon finding a curve that satisfies the data points there are 4 quantities that help us measure the goodness of fit criteria or how well the equation is representing the data points. There are,
- The sum of squares due to error (SSE)
- R-square
- Adjusted R-square
- Root mean squared error (RMSE)
- Sum of Squares Due to Error
- This statistic measures the total deviation of the response values from the fit to the response values. It is also called the summed square of residuals and is usually labeled as SSE.
- SSE = Sum(i=1 to n){wi(yi - fi)2}
- Here yi is the observed data value and fi is the predicted value from the fit. wi is the weighting applied to each data point, usually wi = 1.
- A value closer to 0 indicates that the model has a smaller random error component, and that the fit will be more useful for prediction.
R-Square
- This statistic 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-square is defined as
- R-square = 1 - [Sum(i=1 to n){wi(yi - fi)2}] /[Sum(i=1 to n){wi (yi - yav)2}] = 1 - SSE/SST
- Here fiis the predicted value from the fit, yav is the mean of the observed data yi is the observed data value. wi is the weighting applied to each data point, usually wi=1. SSE is the sum of squares due to error and SST is the total sum of squares.
- R-square can take on any value between 0 and 1, with a value closer to 1 indicating that a greater proportion of variance is accounted for by the model. For example, an R-square value of 0.8234 means that the fit explains 82.34% of the total variation in the data about the average.
- If you increase the number of fitted coefficients in your model, R-square will increase although the fit may not improve in a practical sense. To avoid this situation, you should use the degrees of freedom adjusted R-square statistic described below.
- Note that it is possible to get a negative R-square for equations that do not contain a constant term. Because R-square is defined as the proportion of variance explained by the fit, if the fit is actually worse than just fitting a horizontal line then R-square is negative. In this case, R-square cannot be interpreted as the square of a correlation. Such situations indicate that a constant term should be added to the model.
Degrees of Freedom Adjusted R-Square
- This statistic uses the R-square statistic defined above and adjusts it based on the residual degrees of freedom. The residual degrees of freedom is defined as the number of response values n minus the number of fitted coefficients m estimated from the response values.
- v = n-m
- v indicates the number of independent pieces of information involving the n data points that are required to calculate the sum of squares. Note that if parameters are bounded and one or more of the estimates are at their bounds, then those estimates are regarded as fixed. The degrees of freedom are increased by the number of such parameters.
- The adjusted R-square statistic is generally the best indicator of the fit quality when you compare two models that are nested – that is, a series of models each of which adds additional coefficients to the previous model.
- adjusted R-square = 1 - SSE(n-1)/SST(v)
- The adjusted R-square statistic can take on any value less than or equal to 1, with a value closer to 1 indicating a better fit. Negative values can occur when the model contains terms that do not help to predict the response.
Root Mean Squared Error
- This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data and is defined as
- RMSE =s = (MSE)½
- where MSE is the mean square error or the residual mean square
- MSE=SSE/v
- Just as with SSE, an MSE value closer to 0 indicates a fit that is more useful for prediction.
How to make the perfect fit?
- By increasing the order of equation used for curve fit.
- By minimizing the error in any regression method.
- By increasing the value of R2 in the R-square method.
How to get the best fit?
- By increasing the order of equation used for curve fit.
- By increasing the value of R2 in R-square method.
What could be done to improve the cubic fit?
- By increasing the order of equation used for curve fit.
- By increasing the value of R2 in the R-square method.
OBJECTIVES
- To get the best fit for the cubic curve using MATLAB.
Figure: Flow of the project
CODE
%Curve fitting
clear all
close all
clc
cp_data = load('data');%loading the file named"data".
temperature = cp_data(:,1);%temperature is in first column
Cp = cp_data(:,2);%values of Cp are given in second column w.r.t. the temperature
%Linear Fit
%cp = a*T + b (equation of line), it is for linear fit
coefficient1 = polyfit(temperature,Cp,1);
predicted_Cp1 = polyval(coefficient1,temperature);
figure(1)
plot(temperature,Cp,'linewidth',5)%plotting original or default data
hold on % holding the figure
plot(temperature,predicted_Cp1,'linewidth',5,'color','b')%plotting the curve fit
%defining the legends of graphs
xlabel('Temperature [K]')
ylabel('Specific Heat at constant pressure [kJ/kmol-K]')
legend('Original Data','Curve Fit')
title('Linear Fit')
%Cubic Fit
%cp = a*T^3 + b*T^2 + c*T + d (equation for cubic fit)
coefficient2 = polyfit(temperature,Cp,3);
predicted_Cp2 = polyval(coefficient2,temperature);
figure(2)
plot(temperature,Cp,'linewidth',3)%plotting default or original data
hold on%holding the figure
plot(temperature,predicted_Cp2,'linewidth',5,'color','g')%plotting the predicted of fitted data
%defining the legends
xlabel('Temperature [K]')
ylabel('Specific Heat at constant pressure [kJ/kmol-K]')
legend('Original Data','Curve Fit')
title('Cubic Fit')
%fourth order Fit
%cp = a*T^4 + b*T^3 + c*T^2 + d*T + e (equation for cubic fit)
coefficient3 = polyfit(temperature,Cp,3);
predicted_Cp3 = polyval(coefficient3,temperature);
figure(3)
plot(temperature,Cp,'linewidth',3)%plotting default or original data
hold on%holding the figure
plot(temperature,predicted_Cp3,'linewidth',5,'color','r')%plotting the predicted of fitted data
%defining the legends
xlabel('Temperature [K]')
ylabel('Specific Heat at constant pressure [kJ/kmol-K]')
legend('Original Data','Curve Fit')
title('fourth order fit')
% For linear fit
summation=0;%initialisation from zero
for m=1:3200%loop will start from first value ends at 3200th value
summation=summation+cp_data(m,2);%for summation
end
mean=summation/3200;%calculation of the mean
SSR=0;%initialisation from zero
for j=1:3200%initialisation from zero
SSR=(predicted_Cp1(m)-mean)^2;%calculation of SSR
end
SSE=0;%initialisation from zero
for n=1:3200%loop will start from first value ends at 3200th value
SSE=(cp_data(n,2)-predicted_Cp1(n))^2;%calculation of SSE
end
SST=SSR+SSE;%calculation of SST
R_square1=SSR/SST%calculation of R^2
% For cubic fit
summation=0;%initialisation from zero
for m=1:3200%loop will start from first value ends at 3200th value
summation=summation+cp_data(m,2);%for summation
end
mean=summation/3200;%calculation of mean
SSR=0;%initialisation from zero
for j=1:3200%loop will start from first value ends at 3200th value
SSR=(predicted_Cp2(m)-mean)^2;%calculation of SSR
end
SSE=0;%initialisation from zero
for n=1:3200%loop will start from first value ends at 3200th value
SSE=(cp_data(n,2)-predicted_Cp2(n))^2;%calculation of SSE
end
SST=SSR+SSE;%calculation of SST
R_square2=SSR/SST%calculation of R^2
%for Fourth order fit
summation=0;%initialisation from zero
for m=1:3200%loop will start from first value ends at 3200th value
summation=summation+cp_data(m,2);%for summation
end
mean=summation/3200;%calculation of the mean
SSR=0;%initialisation from zero
for j=1:3200%loop will start from first value ends at 3200th value
SSR=(predicted_Cp3(m)-mean)^2;%calculation of SSR
end
SSE=0;%initialisation from zero
for n=1:3200%loop will start from first value ends at 3200th value
SSE=(cp_data(n,2)-predicted_Cp3(n))^2;%calculation of SSE
end
SST=SSR+SSE;%calculation of SST
R_square3=SSR/SST%calculation of R^2
Explanation of code
- File is loaded.
- Assign that the Temperature is in first column and Cp is in the second column.
- Using polyfit obtain the coefficient in the regression.
- Using the obtained coefficient in the previous step predicts the value of temperature by calling polyval function.
- Curves are plotted using plot command for the original data set and for predicted data set i.e. curve fit.
- The same procedure is followed for cubic and fourth-order curve fit.
- Value of R2 is calculated for the different curve fits using for loop and using the following relationships:
`SSR = sum_(i=1)^n(f(x(i))-Mean)^2`
`SSE = sum_(i=1)^n(Y(i)-f(x(i)))^2`
`SST=SSR+SSE`
`R^2 = (SSR)/(SST)`
Figure 1: Linear fit
Figure 2:Cubic Fit
Figure 3:Fourth order fit
Figure 4: Output of R2 in the command window.
CONCLUSION
- Using MATLAB 18.0 the cubic fit is done successfully.
- By increasing the order of curve we can get the best fit.
- By approaching the value of R2 to 1 we can get the perfect fit
REFERENCES