Project

MATLAB Program based on Curve fitting

AIM :

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

OBJECTIVES :

  • Write code to fit a linear and cubic polynomial for the Cp data.
  • Plot the linear and cubic fit curves along with the raw data points.
  • Write a code to show splitwise method.
  • Explain the parameters used to measure the fitness characteristics for both curves.

THEORY :

Curve fitting is one of the most powerful and most widely used analysis tools in Origin. It examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), intending to define a 'best fit' model of the relationship. Origin provides tools for linear, polynomial, and nonlinear curve fitting along with validation and goodness of fit tests.

Goodness of Fit

The term goodness of fit refers to a statistical test that determines how well sample data fits a distribution from a population with a normal distribution. It establishes the discrepancy between the observed values and those expected of the model in a normal distribution case. There are multiple methods to determine the goodness of fit, which include

  • Sum of Squares due to Error (SSE)
  • R-square
  • Adjusted R-square
  • Root Mean Squared Error (RMSE)

Sum of Squares due to Error (SSE) :-

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.

`SSE = sum_(i=1)^N (y_i - haty_i)^2`

  • `y_i` is one of the values in the sample
  • `hat y_i` is the predicted value

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 :-

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 (haty_i - bary_i)^2`

  • `hat y_i` is the predicted value
  • `bar y_i` is the sample mean

SST is also called the sum of squares about the mean and is defined as

`SST = sum_(i=1)^N (y_i - bary_i)^2`

  • `y_i` is one of the values in the sample
  • `bary_i` is the sample mean

where SST = SSR + SSE. Given these definitions, R-square is expressed as

R-square `= (SSR)/(SST) = 1 − (SSE)/ (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.

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 are 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">nn data points that are required to calculate the sum of squares.

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 (RMSE) :-

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 = sqrt((SSE)/v)`

where `v` indicates the number of independent pieces of information.

PROGRAM :

Main Program

% Curve Fitting Program on MATLAB
clear all
close all
clc

% Step I : Inputs
cp_data = load("data");
temperature = cp_data(:,1);
cp = cp_data(:,2);

% Curve Fit
% Degree of Polynomial is 1
% cp = a*T + b
co_effs1 = polyfit(temperature,cp,1);
predicted_cp1 = polyval(co_effs1,temperature);

% Degree of Polynomial is 3
% cp = a*T^3 + b*T^2 + c*T + d
co_effs2 = polyfit(temperature,cp,3);
predicted_cp2 = polyval(co_effs2,temperature);

% Linear Fit
figure(1)
plot(temperature,cp,'LineWidth', 3);
hold on
plot(temperature,predicted_cp1,'LineWidth',3,'Color','k')
xlabel('Temperature [ K ]')
ylabel('Specific Heat [ KJ/Kmol-K ]')
legend('Original Data Set','Linear Fit')
title('Linear Fit')
grid on
hold off

% Cubic Curve Fit
figure(2)
plot(temperature,cp,'LineWidth', 3);
hold on
plot(temperature,predicted_cp2,'LineWidth',3,'Color','r')
xlabel('Temperature [ K ]')
ylabel('Specific Heat [ KJ/Kmol-K ]')
legend('Original Data Set','Cubic Curve Fit')
title('Cubic Curve Fit')
grid on
hold off

% Linear Fit vs Cubic Curve Fit
figure(3)
plot(temperature,cp,'LineWidth', 3);
hold on
plot(temperature,predicted_cp1,'LineWidth',3,'Color','k')
plot(temperature,predicted_cp2,'LineWidth',3,'Color','r')
xlabel('Temperature [ K ]')
ylabel('Specific Heat [ KJ/Kmol-K ]')
legend('Original Data Set','Linear Fit','Cubic Curve Fit')
title('Linear Fit vs Cubic Curve Fit')
grid on
hold off

% Check Goodness of Fit for both Linear & Cubic Curve Fit

% For Linear Fit
% Calculating Sum of Squares due to Errors (SSE)
SSE_Linear = sum((cp - predicted_cp1).^2);

% Square of Regression (SSR)
SSR_Linear = sum((predicted_cp1 - mean(cp)).^2);

% Total Sum of Squares (SST)
SST_Linear = SSR_Linear + SSE_Linear;

% R-square
Rsquare_Linear = (SSR_Linear/SST_Linear);

% Adjusted R-square
Adjusted_Rsquare_Linear = 1 - ((SSE_Linear.*(length(cp)+2-1))/(SST_Linear.*length(cp)));

% Root Mean Squared Error (RMSE)
RMSE_Linear = sqrt(SSE_Linear/length(cp)); 


% For Cubic Fit
% Calculating Sum of Squares due to Errors (SSE)
SSE_Cubic = sum((cp - predicted_cp2).^2);

% Square of Regression (SSR)
SSR_Cubic = sum((predicted_cp2 - mean(cp)).^2);

% Total Sum of Squares (SST)
SST_Cubic = SSR_Cubic + SSE_Cubic;

% R-square
Rsquare_Cubic = (SSR_Cubic/SST_Cubic);

% Adjusted R-square
Adjusted_Rsquare_Cubic = 1 - ((SSE_Cubic.*(length(cp)+4-1))/(SST_Cubic.*length(cp)));

% Root Mean Squared Error (RMSE)
RMSE_Cubic = sqrt(SSE_Cubic/length(cp));

RESULTS :

1. Linear Fit

2. Cubic Curve Fit

Best Fit is observed as we keep on increasing the Degree of Polynomial

3. Linear vs Cubic Curve Fit

MATLAB Program for Splitwise Curve Fitting Method
% MATLAB Program for Splitwise Curve Fitting
clear all
close all
clc

% Step I : Inputs
cp_data = load("data");
temperature = cp_data(:,1);
cp = cp_data(:,2);

% Step II : Splitting Data into 4 Parts
% Part 1
temperature1 = cp_data(1:800,1);
cp1 = cp_data(1:800,2);
% Part 2
temperature2 = cp_data(801:1600,1);
cp2 = cp_data(801:1600,2);
% Part 3
temperature3 = cp_data(1601:2400,1);
cp3 = cp_data(1601:2400,2);
% Part 4
temperature4 = cp_data(2401:3200,1);
cp4 = cp_data(2401:3200,2);

% Step III : Consider Cubic Polynomial and calulate predicted value of cp
% cp = a*T^3 + b*T^2 + c*T + d
co_effs1 = polyfit(temperature1,cp1,3);
predicted_cp1 = polyval(co_effs1,temperature1);

co_effs2 = polyfit(temperature2,cp2,3);
predicted_cp2 = polyval(co_effs2,temperature2);

co_effs3 = polyfit(temperature3,cp3,3);
predicted_cp3 = polyval(co_effs3,temperature3);

co_effs4 = polyfit(temperature4,cp4,3);
predicted_cp4 = polyval(co_effs4,temperature4);

% Step IV : Plotting Cubic Fit
plot(temperature,cp,'LineWidth',3,'Color','k');
hold on
plot(temperature1,predicted_cp1,'LineWidth',3,'Color','r')
plot(temperature2,predicted_cp2,'LineWidth',3,'Color','g')
plot(temperature3,predicted_cp3,'LineWidth',3,'Color','y')
plot(temperature4,predicted_cp4,'LineWidth',3,'Color','c')
hold off
xlabel('Temperature [ K ]')
ylabel('Specific Heat [ KJ/Kmol-K ]')
legend('Original Data Set','Cubic Curve Fit')
title('Cubic Curve Fit using Splitwise Method')
grid on

RESULTS :

CONCLUSION :

From the above outputs and graphs for both the programs (curve fit and splitwise curve fit), one can conclude that if we increase the order of the equation we will achieve a good fit to the curve. However, if we continue increasing the order of the equation, then at one point we will attain disorder equations, which means points are overlapping, which is called overfit as explained above. For this, the split wise method can be used, in which we can divide the curve into sets and calculate parameters for each set, later combining the sets to get the full curve, which is a good fit.

Q&A :

1. How to make a curve fit perfectly?

A perfect fit means that the curve should match the original curve without any errors as far as that particular polynomial degree is concerned.

2. How to get the best fit?

We can get the best fit by using

  • Higher Degree Polynomial
  • Splitwise Curve Fitting

To compare the accuracy of the fit, we can evaluate the goodness parameter. When the improvement in the goodness of fit becomes stagnant, the best fit curve is determined.

3. What could be done to improve the cubic fit?

We can improve the cubic fit by splitting the curve into multiple parts and using a different cubic polynomial for each set of temperatures and specific heat values. This reduces errors and provides the best cubic fit.

REFERENCES :

  1. How to Perform Curve Fitting Using the Curve Fitting App in MATLAB
  2. Introduction to curve fitting using Matlab - Part 01
  3. Introduction to curve fitting using Matlab - Part 02
  4. The goodness of Fit Statistics
  5. Sum of Squares (SS)
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.