Project

Curve Fitting based on RSME and R-square method

Aim:

To write a code to fit a linear and cubic polynomial for the Cp data given below,using R-square method and RSME method.

Solution:

Curve Fitting:

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data point possibly subject to constraints. Curve fitting can involve either interpolation where an exact fit to the data is required, or smoothing in which a "smooth" function is constructed that approximately fits the data.[from wikipedia]

Best fit:

Best fit is obtained by when the predicted value from various method is having value near to the actual value and value obtained by the method is just near by 1 or 100% match with the actual data.

By increasing the iteration value and and the polynomial power will give the improvement in the cubical plot.

1.By RSME method

First here first i am going to show RSME method, in this the solution obtained from it is very low as below as 0.5 in both linear plot and in cubic plot also

1. Linear plot

close all
clc

%imorting data
c = load('data');
temperature = c(:,1);
c_p = c(:,2);

%predecting value of cp
cp = polyfit(temperature,c_p,1);
p_cp = polyval(cp,temperature);

%using RSME method for curve fit 
sse = 0;
for i = 1:3200
    sse = (c(i,2) - p_cp(i))^2;
    
end
RSME = sqrt(sse/3200)

%plot of linear curve fit
plot(temperature,c_p,'color','k','linewidth',2.5)
hold on 
plot(temperature,p_cp,'r--','linewidth',1.5)
xlabel 'temperature[k]'
ylabel 'specific_heat[KJ/Kmole.k]'
legend ('specific heat','linear fit curve')
title 'Temperature Vs Specific Heat'

Ployfit-command:

p = polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1

p(x)=p1xn+p2xn−1+...+pnx+pn+1.

ployval -command

y = polyval(p,x) evaluates the polynomial p at each point in x. The argument p is a vector of length n+1 whose elements are the coefficients (in descending powers) of an nth-degree polynomial:

p(x)=p1xn+p2xn−1+...+pnx+pn+1.

The polynomial coefficients in p can be calculated for different purposes by functions like polyint, polyder, and polyfit, but you can specify any vector for the coefficients.

[The above contents on commands are obtained from MATLAB Documents]

by using the above two commands, polyfit obtains a second order polynomial equation and get solved to get two values and that two values is used to get the predicted value, this step is repeated for all the below three codes to get the fit value and close plot of fit.

results of first code is shown below

and the plot is shown below;

The code for cubical fit is shown below

close all
clc

%imorting data
c = load('data');
temperature = c(:,1);
c_p = c(:,2);

%predecting value of cp
cp = polyfit(temperature,c_p,3);
p_cp = polyval(cp,temperature);

%using RSME method for curve fit 
sse = 0;
for i = 1:3200
    sse = (c(i,2) - p_cp(i))^2;
    
end
RSME = sqrt(sse/3200)

%plot of linear curve fit
plot(temperature,c_p,'color','k','linewidth',2.5)
hold on 
plot(temperature,p_cp,'r--','linewidth',1.5)
xlabel 'temperature[k]'
ylabel 'specific_heat[KJ/Kmole.k]'
legend ('specific heat','cubic fit curve')
title 'Temperature Vs Specific Heat'

here the above two codes are based on RSME method and the med is explained below

Root Mean Square Error (RMSE) measures how much error there is between two data sets. In other words, it compares a predicted value and an observed or known value. The smaller an RMSE value, the closer predicted and observed values are.

It’s also known as Root Mean Square Deviation and is one of the most widely used statistics in GIS.

And the formula is shown below

RSME =sqrt(sse/n)

based on this method the cubical fit plot and fit value is shown below

the plot of cubical fit is shown below

The obtaines RSME value in both linear and cubic fit values is less than 0.5 but a based on the article provided in the challenge a best fit should have atleast 0.95 or more than that from this it is clear that RSME is not a best fit value for the above problem.

2.By using R-square method the code for finding the fit is shown below

i.linear fit

For linear fit code is given below

close all
clc

%imorting data
c = load('data');
temperature = c(:,1);
c_p = c(:,2);

%predecting value of cp
cp = polyfit(temperature,c_p,1);
p_cp = polyval(cp,temperature);

%using R-square method for curve fit 

%for sse
sse = 0;
for i = 1:3200
    sse = (c(i,2) - p_cp(i))^2;
    
end

%for finding ssr we need mean and summation and i call it here as suma
suma = 0;
for j = 1:3200
    suma = suma + c(j,2);
end
mean = suma/3600;
for k = 1:3200
    ssr = (p_cp(k) - mean)^2;
end

%for finding sst
sst = sse+ssr;

%for finding r-square

R_square = ssr/sst

%plot of linear curve fit
plot(temperature,c_p,'color','k','linewidth',2.5)
hold on 
plot(temperature,p_cp,'r--','linewidth',1.5)
xlabel 'temperature[k]'
ylabel 'specific_heat[KJ/Kmole.k]'
legend ('specific heat','linear fit curve')
title 'Temperature Vs Specific Heat'

R-square Method

A data set has n values marked y1,...,yn (collectively known as yi or as a vector y = [y1,...,yn]T), each associated with a fitted (or modeled, or predicted) value f1,...,fn (known as fi, or sometimes ŷi, as a vector f).(from wikipedia)

This quantity that 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.

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(from Skill-lync article as mentioned in the challenge)

R-Square = ssr/sst

comming back to the above code the values of cp is predected using polyfit and polyval commands and that value is used in finding the value of R-square.

the results of linear fit based on R-square method is shown below

the R-square value

and plot on linear fit is shown below

for cubic fit the code is shown below

close all
clc

%imorting data
c = load('data');
temperature = c(:,1);
c_p = c(:,2);

%predecting value of cp
cp = polyfit(temperature,c_p,3);
p_cp = polyval(cp,temperature);

%using R-square method for curve fit 

%for sse
sse = 0;
for i = 1:3200
    sse = (c(i,2) - p_cp(i))^2;
    
end

%for finding ssr we need mean and summation and i call it here as suma
suma = 0;
for j = 1:3200
    suma = suma + c(j,2);
end
mean = suma/3600;
for k = 1:3200
    ssr = (p_cp(k) - mean)^2;
end

%for finding sst
sst = sse+ssr;

%for finding r-square

R_square = ssr/sst

%plot of linear curve fit
plot(temperature,c_p,'color','k','linewidth',2.5)
hold on 
plot(temperature,p_cp,'r--','linewidth',1.5)
xlabel 'temperature[k]'
ylabel 'specific_heat[KJ/Kmole.k]'
legend ('specific heat','Cubic fit curve')
title 'Temperature Vs Specific Heat'

in this two equation the only difference is change in the polynomial value for linear polt i choosed to have 2 ploynomial value and for cubic fit 3 polynomial is used and the solution is given below,

the R-square vale for the above code

the plot the cubic fit is shown below

based on the closeness of value it is clear that the best fit for cubic and linear fit is based on R-square method because the value obtained is 0.98 and 0.997 which is equal to 1 as a roundoff.

Result:

Finally the code for finding linear and cubic fit for the given data is done and various vale of the method are shown and answers for the above question are also given in the sollution.

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.