Project

Study and Analysis of Curve Fit using Goodness of Fit

ANALYSIS OF CURVE FIT USING GOODNESS CHARACTERISTICS :-

AIM :-

To create a program to perform curve fitting operation and determining the goodness of the curve.

PLATFORM USED :-

  1. Software – MATLAB R2019a
  2. Programming Language – Python

SYSTEM SPECIFICATIONS :-

  1. Manufacturer - DELL
  2. Model – Vostro 3478
  3. Operating System - Windows 10
  4. Processor - Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz 1.80GHz
  5. RAM – 8 GB

DESCRIPTION :-

CURVE FITTING :-

Curve fitting is a process of constructing a curve or a mathematical function, that has the best fit to a series of data points.

FIT CHARACTERISTICS :-

There are four various characteristics which is used to measure goodness of fit using the data points of the curve,

  1. Sum of Squared Errors (SSE)
  2. R-square
  3. Adjacent R-square
  4. Root Mean Squared Error (RMSE)

SUM OF SQUARED ERRORS (SSE) :-

Assume that the data points are (x1,y1), (x2,y2),(x3,y3)………(xn,yn) and the general function that is associated with these datapoints is y=f(x).

The error for the ‘i’ data point are as follows,

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

Now to remove the modulus sign we need to square the error term to make all the error data points to be positive,

SSE (Sum of Squared Errors) = `sum_(i=1)^n Error(i)^2`

Least Sqaure Method,

Assume that the polynomial used here is Quadratic Polynomial,

`f(x) = A x^2+ Bx + C`

  1. By differentiating SSE with each co-efficient of the equation f(x) followed by equating them to zero we can find the minima value.
  2. Then by solving the following equations the co-efficient (A,B&C) values can also be found.
  3. This method is only applicable if the fitting function f(x) is a polynomial.

R-SQUARE :-

  1. It is the quantity that explains the variation of data in the fitting process of the curve.
  2. In other words, it is ration between Sum of Squares of the Regression (SSR) to Sum of Squared Total (SST).

Sum of Squares of Regression (SSR),

It is the difference between value of the fitting function at a point i.e., the predicted value to the arithmetic mean of the data set.

SSR (Sum of Squares of the Regression) = `\sum_{i=1}^{n}((f(x(i))-Mean))^2`

Sum of Squares of Total (SST),

It is total of Sum of Squares of Errors (SSE) and Sum of Squares of Regression (SST).

SST (Sum of Squares of Total) = SSE + SSR

Now the value of is,

`R^2 = \frac{SSR}{SST}`

If value is ‘0’ then it’s the worst possible fit and if is ‘1’ then it’s the best possible fit for the data points.

ADJUSTED R-SQUARE :-

It is used to define the fitness quality of function when we add any other co-efficient to the plot.

`{R^2\ }_{adj} = 1 – [\frac{(1-R^2\ )(n-1)}{n-k-1}]`

Where,

n - Number of Data Points

k - Number of Independent Variables

ROOT MEAN SQUARED ERROR (RMSE) :-

It is known as the fit standard error and standard error of regression.

RMSE (Root Mean Squared Error) = `sqrt \frac{SSE}{n}`

Where,

SSE - Sum of Squared Errors

n - Number of Data Points

INPUT AND OUTPUT PARAMETERS :-

INPUT PARAMETERS :-

  1. Specific Heat (cp) – KJ/(Kmol – K)
  2. Temperature (T) – Kelvin (k)
  3. Total Number of Datapoints (n)
  4. Polynomial Function

OUTPUT PARAMETERS :-

  1. Predicted Specific Heat (pred_cp)
  2. Sum of Squared Errors (SSE) Value
  3. Sum of Squares of the Regression (SSR) Value
  4. Sum of Squares of Total (SST) Value
  5. `R^2` Value
  6. Adjacent `R^2` Value
  7. Root Mean Square Error (RMSE) Value

CODING :-

CODE FOR FITTING LINEAR AND CUBIC POLYNOMIAL FOR CP DATA

CODE :-

% Clearing all the previous memory in the Workspace
clear all
% Terminating all the previous plots
close all
% Clearing all the values in Command Window
clc
 
% Loading the dataset from data file
cp_data = load('data');
% Defining the data for 'cp' and 'temp'
temp = cp_data(:,1);
cp = cp_data(:,2);
 
% FITTING CURVE
% Linear Polynomial
% cp = a*T+b
% Generating the co-efficient for the linear polynomial (a&b)
co_eff = polyfit(temp,cp,1);
% Generating the dataset that fits the curve w.r.t polynomial
pred_cp = polyval(co_eff,temp);
 
% Cubic Polynomial
% cp = a*T^3+b*T^2+c*T+d
% Generating the co-efficient for the linear polynomial (a,b,c&d)
co_eff1 = polyfit(temp,cp,3);
% Generating the dataset that fits the curve w.r.t polynomial
pred_cp1 = polyval(co_eff1,temp);
 
 
% PLOTTING
% Plotting the original dataset
plot(temp,cp,'linewidth',2)
% Retaining the plot
hold on
% Linear Polynomial - generated(pred_cp) 'cp' value
% Plotting the generated cp value w.r.t original 'temp' data
plot(temp,pred_cp,'linewidth',2)
% Retaining the plot
hold on
% Cubic Polynomial - generated(pred_cp1) 'cp' value
% Plotting the generated cp value w.r.t original 'temp' data
plot(temp,pred_cp1,'linewidth',2)
% Terminating the retaining command
hold off
 
% Providing labels to X and Y axes
xlabel('Temperature (k)')
ylabel('Specific Heat (KJ/Kg-k)')
 
% Providing suitable title for the plot
title('Linear & Cubical Polynomial vs Specific Heat (cp)')
 
% Providing the correct legends for better understanding
legend('Original Dataset','Linear Polynomial','Cubical Polynomial','location','northwest') 

PROGRAM PROCESS :-

  1. The compiler starts the program.
  2. Then it loads the data from the specified file name.
  3. Then the appropriate data for ‘cp’ and ‘temp’ are assigned.
  4. Then by using ‘polyfit’ command it generates the co-efficient for linear polynomial (a&b).
  5. Then the dataset is generated for ‘cp’ values i.e., ‘pred_cp’ for fitting the curve.
  6. Then by using ‘polyfit’ command it generates the co-efficient for cubic polynomial (a,b,c&d).
  7. Then the dataset is generated for ‘cp’ values i.e., ‘pred_cp1’ for fitting the curve.
  8. Next the graph is plotted between ‘temp’ and ‘cp’ values from the original dataset.
  9. Then the graph is plotted between generated (linear polynomial) ‘cp’ values ‘pred_cp’ and ‘temp’ values from original dataset.
  10. Then the graph is plotted between generated (cubic polynomial) ‘cp1’ values ‘pred_cp’ and ‘temp’ values from original dataset.
  11. Then the labels are provided for X and Y axes.
  12. Suitable title and legends are provided.
  13. Compiler terminates the program.

WARNING :-

The warning sign is due to the usage of cubical polynomial and to resolve this issue we need to split the complete datapoints into smaller quantities then perform the curve fit using linear and quadratic polynomials.

OUTPUT :-

Graph is plotted between linear polynomial and cubic polynomial curves along with ‘cp’ data from original dataset.

CODE FOR SPLITWISE METHOD

INTRODUCTION :-

In the previous code we received a warning that the ‘Polynomial is Badly Conditioned’, so in order to avoid this error we are using split-wise method. Here we will be splitting the dataset into 4 different datapoints and then perform the curve fitting operation using quadratic polynomial which is the best polynomial for this method.

Splitting Datapoints (‘cp’ & ‘temp’ values),

  1. Range 1 – 1 : 800
  2. Range 2 – 801 : 1600
  3. Range 3 – 1601 : 2400
  4. Range 4 – 2401 : 3200

CODE :-

% Clearing all the previous memory in the Workspace
clear all
% Terminating all the previous plots
close all

% Clearing all the values in Command Window
clc
 
% FOR COMPLETE DATASET
% Loading the dataset from data file
cp_data = load('data');
% Defining the data for 'cp' and 'temp'
temp = cp_data(:,1);
cp = cp_data(:,2);
 
% FITTING CURVE
% Quadratic Polynomial
% cp = a*T^2+b*T+c
[co_eff] = polyfit(temp,cp,1);
[pred_cp] = polyval(co_eff,temp);
 
% SPLITTING THE DATASET
% PREPARING THE DATA
% 1st data point
% Defining the data for 'cp' and 'temp'
temp1 = cp_data(1:800,1);
cp1 = cp_data(1:800,2);
 
% FITTING CURVE
% Quaratic Polynomial
% cp = a*T^2+b*T+c
% Generating the co-efficient for the quadratic polynomial (a,b&c)
co_eff1 = polyfit(temp1,cp1,2);
% Generating the dataset that fits the curve w.r.t polynomial
pred_cp1 = polyval(co_eff1,temp1);
 
% PREPARING THE DATA
% 2nd data point
% Defining the data for 'cp' and 'temp'
temp2 = cp_data(801:1600,1);
cp2 = cp_data(801:1600,2);
 
% FITTING CURVE
% Quaratic Polynomial
% cp = a*T^2+b*T+c
% Generating the co-efficient for the quadratic polynomial (a,b&c)
co_eff2 = polyfit(temp2,cp2,2);
% Generating the dataset that fits the curve w.r.t polynomial
pred_cp2 = polyval(co_eff2,temp2);
 
% PREPARING THE DATA
% 3rd data point
% Defining the data for 'cp' and 'temp'
temp3 = cp_data(1601:2400,1);
cp3 = cp_data(1601:2400,2);
 
% FITTING CURVE
% Quaratic Polynomial
% cp = a*T^2+b*T+c
% Generating the co-efficient for the quadratic polynomial (a,b&c)
co_eff3 = polyfit(temp3,cp3,2);
% Generating the dataset that fits the curve w.r.t polynomial
pred_cp3 = polyval(co_eff3,temp3);
 
% PREPARING THE DATA
% 4th data point
% Defining the data for 'cp' and 'temp'
temp4 = cp_data(2401:3200,1);
cp4 = cp_data(2401:3200,2);
 
% FITTING CURVE
% Quaratic Polynomial
% cp = a*T^2+b*T+c
% Generating the co-efficient for the quadratic polynomial (a,b&c)
co_eff4 = polyfit(temp4,cp4,2);
% Generating the dataset that fits the curve w.r.t polynomial
pred_cp4 = polyval(co_eff4,temp4);
 
% PLOTTING GRAPH
% Plotting the original dataset
plot(temp,cp,'color','black','linewidth',2)
% Retaining the plot
hold on
% Quadratic Polynomial - generated(pred_cp1) 'cp' value
% Plotting the generated cp value for 1st datapoint w.r.t original 'temp' data
plot(temp1,pred_cp1,'linewidth',3)
% Retaining the plot
hold on
% Quadratic Polynomial - generated(pred_cp2) 'cp' value
% Plotting the generated cp value for 2nd datapoint w.r.t original 'temp' data
plot(temp2,pred_cp2,'linewidth',3)
% Retaining the plot
hold on
% Quadratic Polynomial - generated(pred_cp3) 'cp' value
% Plotting the generated cp value for 3rd datapoint w.r.t original 'temp' data
plot(temp3,pred_cp3,'linewidth',3)
% Retaining the plot
hold on
% Quadratic Polynomial - generated(pred_cp4) 'cp' value
% Plotting the generated cp value for 4th datapoint w.r.t original 'temp' data
plot(temp4,pred_cp4,'linewidth',3)
% Terminating the retaining command
hold off
 
% Providing labels to X and Y axes
xlabel('Temperature (k)')
ylabel('Specific Heat (KJ/Kmol-k)')
 
% Providing suitable title for the plot
title('Quadratic Polynomial Polynomial')
 
% Providing the correct legends for better understanding
legend('Original Dataset','Predicted Dataset1','Predicted Dataset2','Predicted Dataset3','Predicted Dataset4','location','northwest');

PROGRAM PROCESS :-

  1. The compiler starts the program.
  2. Then it loads the data from the specified file name.
  3. Then the appropriate data for ‘cp’ and ‘temp’ are assigned.
  4. Then by using ‘polyfit’ command it generates the co-efficient for quadratic polynomial (a,b&c).
  5. Then the dataset is generated for ‘cp’ values i.e., ‘pred_cp’ for fitting the curve.
  6. Then the dataset is split into four datapoints.
  7. The process is repeated for all the four datapoints from step 3 to step 4 and the ‘cp’ values (pred_cp1, pred_cp2, pred_cp3& pred_cp4) are generated.
  8. Next the graph is plotted between ‘temp’ and ‘cp’ values from the original dataset.
  9. Then the graph is plotted between generated (quadratic polynomial) ‘cp’ values ‘pred_cp1’ and ‘temp’ values from original dataset.
  10. The above step is repeated for the rest three datapoints of ‘cp’ values (pred_cp2, pred_cp3& pred_cp4).
  11. Then the labels are provided for X and Y axes.
  12. Suitable title and legends are provided.
  13. Compiler terminates the program.

OUTPUT :-

Graph is Plotted between original dataset and generated dataset using quadratic polynomial.

EVALUATION OF GOODNESS OF FIT :-

The goodness of fit is evaluated for the above split-wise method code using Least Square Method in two different processes,

  1. Coding
  2. Curve Fitting Toolbox

CODING :-

% PERFORMING BEST FIT - FOR DATAPOINT 1:800
% Finding the error for 1st datapoint
error1 = cp1-pred_cp1;
 
% Calculating SSE (Sum of Squared Errors) for 1st datapoint
for i = 1:length(cp1)
    SSE1 = error1(i)^2;
    cp_SUM1 = cp1(i)+0;
end

 
% Mean for calculating SSR - 1st datapoint
n1 = length(cp1);
mean1 = cp_SUM1/n1;
 
% Calculating R^2 for 1st datapoint
% Calculating SSR (Sum of Squares of Regression)
for j = 1:length(pred_cp1)
    SSR1 = (pred_cp1(i)-mean1)^2;
end
 
% Calculating SST (Sum of Squares Total) for 1st datapoint
SST1 = SSR1+SSE1;
 
% R^2 value for 1st datapoint
R_sq1 = SSR1/SST1;
 
% Calculating Adj R^2 for 1st datapoint
k = 1;  % No of independent variable (x)
 
Rsq_adj1 = 1-(((1-R_sq1)*(n1-1))/(n1-k-1));
 
% Calculating RSME (Root Mean Squared Error) for 1st datapoint
RSME1 = (SSE1/n1)^0.5;
 
% PERFORMING BEST FIT - FOR DATAPOINT 801:1600
% Finding the error for 2nd datapoint
error2 = cp2-pred_cp2;
 
% Calculating SSE (Sum of Squared Errors) for 2nd datapoint
for i = 1:length(cp2)
    SSE2 = error2(i)^2;
    cp_SUM2 = cp2(i)+0;
end
 
% Mean for calculating SSR - 2nd datapoint
n2 = length(cp2);
mean2 = cp_SUM2/n2;
 
% Calculating R^2 for 2nd datapoint
% Calculating SSR (Sum of Squares of Regression)
for j = 1:length(pred_cp2)
    SSR2 = (pred_cp2(i)-mean2)^2;
end
 
% Calculating SST (Sum of Squares Total) for 2nd datapoint
SST2 = SSR2+SSE2;
 

% R^2 value for 2nd datapoint
R_sq2 = SSR2/SST2;
 
% Calculating Adj R^2 for 2nd datapoint
k = 1;  % No of independent variable (x)
 
Rsq_adj2 = 1-(((1-R_sq2)*(n2-1))/(n2-k-1));
 
% Calculating RSME (Root Mean Squared Error) for 2nd datapoint
RSME2 = (SSE2/n2)^0.5;
 
% PERFORMING BEST FIT - FOR DATAPOINT 1601:2400
% Finding the error for 3rd datapoint
error3 = cp3-pred_cp3;
 
% Calculating SSE (Sum of Squared Errors) for 3rd datapoint
for i = 1:length(cp3)
    SSE3 = error3(i)^2;
    cp_SUM3 = cp3(i)+0;
end
 
% Mean for calculating SSR - 3rd datapoint
n3 = length(cp3);
mean3 = cp_SUM3/n3;
 
% Calculating R^2 for 3rd datapoint
% Calculating SSR (Sum of Squares of Regression)
for j = 1:length(pred_cp3)
    SSR3 = (pred_cp3(i)-mean3)^2;
end
 
% Calculating SST (Sum of Squares Total) for 3rd datapoint
SST3 = SSR3+SSE3;
 
% R^2 value for 3rd datapoint
R_sq3 = SSR3/SST3;
 
% Calculating Adj R^2 for 3rd datapoint
k = 1;  % No of independent variable (x)
 
Rsq_adj3 = 1-(((1-R_sq3)*(n3-1))/(n3-k-1));
 
% Calculating RSME (Root Mean Squared Error) for 3rd datapoint
RSME3 = (SSE3/n3)^0.5;
 
% PERFORMING BEST FIT - FOR DATAPOINT 2401:3200
% Finding the error for 4th datapoint
error4 = cp4-pred_cp4;
 % Calculating SSE (Sum of Squared Errors) for 4th datapoint
for i = 1:length(cp4)
    SSE4 = error4(i)^2;
    cp_SUM4 = cp4(i)+0;
end
 
% Mean for calculating SSR - 4th datapoint
n4 = length(cp4);
mean4 = cp_SUM4/n4;
 
% Calculating R^2 for 4th datapoint
% Calculating SSR (Sum of Squares of Regression)
for j = 1:length(pred_cp4)
    SSR4 = (pred_cp4(i)-mean4)^2;
end
 
% Calculating SST (Sum of Squares Total) for 4th datapoint
SST4 = SSR4+SSE4;
 
% R^2 value for 4th datapoint
R_sq4 = SSR4/SST4;
 
% Calculating Adj R^2 for 4th datapoint
k = 1;  % No of independent variable (x)
 
Rsq_adj4 = 1-(((1-R_sq4)*(n4-1))/(n4-k-1));
 
% Calculating RSME (Root Mean Squared Error) for 4th datapoint
RSME4 = (SSE4/n4)^0.5;
 
 
% LOOP FOR DETERMINING GOOD FIT USING R^2 VALUE
% Inserting all the R^2 values from 4 datapoints into an array
if [R_sq1 > 0.95,R_sq2 > 0.95,R_sq3 > 0.95,R_sq4 > 0.95]
        disp('The fit is good')
    else
        disp('The fit is not good')
    end

PROGRAM PROCESS :-

  1. The compiler starts the program.
  2. The error is calculated for the 1st set of datapoints and stored in variable ‘error1’.
  3. Then SSE (Sum of Squares Errors) is calculated for 1st data point and stored in variable ‘SSE1’.
  4. Next sum and mean is calculated for 1st data point and stored in variables ‘sum1’ and ‘mean1’ respectively.
  5. Then SSR (Sum of Squares of the Regression),SST (Sum of Squares Total) and values are calculated followed by storing in variables ‘SSR1’,’SST1’ and ‘R_sq1’ respectively.
  6. Next adjacent and RSME (Root Mean Squared Error) values are calculated followed by storing them in variables ‘Rsq_adj1’ and ‘RSME1’.
  7. This process is repeated for 2nd, 3rd and 4th set of data from step 2 to step 6.
  8. Next the curve that is fit is checked by using values from all the four datasets using if-else loop.
  9. Compiler terminates the program.

OUTPUT :-

We are getting the output as ‘The fit is good’.

CURVE FITTING TOOLBOX :-

Using the curve fit toolbox we can cross check whether the plotted graph through coding is a good fit or not.

1ST DATASET :-

2nd DATASET :-

3rd DATASET :-

4th DATASET :-

QUESTION AND ANSWER :-

a) How to make a curve fit perfectly?

Perfect curve fit can be achieved by reducing the errors between the original data and the fitted data. Then select the perfect polynomial for fitting the data w.r.t original data.

b) How to get the best fit?

The best fit can be achieved by calculating the errors using Least Square Method. In this method the errors are reduced and fitted to the data by following calculations,

  1. SSE (Sum of Squares due to Error)
  2. Value
  3. Adjacent Value
  4. RMSE (Root Mean Square Error)

Here the maximum value of is 1 then it’s the best fit.

c) What could be done to improve the cubic fit?

When we use the cubical polynomial equation a warning occurs that ‘Polynomial is Badly Conditioned’, so in order to supress the warning we are using split-wise method. In this method the complete data of ‘Specific Heat (cp) and ‘Temperature (Temp)’ is divided into four datasets. Then after the curve fit is performed individually and then the goodness of fit is verified.

CONCLUSION :-

In the graph before calculating the goodness fit the generated data points don’t match with the original datapoints so we used split-wise method. In this method the errors are reduced by using least square method and the curves are fitted perfectly. The fitness of the curve is decided by `R^2` value.

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.