Project

 PERFORMING CURVE FITTING USING MATLAB AND DEDUCE GOODNESS OF FIT

AIM

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

GOVERNING EQUATION:

SSE:

`SSE=sum_(i=1)^n〖(y(i)-f(x_i))^2 〗`

SSR:

`SSR=∑_(i=1)^n〖(f(x_i)-mean)^2 〗`

SST:

`SST=SSR+SSE`

R-Square:

`R^2=SSR⁄SST`

Adjusted R-Square:

`R_a^2=1-[(1-R^2 )(n-1)/(n-k-1)]`

RMSE:

`RSME=√((SSE⁄n) )`

where,

  • n = no. of data points
  • k = no. of independent variables.

EQUATION TO FIND BEST FIT

Parameters that have using to measure the goodness of the fit.

  1. Firstly we need data set of table with perfect x and y form.
  2. Calculate the Arithmetic mean value for the raw data.
  3. Then, The sum of square of the regression(SSR) =, Where is predicted Cp.
  4. And next The sum of square of the Error(SSE)=.
  5. Now we need SST = SSR+SSE
  6. At finally R-Square = SSR/SST, Now we get the final result of R-square Which is decided to fit conditions
  7. So, when the R-Square value is greater than 0.95 the fit is assumed good, same time its nearly 1 the fit is assumed as best fit.

OBJECTIVE

  • To write a program code to fit a linear and cubic polynomial for the cp data using Matlab .
  • Plot the linear and cubic fit curves along with the raw data points and also shown Title, Axis, Labels and Legends.
  • Explain the parameters used to measure the fitness characteristics for both the curves.
  • To write a code to show split wise method.

THEORY ABOUT CURVE FIT

Curve Fitting is a process of constructing a curve which has the closest similarity for the series of data being considered. This method really helps in the mathematical modeling of an experimental or natural process.

Curve fit describes how your data changes mathematically once you have curve fit you can do additional mathematical operations which might not possible with the initial dataset. Curve fitting is used to carry out the system simulation and optimization. To do Trend analysis, determine values of properties at intermediate points and hypothesis testing.

There are two types of curve fit Perfect and Best. In our case, we will be using the best fit. the curve made fit perfectly by increasing the order of polynomial or by using non-linear regression. Best fit can be obtained by the Least Square Regression Method known as L.S.R cubic fit can be improved by scaling and centering.

Goodness of Fit is decided on the basis of 4 measurable quantities.

  1. Sum of Squared Error (SSE).
  2. R - SquareAdjusted
  3. 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 labeled as SSE.

Sum of Squares due to Error:`SSE=∑_(i=1)^n〖(y(i)-f(x_i))^2 〗`

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).

R-square can take on any value between 0 and 1, with a value closer to 1 indicating a better fit.

R-Square: `R^2=SSR⁄SST`

Adjusted R-square:

The adjusted R-square statistic is generally the best indicator of the fit quality when you add additional coefficients to your model.

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.

Adjusted R-Square `R_a^2=1-[(1-R^2 )(n-1)/(n-k-1)]`

Root Mean Squared Error:

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

A RMSE value closer to 0 indicates a better fit.

Root Mean Squared Error: `RSME=√((SSE⁄n) )`

Using these four quantities Matlab effectively deduces the goodness of fit, typically is R-square is greater than 0.95 the fit is assumed to be good.

WHAT IS DEGREE OF POLYNOMIAL

The degree of a polynomial is the highest of the degrees of the polynomial's monomials with non-zero coefficients. The degree of a term is the sum of the exponents of the variables that appear in it, and thus is a non-negative integer.

HOW TO MAKE A CURVE FIT PERFECTLY?

A perfect curve basically valuate by the error between the original dataset and the predicted dataset. Perfect fit means, the curve should fit original curve without showing any errors. To make a curve fit perfectly over the data points provided, all the errors including centering and scaling errors should be nulled i.e., the value of R-square should be equal to ‘1’.

HOW TO GET THE BEST FIT?

There are basically two methods to obtain the best fit.

  1. To increase the degree of the polynomial.
  2. To use the split-wise method i.e. to split the data into folds and do the curve fitting for it.

By increasing the degree of polynomial can get good, best, perfect curve fit. At same time when we get perfect fit or best fit most of the time we get polynomial badly condition warning. For that we can use split-wise method curve fit. A perfect fit can all the time be the best fit but the best fit may not be always a perfect fit.

  • R-square is less than to 0.95 is Bad Fit.
  • R-square is greater than equal to 0.95 is Good Fit.
  • R-square is greater than 95 and less than to 1 is Best Fit.
  • R-square is equal to 1 is Perfect Fit.

WHAT COULD BE DONE TO IMPROVE THE CUBIC FIT?

By using the split wise polynomial technique.

As mentioned above split-wise curve fitting can be used to improve the cubic fit. By splitting the dataset that temperature and Cp into Four parts. Than do the same as we did for the normal method at by using polyfit and polyval commands to calculate the predicted Cp value and plot it. So, The R^2 value will be increased and the curve fit will be improved.

ABOUT MATLAB CURVE FITTING TOOLBOX

Curve Fitting Toolbox is an inbuilt tool in MATLAB. Here we won’t be using any curve fit functions like 'polyfit' or 'polyval', you can mention the datasets directly under the X data and Y data option. For doing so you need to make sure that your data is properly updated in the workspace. One more advantage is that the toolbox enables you to perform fitting of 3d surfaces too, which cannot be seen in 'polyfit'.

While using the curve fit toolbox the ‘X data’, ‘Y data’, ‘Z data’, represents the coordinates of the particular data set being considered. ‘Z data’ only comes into play when the fitting operation is to be done for a surface/3D plot.

REFER ABOUT CURVE FITTING TOOLBOX

https://skill-lync.freshdesk.com/support/solutions/articles/43000584172

LINEAR CURVE FITTING TOOLBOX OUTPUT

R-SQUARE IS NEARLY 0.95. IT CAN CONSIDER AS A GOOD FIT(NOT BAD)

CUBIC CURVE FITTING TOOLBOX OUTPUT

R-SQUARE IS GREATER THAN 0.95 AND LESS THAN 1 IS BEST FIT. . IT CONSIDER AS BEST FIT.

LINEAR CURVE FIT CODE WITH EXPLAINATION(PROGRAM-1)

% Program for find best curve fit using frist degree polynomial
%basic startup commands
close all
clear all
clc

%Preparing the data
cp_data = load('data');% Loading data from computer
temperature = cp_data(:,1);% 1st column of the dataset is temprature
cp = cp_data(:,2);% 2nd column of the dataset is constant pressure

% Linear Curve fit polynamial frist degree(Assuming)
% cp=a*T+b - Orbitary function for linear equation, find the best value for a and b to calculate Predicted cp
co_efficiency = polyfit(temperature,cp,1);% polyfit gives value for a and b(set of co_efficiency)
predicted_cp = polyval(co_efficiency,temperature);% predicted cp values created co_efficiency(polyfit)

% Plotting and comparing curve fit with original dataset
plot(temperature,cp,'linewidth',3,'color','r')% ploting original dataset
hold on % hold the figure for multi plot
plot(temperature,predicted_cp,'linewidth',3,'color','b')% ploting predicted dataset with temperature
xlabel('Temperature [K]')% Name of x-axis
ylabel('Specific Heat [KJ/Kmol-K]')% Name of y-axis
title('Linear Curve Fit')% Title of the plot
legend('Original dataset','Linear curvefit')% differciate and name of the plots

% Calculating R-square value to find curve fit goodness
% Loop function to calculate R-square
summation=0; %initialisation values of variable
for i = 1:3200; % for loop starts at 1st value and ends with 3200th value
    summation = summation+cp_data(i,2);% summation(add) of all cp values for find the mean
end
% without for loop function we have direct command to find summation value
% that one is command |summation = sum(cp_data(:,2))|. In this we find same summation result like for loop function
%Arithmatic mean of the data
mean = summation/3200; % calculation of arithmetic mean value from original dataset to compute SSR

% Calculation of Sum of Squares of the Regression |SSR = (f(x(i)-mean)^2|
SSR=0;% Initialisation values of variables
for j = 1:3200; %for loop starts at 1st value and ends with 3200th value
    SSR = (predicted_cp(j)-mean)^2; %calculation of SSR to compute R-square value
end

% Calculation of Sum of Squares of the Error |SSE = (y(i)-f(x(i))^2|
SSE=0;% Initialisation values of variables
for k = 1:3200;% Initialisation values of variables
    SSE = (cp_data(k,2)-predicted_cp(k))^2; %calculation of SSE to compute R-square value
end

% calculate SST 
SST = SSR+SSE; % sum of SST is addition of SSR an SSE

% caluation of R_square the final result to find goodness of the curve fit
R_square = SSR/SST % Calculation Equationof R_square

OUTPUT SCREENSHOT FOR LINEAR CURVE FIT

R-SQUARE IS GREATER THAN EQUAL TO 0.95 IS GOOD FIT.

OUTPUT PLOT FOR LINEAR CURVE FIT

CUBIC CURVE FIT CODE WITH EXPLAINATION(PROGRAM-2)

% Program for find best curve fit using third degree polynomial
%basic startup commands
close all
clear all
clc

%Preparing the data
cp_data = load('data');% Loading data from computer
temperature = cp_data(:,1);% 1st column of the dataset is temprature
cp = cp_data(:,2);% 2nd column of the dataset is constant pressure

% Cubic Curve fit polynamial third degree(Assuming)
% S= Error estimation structure
% mu= Centering and Scaling values(input matrix of mean)
% cp=a*T^3+b*T^2+c*T+d - Orbitary function for cubic equation, find the best value for a, b, c and d to calculate Predicted cp
[co_efficiency,S,mu] = polyfit(temperature,cp,3);% polyfit gives value for a, b, c and d(set of co_efficiency)
predicted_cp = polyval(co_efficiency,temperature,S,mu);% predicted cp values created co_efficiency(polyfit)

% Plotting and comparing curve fit with original dataset
plot(temperature,cp,'linewidth',3,'color','r')% ploting original dataset
hold on % hold the figure for multi plot
plot(temperature,predicted_cp,'linewidth',3,'color','b')% ploting predicted dataset with temperature
xlabel('Temperature [K]')% Name of x-axis
ylabel('Specific Heat [KJ/Kmol-K]')% Name of y-axis
title('Cubic Curve Fit')% Title of the plot
legend('Original dataset','Cubic curvefit')% differciate and name of the plots

% Calculating R-square value to find curve fit goodness
% Loop function to calculate R-square
summation=0; %initialisation values of variable
for i = 1:3200; % for loop starts at 1st value and ends with 3200th value
    summation = summation+cp_data(i,2);% summation(add) of all cp values for find the mean
end
% without for loop function we have direct command to find summation value
% that one is command |summation = sum(cp_data(:,2))|. In this we find same summation result like for loop function
%Arithmatic mean of the data
mean = summation/3200; % calculation of arithmetic mean value from original dataset to compute SSR

% Calculation of Sum of Squares of the Regression |SSR = (f(x(i)-mean)^2|
SSR=0;% Initialisation values of variables
for j = 1:3200; %for loop starts at 1st value and ends with 3200th value
    SSR = (predicted_cp(j)-mean)^2; %calculation of SSR to compute R-square value
end

% Calculation of Sum of Squares of the Error |SSE = (y(i)-f(x(i))^2|
SSE=0;% Initialisation values of variables
for k = 1:3200;% Initialisation values of variables
    SSE = (cp_data(k,2)-predicted_cp(k))^2; %calculation of SSE to compute R-square value
end

% calculate SST 
SST = SSR+SSE; % sum of SST is addition of SSR an SSE

% caluation of R_square the final result to find goodness of the curve fit
R_square = SSR/SST % Calculation Equationof R_square

OUTPUT SCREENSHOT FOR CUBIC CURVE FIT

R-SQUARE IS GREATER THAN 0.95 AND LESS THAN TO 1 IS BEST FIT. . POLYNIMIAL IS BADLY CONDITIONED CHANGED BY USING OF CENTERING AND SCALING.

OUTPUT PLOT FOR CUBIC CURVE FIT

CUBIC SPLIT WISE CURVE FIT CODE WITH EXPLAINATION (PROGRAM-3)

% Program for find best curve fit using third degree polynomial split wise
%basic startup commands
close all
clear all
clc

%Preparing the data
cp_data = load('data');% Loading data from computer
temperature = cp_data(:,1);% 1st column of the dataset is temprature
cp = cp_data(:,2);% 2nd column of the dataset is constant pressure

% Split the dataset to 4 parts of dataset
% split the temperature data to 4 parts
temp_1 = temperature(1:800); % First 800 values of the temperatures as part 1
temp_2 = temperature(801:1600);%Second 800 values of the temperatures as part 2
temp_3 = temperature(1601:2400);%Third 800 values of the temperatures as part 3
temp_4 = temperature(2401:3200);%Fourth 800 values of the temperatures as part 4
% split the CP data to 4 parts
cp_1 = cp(1:800); % First 800 values of the Cp as part 1
cp_2 = cp(801:1600);%Second 800 values of the Cp as part 2
cp_3 = cp(1601:2400);%Third 800 values of the Cp as part 3
cp_4 = cp(2401:3200);%Fourth 800 values of the Cp as part 4

% Cubic Curve fit polynamial third degree(Assuming)
% S= Error estimation structure
% mu= Centering and Scaling values(input matrix of mean)
% cp=a*T^3+b*T^2+c*T+d - Orbitary function for cubic equation, find the best value for a, b, c and d to calculate Predicted cp
% Calculation of split co_efficiency and predicted_cp of split dataset
[co_effy_1,S,mu] = polyfit(temp_1,cp_1,3);% polyfit gives value for a, b, c and d(set of co_efficiency)
predicted_cp_1 = polyval(co_effy_1,temp_1,S,mu);% predicted cp_1 values created by co_efficiency_1(polyfit)
[co_effy_2,S,mu] = polyfit(temp_2,cp_2,3);
predicted_cp_2 = polyval(co_effy_2,temp_2,S,mu);
[co_effy_3,S,mu] = polyfit(temp_3,cp_3,3);
predicted_cp_3 = polyval(co_effy_3,temp_3,S,mu);
[co_effy_4,S,mu] = polyfit(temp_4,cp_4,3);
predicted_cp_4 = polyval(co_effy_4,temp_4,S,mu);

% Merge the splited Predicted cp values
predicted_cp = [predicted_cp_1; predicted_cp_2; predicted_cp_3; predicted_cp_4];

% Plotting and comparing curve fit with original dataset
plot(temperature,cp,'linewidth',3,'color','r')% ploting original dataset
hold on % hold the figure for multi plot
plot(temperature,predicted_cp,'linewidth',3,'color','b')% ploting predicted dataset with temperature
xlabel('Temperature [K]')% Name of x-axis
ylabel('Specific Heat [KJ/Kmol-K]')% Name of y-axis
title('Cubic Split Curve Fit')% Title of the plot
legend('Original dataset','Cubic split curvefit')% differciate and name of the plots

% Calculating R-square value to find curve fit goodness
% Loop function to calculate R-square
summation=0; %initialisation values of variable
for i = 1:3200; % for loop starts at 1st value and ends with 3200th value
    summation = summation+cp_data(i,2);% summation(add) of all cp values for find the mean
end
% without for loop function we have direct command to find summation value
% that one is command |summation = sum(cp_data(:,2))|. In this we find same summation result like for loop function
%Arithmatic mean of the data
mean = summation/3200; % calculation of arithmetic mean value from original dataset to compute SSR

% Calculation of Sum of Squares of the Regression |SSR = (f(x(i)-mean)^2|
SSR=0;% Initialisation values of variables
for j = 1:3200; %for loop starts at 1st value and ends with 3200th value
    SSR = (predicted_cp(j)-mean)^2; %calculation of SSR to compute R-square value
end

% Calculation of Sum of Squares of the Error |SSE = (y(i)-f(x(i))^2|
SSE=0;% Initialisation values of variables
for k = 1:3200;% Initialisation values of variables
    SSE = (cp_data(k,2)-predicted_cp(k))^2; %calculation of SSE to compute R-square value
end

% calculate SST 
SST = SSR+SSE; % sum of SST is addition of SSR an SSE

% caluation of R_square the final result to find goodness of the curve fit
R_square = SSR/SST % Calculation Equationof R_square

OUTPUT SCREENSHOT FOR CUBIC SPLIT WISE CURVE FIT

R-SQUARE IS EQUAL TO 1 IS PERFECT FIT. . IT IS CONSIDER AS PERFECT FIT. POLYNIMIAL IS BADLY CONDITIONED CHANGED BY USING OF CENTERING AND SCALING.

OUTPUT PLOT FOR CUBIC SPLIT CURVE FIT

PROCEDURE

  • First, we close all previous file and clear workspace and screen.
  • We have loaded the data file which contain two column, first column contains temperature and second column contain specific heat.
  • We have used “polyfit” command to find coefficients of first order polynomial by giving input as x and y coordinate and order of equation.
  • By using those coefficients, we have got predicted cp value with help of “polyval” command.
  • We have made two plots in one figure window with the help of hold on command, first plot is of original dataset (cp, temperature) and second plot is of predicted cp and temperature.
  • We have provided xlabel ylabel and legend to plot.
  • Repeat the process for third-degree polynomial and get result.
  • Now we are going to find out parameter which we ensure goodness of fit.
  • First, we have found the Error1 that is difference of cp and predicted cp.
  • We have used for loop to find sum of square of error as it will run length of cp times and each time sum of square of error will be added to SSE1.
  • We have also added the cp in previous for loop to get sum all cp and stored it to SUM1 for finding mean .
  • Also defined that the no of observations is equal to length of cp.
  • Next, we found the mean.
  • Next, we have calculated SSR (sum of square of regression) by using for loop which run as length of cp times and stored to SSR1.
  • Next, we have added SSE1 and SSR1 to SST1.
  • After that we have calculated the R-square parameter which is ratio of SSR1 and SST1
  • Next, we have calculated R-square-adjusted by using formula given in theoretical background.
  • Then we have used if -else statement to check whether the curve is good fitted.
  • Repeat the same procedure to calculate parameter of cubic polynomial.

At this procedure we have know some about polynomial badly conditioned. when the program 1(Linear curve fit) ends the result didn't show the warning of polynomial condition. But program 2(cubic curve fit) and 3(cubic split wise curve fit) had an warning of polynomial is badly conditioned. , we correct it by using the CENTERING AND SCALING method in program 2 (which uses the error estimation structure and the input matrix of mean)and after plotting it there is no warning occurred. Then we compare our curve with the original data set so that difference can be seen easily.

In program 3(cubic split wise curve fit), we have used the dataset split technique and I have split it in four parts of dataset . By using this method a better curve fit is achieved. We can also use the polynomial split technique.

It can be observed when we compare the original data set with the curve fit.

REFERENCES

https://en.wikipedia.org/wiki/Curve_fitting

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

https://statisticsbyjim.com/regression/curve-fitting-linear-nonlinear-regression/

CONCULUTION

Curve fitting of the given data is done by suitable methods and techniques to fit the linear and cubic curve. And the plot to fit linear and cubic polynomial are made. Usage of Curve fitting toolbox will make the work easier. At last we can make curve fit perfect and best fit by increasing order of polynomial equation and split wise method.

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.