Project

Curve fitting using Matlab

Aim:-

To write code to fit a linear and cubic polynomial for the Cp data.

Introduction:-

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.

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 data points. There are,

  1. The sum of squares due to error (SSE)
  2. R-square
  3. Adjusted R-square
  4. 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 labelled as SSE. The equation for SSE is shown in Figure 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 the ratio of the sum of squares of the regression (SSR) and the total sum of squares (SST). SSR is defined as in figure 2.

What that means in layman's terms is that the higher the value of R-square the better the fit, the lower it is the lousier the fit, R-square ranges from a value of 0 to 1

  • 0 for the worst possible fit for the data set
  • 1 for the best possible fit for the data set

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 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)/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 in figure 3

Code:-

Figure 4 shows the code made for the linear and cubic fit without using the split method

% project: Curve fitting
%Plot the linear and cubic fit curves along with the raw data points.

clear all
close all
clc


% Preparing the data
cp_data = load('data');   % importing the file to mATLAB
temperature = cp_data(:,1); % TEMPERATURE VECTOR
cp = cp_data(:,2); % SPECIFIC HEAT VECTOR
% Curve fit

% cp_linear= a*t +b
% curve fit for 1 degree polynomial
coefficients1 = polyfit(temperature,cp,1);
predicted_cp1 = polyval(coefficients1, temperature);


% curve fit for 3 degree polynomial
coefficients3 = polyfit(temperature,cp,3);
predicted_cp3 = polyval(coefficients3, temperature);


% Compare the curve fit for 1 degree polynomial with the original data
figure(1)
plot(temperature, cp, 'linewidth', 3)
hold on
plot(temperature,predicted_cp1,'linewidth',3,'color','r')
xlabel('Temperature[K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Orignal dataset' , 'Curve Fit')
title('First Degree Polynomial')


% Compare the curve fit for 3 degree polynomial with the original data
figure(2)
plot(temperature, cp, 'linewidth', 3)
hold on
plot(temperature,predicted_cp3,'linewidth',3,'color','r')
xlabel('Temperature[K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Orignal dataset' , 'Curve Fit')
title('Third Degree Polynomial')


% calculating SSE,SSR, SSt,R_sq,Rsq_adj,RMSE for 1st and 3rd degree polynomial

error1= predicted_cp1 - cp;
error3 = predicted_cp3 - cp;
n = length(cp);
i1 = 2;
i3 = 3;

%mean
M = mean(cp);

%calc SSE for 1st and 3rd degree
sse1 = (error1).^2;
sse3 = (error3).^2;
SSE1 = sum(sse1)
SSE3 = sum(sse3)

% calculating SSR for 1st and 3rd degree
ssr1 = (predicted_cp1 - M).^2;
ssr3 = (predicted_cp3 - M).^2;
SSR1 = sum(ssr1)
SSR3 = sum(ssr3) 
% calculating SST for 1st and 3rd degree 
SST1 = SSE1 + SSR1
SST3 = SSE3 + SSR3
 
% calculating R_sq for first and third degree 
R_sq1 = SSR1/SST1
R_sq3 = SSR3/SST3
 
% calculating R_sq_adj for first and third degree
R_sq_adj1 = 1-(((1-R_sq1)*(n-1))/(n-i1-1))
R_sq_adj3 = 1-(((1-R_sq3)*(n-1))/(n-i3-1)) 

% calculating RMSE for first and third degree
RMSE1 = (SSE1/n)^0.5
RMSE3 = (SSE3/n)^0.5

Before writing the code, it's important to start with clear all, close all and clc commands since

Clear all - removes data from the workspace, freeing up system memory

Close all- deletes the current figure or the specified figure

clc - clears all input and output from the Command Window display, giving you a "clean screen

Next step is to define the inputs and variables. Temperature and specific heat were defined from the data file. Next step is to create data points by using a linear equation. The polyfit command is used to calculate the linear coefficients of the specific heat, polyfit is a Matlab function that computes a least-squares polynomial for a given set of data. Polyfit generates the coefficients of the polynomial, which can be used to model a curve to it the data. To calculate the predicted specific heat value for the first-degree polynomial, polyval command is used. Polyval evaluates a polynomial for a given set of x values. So, Polyval generates a curve to fit the data based on the coefficients found using polyfit.

Same steps are done for calculating the third-degree polynomial (cubic fit).

Next step is to plot the graphs for the two curve fits. One for the linear polynomial degree and one for the cubic polynomial degree. The x-axis and y-axis for both curve fits have been defined. Figure 5 and 6 show the two curve fits.

The final step for part 1 of the project is to find out the goodness of fit, 4 criteria were used, these are :

  • The sum of squares due to error (SSE)
  • R-square
  • Adjusted R-square
  • Root mean squared error (RMSE)

Each quantity was calculated using the formulae shown above, The answer for each quantity for both curves are shown in figure 7

SSE1 =

   2.1630e+06


SSE3 =

   9.4272e+04


SSR1 =

   2.6631e+07


SSR3 =

   2.8699e+07


SST1 =

   2.8794e+07


SST3 =

   2.8794e+07


R_sq1 =

    0.9249


R_sq3 =

    0.9967


R_sq_adj1 =

    0.9248


R_sq_adj3 =

    0.9967


RMSE1 =

   25.9991


RMSE3 =

    5.4277

To find out whether the results shown here are correct, cftool command is used. cftool command or curve fit toolbox provides an app and functions for fitting curves and surfaces to data. The toolbox lets the user perform exploratory data analysis, preprocess and post-process data, compare candidate models, and remove outliers. Figure 8 and 9 shows the graphs and data for the curve fits in the curve fitting toolbox.

As seen in figure 9, a warning sign says that ' Equation is badly conditioned. Remove repeated data points or try centring and scaling.'

In most cases, it is better to go with the first-order fit because it will be more robust. but if the user wants to do a fitting operation for a very large dataset, it is always better to split the data into equal fragments and perform a fitting operation individually.

Figure 10 shows the PROGRAM TO FITTING THE CURVE WITH SPLITTING ORIGINAL RAW DATA

% Part 2 of the project: splitting the data


clear all
close all 
clc


% Preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);


%Splitting the raw data to create a curve for the linear equation
% cp_linear= a*t +b
% curve fit for 1st degree polynomial

Linear_coefficients_1 = polyfit(temperature(1:800),cp(1:800),1); % splitting the raw data and coefficient indivdual range by 800
Linear_coefficients_2 = polyfit(temperature(801:1600),cp(801:1600),1);
Linear_coefficients_3 = polyfit(temperature(1601:2400),cp(1601:2400),1);
Linear_coefficients_4 = polyfit(temperature(2401:3200),cp(2401:3200),1);


linear_predicted_cp1 = polyval(Linear_coefficients_1,temperature(1:800));  % to find the polynomial values indivdually by splitting 
linear_predicted_cp2 = polyval(Linear_coefficients_2,temperature(801:1600));
linear_predicted_cp3 = polyval(Linear_coefficients_3,temperature(1601:2400));
linear_predicted_cp4 = polyval(Linear_coefficients_4,temperature(2401:3200));

linear_predicted_cp = [ linear_predicted_cp1; linear_predicted_cp2;linear_predicted_cp3;linear_predicted_cp4];


% Splitting the data to create a curve for the cubic equation
% Cubic_Cp = a*T^3+b*T^2+c*T+d
% curve fit for 3rd degree polynomial

Cubic_coefficients_1 = polyfit(temperature(1:800),cp(1:800),3)
Cubic_coefficients_2 = polyfit(temperature(801:1600),cp(801:1600),3)
Cubic_coefficients_3 = polyfit(temperature(1601:2400),cp(1601:2400),3)
Cubic_coefficients_4 = polyfit(temperature(2401:3200),cp(2401:3200),3)

Cubic_predicted_cp1 = polyval(Cubic_coefficients_1,temperature(1:800))
Cubic_predicted_cp2 = polyval(Cubic_coefficients_2,temperature(801:1600))
Cubic_predicted_cp3 = polyval(Cubic_coefficients_3,temperature(1601:2400))
Cubic_predicted_cp4 = polyval(Cubic_coefficients_4,temperature(2401:3200))

Cubic_predicted_cp = [Cubic_predicted_cp1;Cubic_predicted_cp2;Cubic_predicted_cp3;Cubic_predicted_cp4];


% Compare the curve fit for 1 degree polynomial with the original data
figure(1)
plot(temperature, cp, 'linewidth', 3)
hold on
plot(temperature,linear_predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature[K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Orignal dataset' , 'Curve Fit')
title('First Degree Polynomial')




% Compare the curve fit for 3 degree polynomial with the original data
figure(2)
plot(temperature, cp, 'linewidth', 3)
hold on
plot(temperature,Cubic_predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature[K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Orignal dataset' , 'Curve Fit')
title('Third Degree Polynomial')



% calculating SSE,SSR, SSt,R_sq,Rsq_adj,RMSE for 1st and 3rd degree polynomial

error1= linear_predicted_cp - cp;
error3= Cubic_predicted_cp - cp;
n = length(cp);
i1 = 2;
i3 = 3;

%mean
M = mean(cp);

%calc SSE for 1st and 3rd degree
sse1 = (error1).^2;
sse3 = (error3).^2;
SSE1 = sum(sse1)
SSE3 = sum(sse3)

% calculating SSR for 1st and 3rd degree
ssr1 = (linear_predicted_cp - M).^2;
ssr3 = (Cubic_predicted_cp - M).^2;
SSR1 = sum(ssr1)
SSR3 = sum(ssr3) 
% calculating SST for 1st and 3rd degree 
SST1 = SSE1 + SSR1
SST3 = SSE3 + SSR3
 
% calculating R_sq for first and third degree 
R_sq1 = SSR1/SST1
R_sq3 = SSR3/SST3
 
% calculating R_sq_adj for first and third degree
R_sq_adj1 = 1-(((1-R_sq1)*(n-1))/(n-i1-1))
R_sq_adj3 = 1-(((1-R_sq3)*(n-1))/(n-i3-1)) 

% calculating RMSE for first and third degree
RMSE1 = (SSE1/n)^0.5
RMSE3 = (SSE3/n)^0.5

The fit is divided into 4 parts, with each part has 800 values

Before writing the code, it's important to start with clear all, close all and clc commands since

Clear all - removes data from the workspace, freeing up system memory

Close all- deletes the current figure or the specified figure

clc - clears all input and output from the Command Window display, giving you a "clean screen

Similar steps in part one of the project were used. To calculate the coefficients and the predicted specific heat values, the whole data was divided into four parts that is 1 – 800, 801 – 1600, 1601 – 2400, and 2401 – 3200.

Next step is to create data points by using a linear equation. The polyfit command is used to calculate the linear coefficients of the specific heat, polyfit is a Matlab function that computes a least-squares polynomial for a given set of data. Polyfit generates the coefficients of the polynomial, which can be used to model a curve to it the data. To calculate the predicted specific heat value for the first-degree polynomial, polyval command is used. Polyval evaluates a polynomial for a given set of x values. So, Polyval generates a curve to fit the data based on the coefficients found using polyfit.

Same steps are done for calculating the third-degree polynomial (cubic fit).

Next step is to plot the graphs for the two curve fits. One for the linear polynomial degree and one for the cubic polynomial degree. The x-axis and y-axis for both curve fits have been defined. Figure 11 and 12 show the two curve fits.

The final step for part 2 of the project is to find out the goodness of fit, 4 criteria were used, these are :

  • The sum of squares due to error (SSE)
  • R-square
  • Adjusted R-square
  • Root mean squared error (RMSE)

Each quantity was calculated using the formulae shown above, The answer for each quantity for both curves are shown in figure 13

SSE1 =

   4.4476e+04


SSE3 =

   1.4623e+03


SSR1 =

   2.8749e+07


SSR3 =

   2.8792e+07


SST1 =

   2.8794e+07


SST3 =

   2.8794e+07


R_sq1 =

    0.9985


R_sq3 =

    0.9999


R_sq_adj1 =

    0.9985


R_sq_adj3 =

    0.9999


RMSE1 =

    3.7281


RMSE3 =

    0.6760

Conclusion:-

The R – square parameter for the linear polynomial equation is 0.9249 whereas for the cubic polynomial equation it is 0.9967 which means the degree of the polynomial increases the curve fitting also increases. For R – square the higher the number the better the fit, the lower the number the lousier the fit. When using the splitting method the linear polynomial equation is 0.9985 and the cubic polynomial equation is 0.9999 this shows that splitting method provides a more accurate result for curve fits

References:-

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.