Curve Fitting
Code to fit a linear and cubic polynomial for the Cp data.
Curve fit describes how your data changes mathematically once you have curve fit you can do additional mathematical operations which might not be possible with the initial dataset. Curve Fitting is used to carry out the system simulation and optimization, To do Trend analysis, To determine values of properties at intermediate points and for hypothesis testing.
There are two types of curve fit Exact and Best. In our case, we will be using the best fit. The Curve can be made fit perfectly by increasing the order of polynomial or by using non-linear regression. Best fit can be obtained by the Least Square Regression Method known as L.S.R. Cubic fit can be improved by centering and scaling.
I have taken reference from this
Goodness of Fit is decided on the basis of 4 measurable quantities.
1- Sum of Squared Error (SSE). 2- R - Square 3 - Adjusted R square. 4- Root Mean Squared Error (RMSE).
Using these four quantities Matlab effectively deduces the goodness of fit, typically is R-square is greater than 0.95 the fit is assumed to be good.
clear all
close all
clc
%Preparing the Data
cp_data = load('data'); %Loading Data file from Computer in variable cp_data.
temperature = cp_data(:,1); % Assigning variable temperature with values in column 1 of cp_data.
cp = cp_data(:,2); % Assigning variable cp with values in column 2 of cp_data.
%Curve Fit
%Assuming Polynomial of Degree 3.
%cp =at^3+bt^2+ct+d
[co_effs] = polyfit(temperature,cp,3); �lculation of values of coefficients of polynomial.
predicted_cp = polyval(co_effs,temperature); �lculating Predicted Value of Cp.
%Creating Table to Calculate Erro r
T =table(temperature,cp,predicted_cp,cp-predicted_cp,'VariableNames',{'Temperature','Cp','CpFit','FitError'});
mean = mean(T.Cp); �lculating Mean of Original Cp=Ymean
T.Fit_Square = (T.FitError.^2); �ding One More Variable FitError_Square
T.Cp_mean = (T.Cp)- mean; �lculating Y-Ymean
T.Cp_Mean_Square = (T.Cp_mean.^2); �lculating (Y-Ymean)^2
Summation_fitsquare = sum(T.Fit_Square); �lculating Summation of (y-yfit)^2 = Sr
summation_Cp_Mean_Square = sum(T.Cp_Mean_Square); �lculating summation of (y-ymean)^2 = St
T(1:5,:); %Display First 5 Rows of Table
Correlation_Coefficient = ((summation_Cp_Mean_Square-Summation_fitsquare)/summation_Cp_Mean_Square)^1/2; �lculating Correlation Coefficient.
%Compare my curve fit with original data
plot(temperature,cp,'linewidth',3) %Plot with original values of temperature and cp.
hold on % Plot should not get overrite.
plot(temperature,predicted_cp,'linewidth',3,'color','r')% Plot with predicted values of cp.
xlabel('Temperature [k]') % Giving label for x axis.
ylabel('Specific Heat [kJ/Kmol-K]')% Giving label for y axis.
legend('original dataset','curve fit') % Giving identification for old and new curves.
A Program code is attached with this. Initially, we have assigned the temperature variable with values in column 1 from cp vs temperature data. we have also assigned cp variable with values in column from data. we assumed the cubic polynomial.
By using polyfit command we have calculated the coefficient of the polynomial. Now by using polyval command w have calculated the predicted value of cp. Now we have created a table using table command while creating table I have encountered an error as shown below, however, the error was I had written VariablesNames instead of VariableNames.
After correcting I had a new column in a table with a variable called Fit Square. While calculating Fit Square I have encountered an error as shown below the error was for multiplication of element it should be . after T.FitError.
After Correcting I have calculated the R Square.
Now we have plot temperature vs cp with initial value of cp after hold on we have also plotted the temperature vs predicted value of cp. The variation can be seen.
I have also used a Curve Fitting Toolbox which is an inbuilt Matlab tool for calculating the Curve fitting Calculations. I have calculated for linear as well as Cubic polynomial. Its file is attached with this.
In our Program for Linear Fit, we get the value of R Square = 0.9249 and for Cubic Polynomial value of R Square = 0.9967. From This, we can conclude that as we increase the order of polynomial value of R square increases and so the Best Fit is achieved.