Curve Fitting in Matlab
AIM- To write a Matlab code to fit a linear and cubic polynomial for Cp data and plot the linear and cubic fit curves along with the raw data points.
THEORY-
Curve fitting is the process of constructing a curve or mathematical function that has the best fit to a series of data points . curve fitting can involve interpolation , where an exact fit to the data is required or smoothing in which a smooth function is constructed that approxmately fits the data.
fig 1 a plot of orginal data and curve fit
Methods to evaluate goodness of a fit
1.Graphical- Plotting residuals and prediction bounds are graphical methods that aid visual interpretation.Generally speaking, graphical measures are more beneficial than numerical measures because they allow you to view the entire data set at once, and they can easily display a wide range of relationships between the model and the data. In this method we plot the obtained resultsand then compare with the orginally obtained curve , the amount of deviation from the orginal curve to fitted curve give the goodness aspect. If the fitted curve compleately coincides with the orginal then it is an ideal curve fit or good fit which is just a model.
2.Numerical- These are of four types-
i.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)^n=w_i (y i−yi ) ^2`
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.
ii.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 the ratio of the sum of squares of the regression (SSR) and the total sum of squares (SST). SSR is defined as
`SSR= sum_(i=1)^n w_i ( ˆy i −‾ y ) ^2`
SST is also called the sum of squares about the mean, and is defined as
`SST= sum_(i=1)^n w_i ( y i −‾ y ) ^2`
where SST = SSR + SSE. Given these definitions, R-square is expressed as
`R-SQUARE= SSR div SST =1− SSE div SST`
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.
iii.Degrees of freedom adjudted R-Square-
his 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 is 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)div 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.
iv.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= sqrtMSE`
where MSE is the mean square error or the residual mean square
`MSE= SSE div v`
Just as with SSE, an MSE value closer to 0 indicates a fit that is more useful for prediction.
How to make a curve fit perfectly?
A curve can be fitted perfectly by measuring how much deviation it has gone through from the orginal curve, further for every consecutive change in degree of polynomial.
How to get the best fit?
Best fit can be obtained by following the numerical methods e.g for in the method 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. And for the method sum of squares due to error(SSE) a value closer to 0 indicates that the model has a smaller random error component.
What could be done to improve the cubic fit?
Cubic fit can be improved by scalling the graph obtained and recorrecting it.
Software used- MatlabR2018a
INPUT PROGRAM-
clear all
close all
clc
% preparing data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
% curve fit linear
% a*T+d
coefficients=polyfit(temperature,cp,1)
predicted_cp=polyval(coefficients,temperature)
% comparing
figure(1)
hold on
plot(temperature,cp,'linewidt',2)
xlabel('temperature [K]')
ylabel('specific heat [KJ/Kmol-k]')
plot(temperature,predicted_cp,'linewidt',2,'color','r')
grid on
legend('orginal dataset','linear curvefit')
% curve fit cubic
% a*T^3+b*T^2+c*T+d
coefficients=polyfit(temperature,cp,3)
predicted_cp=polyval(coefficients,temperature)
% comparing
figure(2)
hold on
plot(temperature,cp,'linewidt',2)
xlabel('temperature [K]')
ylabel('specific heat [KJ/Kmol-k]')
plot(temperature,predicted_cp,'linewidt',2,'color','r')
grid on
legend('orginal dataset','cubic curvefit')
Matlab code explanation-
//
clear all
close all
clc
% preparing data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);
Initally the progrma has began using clear all,close all,clc which closes all the previously assigned values and clears the screeen.
After that we have created an array cp_data and assigned the file data using load command , the file data has information of specific heat and temperatures in two columns.
Then mentioned that all the values in column 1 are temperatures and assigned to an array temperature using-temperature=cp_data(:,1);
Further the values of specific heat(cp)are also assigned to an array cp which are of 2nd column in the file data using the command cp=cp_data(:,2);
//
% curve fit linear
% a*T+d
coefficients=polyfit(temperature,cp,1)
predicted_cp=polyval(coefficients,temperature)
Now the curve fit is determined using some euation as polynomial, for inserting a polynomial we use polyfit command and respective coefficients and degree of polynomial is enterd like coefficients=polyfit(temperature,cp,1) as mentioned in code , it has a degree 1 and these are assigne to an array coefficients.
Now using polyval command we find the results of coefficients for the degree of equation we have given, further these values are assigned to an array preducted_cp.
//
% comparing
figure(1)
hold on
plot(temperature,cp,'linewidt',2)
xlabel('temperature [K]')
ylabel('specific heat [KJ/Kmol-k]')
plot(temperature,predicted_cp,'linewidt',2,'color','r')
grid on
legend('orginal dataset','linear curvefit')
Now using the plot command we make plots between the temperature vs specific heat for previously stored data and predicted values of specific heat.
Initally a plot for orginal stored data is made using 'plot(temperature,cp,'linewidt',2)' having line width 2 , corresponding x & y labels are given using the commands 'xlabel ('temperature [K]'),ylabel('specific heat [KJ/Kmol-k]')' .
Further a plot for predicted cp value and temperature is plotted using plot command 'plot(temperature,predicted_cp,'linewidt',2,'color','r')' .
The above two mentioned plots are generated under the name figure 1 , for comparing these two graphs the two plots are generated on same plot.
legend command is used to name the curves as 'orginal dataset','linear curvefit'.
//
% curve fit cubic
% a*T^3+b*T^2+c*T+d
coefficients=polyfit(temperature,cp,3)
predicted_cp=polyval(coefficients,temperature)
% comparing
figure(2)
hold on
plot(temperature,cp,'linewidt',2)
xlabel('temperature [K]')
ylabel('specific heat [KJ/Kmol-k]')
plot(temperature,predicted_cp,'linewidt',2,'color','r')
grid on
legend('orginal dataset','cubic curvefit')
Further plots are generated using cubic polynomial.
The steps and commands followed are same as that we used for a linear fit just the degree of polynomial is changed.
OUTPUT- The following are the outputs obtained from the above Matlab code.
fig 2 plot comparing linear curvefit and orginal dataset values
fig 3 plot comparing cubic curvefit and orginal data set values
fig 4 plot comparing 4th degree polynomial and orginal data set values