Project

Linear and cubic curve fitting for the Cp data using MATLAB

AIM: To write a code in MATLAB to fit a linear and cubic polynomial for the Cp data.

THEORY:

  1. Parameters used to measure the fitness characteristics for both the curves:
  • R-square

This quantity measures how successful the fit is in explaining the variation of the data. It is also called the square of the multiple co-relation coefficient and the coefficient of multiple determination. R- square is 0 (min) for the worst possible fit for the data set. R-square is 1 (max) for the best possible fit for the data set. If R-square is greater than 0.95, the fit is assumed to be good.

Sum of squared errors (SSE): It is the summation of squares all errors. Error is the difference between predicted value and the original value.

Sum of squares of regression (SSR): It 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.

SST: Summation of SSR and SSE.

  • 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,

  1. How to make a curve fit perfectly?

In order to make perfect fit, we must consider error estimates as well. Perfect fit means, the curve should fit the original curve without showing the errors (such as centering and scaling errors) in that particular degree of polynomial. Perfect fit can always be a best fit but best fit can not be a perfect fit .

  1. How to get the best fit?

For this, we can use RMS (Root Mean Square) or simple linear regression. We can change the order of polynomial to get the best fit . The best way is split the data in two or more data folds and do the curve fit.

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

We can improve the cubic fit by splitting the data in two or more data folds and do the curve fit. I have written the code below to show split –wise method to improve the cubic fit.

5. What is need for curve fitting? What is the reason for using a certain order of polynomial ?

Using curve fitting techniques, we can find the unknown dependent value at a particular independent value. We use a certain order of polynomial because with the increase in order of the polynomial , the goodness of the fit also increases.

CODE:

Main code:

clear all
close all
clc

% reading the file 'data' and taking the inputs
cpdata=load('data');                                    %loading the particular file
temperature=cpdata(:,1);                                
cp=cpdata(:,2);

%curve fitting for a linear polynomial
coeff=polyfit(temperature,cp,1);                        %coefficients of the polynomial are generated using polyfit() command
predictedcp=polyval(coeff,temperature);                 %predicted values of cp are generated using polyval() command

%compare my linear curve fit with original dataset
figure(1)
plot(temperature,cp,'linewidth',3,'color','b')          %plotting of original data set
hold on
plot(temperature,predictedcp,'linewidth',3,'color','r') %plotting of curve fit
xlabel('temperature[k]')
ylabel('specific heat[KJ/Kmol-K]')
legend('original dataset','my curvefit')
title('curve fit for a linear polynomial')
hold off

%checking error for linear curve fit
disp('For the linear curve fit, we have found :')
curvefit_error(cp,predictedcp)                          %calling the user-defined function curvefit_error() to find the goodness of curve fit

%curve fitting for a cubic polynomial
coeff=polyfit(temperature,cp,3);                        %coefficients of the polynomial are generated using polyfit() command
predictedcp=polyval(coeff,temperature);                 %predicted values of cp are generated using polyval() command

%compare my cubic curve fit with original dataset
figure(2)
plot(temperature,cp,'linewidth',3,'color','b')          %plotting of original data set
hold on
plot(temperature,predictedcp,'linewidth',3,'color','r') %plotting of curve fit
xlabel('temperature[k]')
ylabel('specific heat[KJ/Kmol-K]')
legend('original dataset','my curvefit')
title('curve fit for a cubic polynomial')
hold off

%checking error for cubic curve fit
disp('For the cubic curve fit, we have found :')
curvefit_error(cp,predictedcp)                          %calling the user-defined function curvefit_error() to find the goodness of curve fit

Function:

function [f]= curvefit_error(cp,predictedcp)
meancp=mean(cp);
ssr=0;
sse=0;
for i=1:length(cp)
    sse=sse+(cp(i)-predictedcp(i))^2;
end
for i=1:length(cp)
    ssr=ssr+(meancp-predictedcp(i))^2;
end
sst=sse+ssr;
Rsquare=ssr/sst
rmse=(sse/length(cp))^0.5
if (Rsquare>0.95)
    disp('The fit is GOOD because Rsquare > 0.95')
else disp('The fit is NOT GOOD because Rsquare <= 0.95')
end

OUTPUT:

Plot - curve fit for a linear polynomial Snip - command window - for linear curve fit

Plot - curve fit for a cubic polynomial Snip - command window - for cubic curve fit

EXPLANATION OF THE CODE AND THE OUTPUT:

In the main code, I first loaded the file ‘data’ and took the inputs . Then comes curve fitting for a linear polynomial. Here, at first I generated the values of coefficients of polynomial and predicted values of cp using polyfit() and polyval() commands respectively. Then, I compared my linear curve fit with the original data set where I made a plot showing the two curves simultaneously . For measuring the fitness characteristics of the linear curve fit, I called the user-defined function curvefit_error() where I passed the parameters ‘cp’ and ‘predictedcp’. Similarly , I did the curve fitting for a cubic polynomial.

In the user-defined function curvefit_error(), I first calculated the values of sse,ssr and sst using the formulas given above in theory. Then, I found Rsquare and rmse using the formulas and printed them on the command window. If R-square is greater than 0.95, the fit is assumed to be good. Depending on this , I displayed whether the fit is good or not.

In the output I showed the plots for linear curve fit and cubic curve fit. I displayed values of Rsquare and rmsefor both the fits. For the linear curve fit, Rsquare = 0.9249 which is less than 0.95 mark thus it is not a good fit. On the other hand, for the cubic curve fit, Rsquare = 0.9967 which greater than 0.95 mark thus it can be assumed as a good fit. The rmse value for linear curve fit is 25.9991 and that for cubic curve fit is 5.4277 which is lesser . Thus from these results , we can say that the goodness of the fit increases with the increase in degree of the polynomial.

ERROR:

Error 1. Following error was displayed. The keyword 'end' was missing for the first for loop in the user-defined function curvefit_error().

Error 1

STEPS TAKEN TO OVERCOME THE ERROR:

I missed the word ‘end’ after line no. 6 in the user-defined function curvefit_error() . I wrote ‘end’ in line no.7 which rectified my error.

SPLIT-WISE METHOD (improving the cubic fit):

Main code:

clear all
close all
clc

% reading the file 'data' and taking the inputs
cpdata=load('data');                                    %loading the particular file
temperature=cpdata(:,1);                                
cp=cpdata(:,2);

temp1=cpdata(1:460,1);
cp1=cpdata(1:460,2);

temp2=cpdata(461:1100,1);
cp2=cpdata(461:1100,2);

temp3=cpdata(1101:2200,1);
cp3=cpdata(1101:2200,2);

temp4=cpdata(2201:3200,1);
cp4=cpdata(2201:3200,2);

%curve fitting for a cubic polynomial
coeff1=polyfit(temp1,cp1,3);                        %coefficients of the polynomial are generated using polyfit() command
predictedcp1=polyval(coeff1,temp1);                 %predicted values of cp are generated using polyval() command

coeff2=polyfit(temp2,cp2,3);                        %coefficients of the polynomial are generated using polyfit() command
predictedcp2=polyval(coeff2,temp2); 

coeff3=polyfit(temp3,cp3,3);                        %coefficients of the polynomial are generated using polyfit() command
predictedcp3=polyval(coeff3,temp3); 

coeff4=polyfit(temp4,cp4,3);                        %coefficients of the polynomial are generated using polyfit() command
predictedcp4=polyval(coeff4,temp4); 

%compare my cubic curve fit with original dataset
plot(temperature,cp,'linewidth',3,'color','b')          %plotting of original data set
hold on
plot(temp1,predictedcp1,'linewidth',3,'color','r') %plotting of curve fit
plot(temp2,predictedcp2,'linewidth',3,'color','r')
plot(temp3,predictedcp3,'linewidth',3,'color','r')
plot(temp4,predictedcp4,'linewidth',3,'color','r')
xlabel('temperature[k]')
ylabel('specific heat[KJ/Kmol-K]')
legend('original dataset','splitwise curvefit')
title('curve fit (split-wise) for a cubic polynomial')
hold off

predictedcp(1:460)=predictedcp1;
predictedcp(461:1100)=predictedcp2;
predictedcp(1101:2200)=predictedcp3;
predictedcp(2201:3200)=predictedcp4;

%checking error for cubic curve fit
disp('For the cubic curve fit, we have found :')
curvefit_error(cp,predictedcp)                          %calling the user-defined function curvefit_error() to find the goodness of curve fit

Function:

function [f]= curvefit_error(cp,predictedcp)
meancp=mean(cp);
ssr=0;
sse=0;
for i=1:length(cp)
    sse=sse+(cp(i)-predictedcp(i))^2;
end
for i=1:length(cp)
    ssr=ssr+(meancp-predictedcp(i))^2;
end
sst=sse+ssr;
Rsquare=ssr/sst
rmse=(sse/length(cp))^0.5
if (Rsquare>0.95)
    disp('The fit is GOOD because Rsquare > 0.95')
else disp('The fit is NOT GOOD because Rsquare <= 0.95')
end

Output:

Plot - cubic curve fit (split wise) Snip - command window - splitwise method

#Here, we can see after splitting the data into four folds and then doing the curve fitting , we can improve the cubic curve fit.

RESULT:

A code in MATLAB to fit a linear and cubic polynomial for the Cp data is created.

REFERENCE:

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

https://skill-lync.com/projects/Week-4-Curve-fitting-31550

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.