CURVE FITTING IN MATLAB
CURVE FIT
AIM :
To prepare a code to fit the linear and the cubic polynomial for the Cp data and to study the goodness of it for Curve Fitting.
THEORY :
- Curve fitting, also known as regression analysis, is used to find the best fit line or curve for a series of datapoints. It is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data.
- A polynomial function is a function that can be expressed in the form of a polynomial and is generally represented as P(x). The highest power of the variable of P(x) is known as its degree.
- Degree of a polynomial function is very important as it tells us about the behaviour of the function P(x) when x becomes very large.
Types of Polynomial Functions -
- Linear Polynomial Function: P(x) = ax + b
- Quadratic Polynomial Function: P(x) = ax^2+bx+c
- Cubic Polynomial Function: ax^3+bx^2+cx+d
- To measure the fitness characteristics or to choose the best fit –
To measure the goodness of fit criteria or how well the equation is a representing the datapoints there are 4 quantities mainly used such as ,
- Sum of squared error (SSE)
- R-square
- Adjusted R-square
- Root mean squared error (RMSE)
- Sum of squared error (SSE) –
In sum of squared error we subtract the a point of function y = f(x) at x = x(i) from a point y(i) which gives the vertical error and then we take a square of it.
- R-square –
- R square is the quantity that measures how successful the fit is in explaining the variation of the data. Higher the value of R-square the better the fit, the lower it is the lousier the fit. R-square typically, ranges from a value of 0 to 1. In which 0 for the worst possible fit and 1 for the best fit for the data set.
- It is a ratio of sum of squares of the regression (SSR) and SST.
`R^2 = (SSR)/(SST)`
where,
SST is given by the summation of SSR">SSR and SSE.
`SST = SSR + SSE`
- Adjusted R-square –
Adjusted R-square disregards the infinitesimally small increments to R-square, and only considers the variables which actually contribute to the output of the system in a drastic way. In short, it is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variables.
- Root mean squared error (RMSE)
It is an estimate of the standard deviation of the random component in the data, and is defined as,
`RMSE = sqrt((sum_(i=1)^n(Y(i)-f(x(i)))^2)/n) = sqrt((SSE)/n)`
- How to make a curve fit perfectly?
- Higher the value of R-square the better the fit, the lower it is the lousier the fit. To make a curve fit perfect we need to reduce the errors to exactly 0 and the R square equal to 1.
- How to get the best fit?
- To get the best we can change the polynomial order or use the splitwise technique in which we give a 1st set of input data some polynomial order and remaining data as 2nd set another different polynomial order. Also, we can examine the sum of squares due to error (SSE) and the adjusted R-square statistics to help determine the best fit. The SSE statistic is the least-squares error of the fit, with a value closer to zero indicating a better fit.
- What could be done to improve the cubic fit?
- To improve the cubic fit we can use the split method by reducing the some ranges, separating the curves and then joining them respectively.
GOVERNING EQUATIONS :
- Sum of squared error (SSE)
Mathematically, it is defined as,
`SSE = sum_(i=1)^n(Y(i)-f(x(i)))^2`
- R-square
`R^2 = (SSR)/(SST)`
where,
If y=f(x)"> y=f(x) is the fit curve then the SSR is defined as,
`SSR = sum_(i=1)^n(f(x(i))-Mean)^2`
- Adjusted R-square
Adjusted R-square is given by,
`R_(adjust)^2=1- [((1-R^2 )(n-1))/(n-k-1)]`
- Root mean squared error (RMSE)
RMSE is defined by the equation,
`RMSE = sqrt((sum_(i=1)^n(Y(i)-f(x(i)))^2)/n) = sqrt((SSE)/n)`
OBJECTIVES OF THE PROJECT:
- To write a code and plot the Linear and cubic fit curves along with the raw data points.
- To study the parameters used to measure the fitness characteristics for both the curves.
MAIN PROGRAM CODE -
clear all
close all
clc
% Preparing the data
Cp_data = load('data'); % Loading the data for Cp and Temp...
Temperature = Cp_data(:,1); % Saving the temperature values of all rows from 1st column
Cp = Cp_data(:,2); % Saving the Specific heat (Cp) values of all rows from 1st column
% Curve Fit for Linear polynomial
% Cp = a*T + b
% Cp = a*T^2 + b*T + c
coefficients = polyfit(Temperature,Cp,1); % Using polyfot for the curve fit and to get the coefficients
Predicted_Cp = polyval(coefficients,Temperature); % Using polyval for the curve fit and to get the Predicted_Cp
% Comparing the Curve Fit with Original data
figure(1)
plot(Temperature,Cp,'linewidth',3) % Plotting the Temperature vs Cp figure for original data
hold on
plot(Temperature,Predicted_Cp,'linewidth',3,'color','g') % Plotting the Temperature vs Predicted_Cp for linear polynomial
title('CURVE FITTING for Linear Polynomial')
xlabel('Temperature [k]') % Writing the labels for X axis i.e. Temnperature
ylabel('Specific Heat (Cp) [kJ/Kmol-K]') % Writing the labels for Y axis i.e. Specific heat
legend('Original Dataset',"Curve Fit")
% Curve Fit for Cubic polynomial
% Cp = a*T^3 + b*T^2 + c*T + d
coefficients_cubic = polyfit(Temperature,Cp,3);
Predicted_Cp_cubic = polyval(coefficients_cubic,Temperature);
% Comparing the Curve Fit with Original data
figure(2)
plot(Temperature,Cp,'linewidth',3) % Plotting the Temperature vs Cp figure for original data
hold on
plot(Temperature,Predicted_Cp_cubic,'linewidth',3,'color','r') % Plotting the Temperature vs Predicted_Cp for Cubic polynomial
title('CURVE FITTING for Cubic Polynomial')
xlabel('Temperature [k]') % Writing the labels for X axis i.e. Temnperature
ylabel('Specific Heat (Cp) [kJ/Kmol-K]') % Writing the labels for Y axis i.e. Specific heat
legend('Original Dataset',"Curve Fit")
% Plotting and Comparing the Linear and Cubic fit with orginal data
figure(3)
plot(Temperature,Cp,'linewidth',3,'color','k') % Plotting the Temperature vs Cp figure for original data
hold on
plot(Temperature,Predicted_Cp,'linewidth',2.5,'color','g') % Plotting the Temperature vs Predicted_Cp for Linear polynomial
hold on
plot(Temperature,Predicted_Cp_cubic,'linewidth',2.5,'color','r') % Plotting the Temperature vs Predicted_Cp for Cubic polynomial
title('CURVE FITTING for Linear and Cubic Fit ')
xlabel('Temperature [k]')
ylabel('Specific Heat (Cp) [kJ/Kmol-K]')
legend('Original Dataset','LINEAR Curve fit','CUBIC Curve fit')
GENERATED PROGRAM CODE -
This is the generated code after using center and scale method for goodness of curve fit by using Curve fitting toolbox.
function [fitresult, gof] = createFits(Temperature, Cp)
% CREATEFITS(TEMPERATURE,CP)
% Create fits.
% Data for 'Linear fit' fit:
% X Input : Temperature
% Y Output: Cp
% Data for 'cubic curve fit 2' fit:
% X Input : Temperature
% Y Output: Cp
% Output:
% fitresult : a cell-array of fit objects representing the fits.
% gof : structure array with goodness-of fit info.
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 08-Jun-2020 20:17:41
%% Initialization.
% Initialize arrays to store fits and goodness-of-fit.
fitresult = cell( 2, 1 );
gof = struct( 'sse', cell( 2, 1 ), ...
'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );
%% Fit: 'Linear fit'.
[xData, yData] = prepareCurveData( Temperature, Cp );
% Set up fittype and options.
ft = fittype( 'poly1' );
% Fit model to data.
[fitresult{1}, gof(1)] = fit( xData, yData, ft, 'Normalize', 'on' );
% Plot fit with data.
figure( 'Name', 'Linear fit' );
h = plot( fitresult{1}, xData, yData );
legend( h, 'Cp vs. Temperature', 'Linear fit', 'Location', 'NorthEast' );
% Label axes
xlabel Temperature
ylabel Cp
grid on
%% Fit: 'cubic curve fit 2'.
[xData, yData] = prepareCurveData( Temperature, Cp );
% Set up fittype and options.
ft = fittype( 'poly3' );
% Fit model to data.
[fitresult{2}, gof(2)] = fit( xData, yData, ft, 'Normalize', 'on' );
% Plot fit with data.
figure( 'Name', 'cubic curve fit 2' );
h = plot( fitresult{2}, xData, yData );
legend( h, 'Cp vs. Temperature', 'cubic curve fit 2', 'Location', 'NorthEast' );
% Label axes
xlabel Temperature
ylabel Cp
grid on
CALCULATION OF GOODNESS OF FIT using CURVE FIT TOOLBOX :
- For linear Curve fit -
Here, we had calculated for the goodness of fit for linear polynomial function for curve fit using center and scale method in which polynomial has degree 1 and the R-square value is 0.92 which fine but not best as there are some SSE errors present.
- For Cubic Curve fit -
Here, we had calculated for the goodness of fit for Cubic polynomial function for curve fit using center and scale method in which polynomial has degree 3 and the R-square value is 0.99 approx.. to 1 which is a best fit and also it has minimum SSE.
STEPWISE EXPLANATION OF THE CODE :
- We start the code with regular usual commands by using clear all ; close all ; clc ; for clearing the windows with previous data and closing any previous figures/plots genearted.
- Prepare the data in which we use the load command to load the file named data and store it in a variable Cp_data. Cp_data has 2 columns 1st for temperature and second for Specific heat.
- After loading the data file we store all the temperature values from the first column of Cp_data by using Cp_data(:,1) which means all the rows and the 1st column for the matrix. Similarly, we save the Specific heat Cp values from the second column of the Cp_data.
- First we are preparing and doing the curve fit for the linear polynomial in which polyfit and polyval command is used.
- In Matlab, polyfit command is used for the polynomial curve fitting in which here we give the input for x as temperature , y as Cp , and n the degree of polynomial as 1 for the linear polynomial function. Thus, we get the coefficients of the polynomial after executing this polyfit command.
- After, this we had used the polyval command which is used for polynomial evaluation in which inputs given for polynomial by coefficients, and for x as temperature and saved it in the variable Predicted_Cp .
- Next, we are comparing the data with the curve fit in which we had used the plot command for plotting the temperature and Cp for original data set and again plot command but, used for plotting the temperature and the Predicted_Cp for the Linear Curve fit respectively.
- Same procedure had been followed in the next steps in the code for Comparing the Cubic Polynomial curve fit except the only point is to change the n degree of the polynomial to 3 in the polyfit command as for the cubic polynomial function.
- In the final step we had plotted the linear and the Cubic polynomial against the Original data and studied the error and the goodness of the fit.
- To measure the fitness characteristics for both the curves and to study the goodness of fit and the error we had used the Curve fitting toolbox by using cftool command for the setup and given the input parameters and generated the code for the best fit or goodness of fit in which they used the center and scale method respectively.
OUTPUTS -
- For Linear curve Fit -
In the above figure we get the output for the plot for linear curve fit compared with respect to Orginal data set in which the accuracy of the curve is low as to reach the demand for the original data as linear curve fit polynomial function has very low degree of 1.
- For Cubic curve Fit -
In the above figure we get the output for the plot for cubic curve fit compared with respect to Orginal data set in which the accuracy of the curve is high as to reach the demand for the original data as Cubic curve fit polynomial function has a degree of 3.
- Plot for Original data, Linear Curve and Cubic Curve Fit -
In the above figure we get the output for temperature vs Specific heat (Cp) for orginal data, Linear curve fit and the Cubic curve fit. We can compare the linear curve fit, cubic curve fit with respect to orginal data set and know the results graphically that the cubic curve fit is more accurate and the best fit as compared to linear curve fit as the degree of polynomial is high and errors has diminished in the Cubic curve fit.
Output for goodness of fit for linear curve fit Output for goodness of fit for Cubic Curve fit
Here, in the above results we can clearly see that the linear curve fit has R-square value equal to 0.92 and the Cubic curve fit has the R-square value equal to 0.99 . Thus, Cubic curve fit is a best fit as it is more accurate having better characteristics with having high R-square value equal to 0.99 giving a good curve fit respectively.
Errors faced while programming :
In command window error shows about Polynomial is badly conditioned as we increase the degree of polynomial.
Steps taken to overcome the Errors Faced :
We can reduce the degree of polynomial or the best way is to use Center and scale method. So, here we had used center and scale method by using Curve Fit toolbox for the goodness of fit.
Screenshots showing errors in command window -
CONCLUSION -
- Hence, we had sucessfully coded and plotted the curve fit for linear, cubic polynomial function and compared it to the original data set. Also, we had studied the various method used for CURVE FITTING and analyzed the curve fit characteristics. In curve fit we had also checked the goodness of fit by using curve fit toolbox and got the result that the cubic curve fit is a best fit as compare to the linear curve fit as it has a R-square value equal to 0.99 almost 1 and also had better characteristics with minimum error.
- We can also say that by increasing the polynomial degree our curve fit accuracy increases and results in a best fit or even can achieve the perfect fit by using various techniques.
REFERENCES -