Project

Curve fitting using MATLAB

Aim – To write a program to fit a linear and cubic polynomial for Cp.

Objective – 1. To write a code to fit a linear and cubic polynomial for the given Cp data.

  1. Plot the linear and cubic fit curves.
  2. Explain the parameters used to measure the fitness characteristics

Theory –

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.

After fitting data with one or more models, we should evaluate the goodness of fit. We can evaluate the goodness of fit in two ways – Graphical and Numerical.

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 practice, depending on your data and analysis requirements, you might need to use both types to determine the best fit.

After using graphical methods to evaluate the goodness of fit, you should examine the goodness of fit statistics. The Curve Fitting Toolbox supports these goodness of fit statistics for parametric models:

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

For the current fit, these statistics are displayed in the Results pane in the Curve Fitting app. For all fits in the current curve-fitting session, you can compare the goodness-of-fit statistics in the Table of fits.

Assume data points (x1,y1) , (x2,y2) , (x3,y3) , ... (xn,yn) and assume the best fit for these set of points will be y = f(x), the criterion under which the equation of the curve will be the best fit possible is as follows,

Suppose each of these n points has an individual error (difference in y co-ordinate values)

The error for a general "i" point can be given by the function,

Error(i) = |y(i) − value of f(x) at x(i)| = |y(i) − f(x(i))|

then,

  1. SSE (Sum of squared errors) - This statistic measures the total deviation of the response values from the fit to the response values.
  1. R-square - It measures how successful the fit is in explaining the variation of the data. The higher the value of R-square the better the fit. R-square ranges from a value of 0 to 1.

The value of R-square can be calculated using the following formula,

`R^2 = (SSR)/(SST)`

where,

SSR = sum of squares of the regression, which is basically the square of the difference between the value of the fitting function at a given point and the arithmetic mean of the data set.

and, `SST = SSR + SSE`

  1. Adjusted R-square –

Adjusted R-square is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variables.

where,

n - the number of datapoints

k - the number of independent variables

  1. RMSE (Root Mean Squared Error) –

This statistic is also known as the fit standard error and the standard error of the regression.

Using these four quantities, MATLAB effectively deduces the goodness of a fit, typically R-square is greater than 0.95 then the fit is considered good.

Code -

clear all
close all
clc
 
% loading the data
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);
 
% linear curve fit
 
% cp = a*temp + b
coeff_l = polyfit(temp,cp,1)
pred_cpl = polyval(coeff_l,temp);
 
 
% cubic curve fit
 
% cp = a*temp^3 + b*temp^2 + c*temp + d
coeff_c = polyfit(temp,cp,3)
pred_cpc = polyval(coeff_c,temp);
 
% Plottimg
plot(temp,cp,'linewidth',4,'color','b')
hold on
plot(temp,pred_cpl,'linewidth',2,'color','g')
plot(temp,pred_cpc,'linewidth',2,'color','r')
xlabel('Temperature [K]')
ylabel('Cp [KJ/kmolK]')
legend('Original Dataset','Linear Curvefit','Cubic Curveffit')
 
 
% Fitness parameters
% Linear equation
 
SSE_linear = 0;
SSR_linear = 0;
for i=1:length(cp)
    SSE_l =(cp(i)- pred_cpl(i)).^2;
    SSE_linear = SSE_linear + SSE_l;
    
    SSR_l= (pred_cpl(i)- mean(cp)).^2;
    SSR_linear= SSR_linear + SSR_l;
end
 
SST_linear = SSE_linear + SSR_linear;
Rsquare_linear = SSR_linear/SST_linear
 
k=1;
Adj_Rsq_linear = 1- ((1-Rsquare_linear).*(length(cp)-1))./(length(cp)-k-1)
RMSE_linear = sqrt(SSE_linear/length(cp))
 
% Fitness parameters 
% Cubic polynomial
 
SSE_cubic = 0;
SSR_cubic = 0;
for j=1:length(cp)
    SSE_c = (cp(j) - pred_cpc(j)).^2;
    SSE_cubic = SSE_cubic + SSE_c;
    
    SSR_c = (pred_cpc(j) - mean(cp)).^2;
    SSR_cubic = SSR_cubic + SSR_c;
end
 
SST_cubic = SSE_cubic + SSR_cubic;
Rsquare_cubic = 1 - SSE_cubic/SST_cubic
 
Adj_Rsq_cubic = 1 - ((1-Rsquare_cubic).*(length(cp)-1))./(length(cp)-k-1)
RMSE_cubic = sqrt(SSE_cubic/length(cp))

Result – 1. CP vs Temperature curve –

  1. Workspace Output –
  1. Validation –

Linear Curvefit –

Cubic Curvefit –

Q1. How to make a curve fit perfectly?

Ans. In order to make a curve fit perfectly, we can either increase the degree of polynomial or we can use the split data-sets. In the problem, if there is no boundation on degree of polynomial then it is always easier to keep increasing the degree of polynomial. Otherwise use split curve-fit method. In split curve-fit, we divide the data into ‘n’ no of data-sets which can or can’t be of the same size and fit them accordingly.

Q2. How to get best fit?

Ans. 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. The adjusted R-square statistic is generally the best indicator of the fit quality when you add additional coefficients to your model. If the value of adjusted R-square is close to 1, then your curve is best fit.

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

Ans. Since we can’t increase the order of the polynomial, so it would be best to use the split curve-fit. We are going to divide the data for Cp into 4 different data-sets and use the fit to see if it is best fit or not.

Code – Splitwise Curve-fit

clear all 
close all
clc
 
%preparing inputs
input = load('data');
temp = input(:,1);
cp = input(:,2);
 
%curve fitting
%splitting data into four part
temp1=input(1:500,1);
cp1=input(1:500,2);
 
temp2=input(501:1100,1);
cp2=input(501:1100,2);
 
temp3=input(1101:2100,1);
cp3=input(1101:2100,2);
 
temp4=input(2101:3200,1);
cp4=input(2101:3200,2);
 
 
%cubic fit
[coeff1] = polyfit(temp1,cp1,3);
pred_cp1 = polyval(coeff1,temp1);
 
[coeff2] = polyfit(temp2,cp2,3);
pred_cp2 = polyval(coeff2,temp2);
 
[coeff3] = polyfit(temp3,cp3,3);
pred_cp3 = polyval(coeff3,temp3);
 
[coeff4] = polyfit(temp4,cp4,3);
pred_cp4 = polyval(coeff4,temp4);
 
%plotting and comparison
plot(temp,cp,'linewidth',4,'color','b');
hold on
 
% split cubic fit
plot(temp1,pred_cp1,'linewidth',2,'color','r');
plot(temp2,pred_cp2,'linewidth',2,'color','r');
plot(temp3,pred_cp3,'linewidth',2,'color','r');
plot(temp4,pred_cp4,'linewidth',2,'color','r');
 
xlabel('Temperature[K]');
ylabel('Specific heat[KJ/kmol-K]');
title('Splitwise Curve fit');
legend('original dataset','split curve-fit')
axis([0 4000 900 1500])
grid on

Result –

Comparison –

Conclusion –

As we can see, after splitting the data into 4 parts, our curve is almost similar to the original curve, hence graphically we can say that our curve is perfect fit for the given data.

And not just graphically, numerically too it is best-fit as the value of R-square and adjusted R-square is equal to 1.

Reference Links – 1. https://in.mathworks.com/help/curvefit/evaluating-goodness-of-fit.html

2. https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860

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.