CURVE FITTING
AIM:
TO PERFORM CURVE FITTING FOR GIVEN DATA SET AND FIND IT’S PERFECT FIT
INTRODUCTION:
- What is curve fitting?
Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subjected to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, smoothing, in which a ‘smooth’ function is constructed that approximately fits the data.
- How to make a curve fit perfectly?
Finding a curve which satisfies the data points is done by using 4 quantities which help us to measure the goodness of fit criteria or how well the equation is representing the datapoints. These four quantities are,
1) The sum of squares due to errors (SSE)
2) R-square
3) Adjusted R-square
4) Root mean square error (RMSE)
Using these four quantities, Matlab effectively deduces the goodness of fit curve, typically R-square is greater than 0.95 the fit is assumed to be good.
- How to get the best fit?
To determine the best fit, we can use simple liner regression, eyeball method by drawing a straight line on scatter plot so that so that number of points above and below the line are approximately equal and the line passes through as many points on the plot.
The Split-wise polynomial method can be also used to find the curve fit closest to data set. This split the data into two or more data folds and do the curve fit. Third order polynomial gives the best fit to the given data by using split-wise polynomial method.
- What could be done to improve the cubic fit?
1) Reduce the convergence tolerance
2) Sub-setting the data and fitting different sub-sets of data separately
3) Increase the number of iterations or function evaluation is allowed
4) Using split-wise method and then performing cubic fit in each interval
GOVERNING EQUATIONS:
1) Sum of squared errors (SSE)
`SSE = Σ(Y(i) - f(x(i)))^2`
Where,
f(x(i)) = Mean
2) R-square
`R^2 = (SSR)/(SST)`
Where,
SSR = sum of square of regression, which is square of the difference between the value of the fitting function at a given point and the arithmetic mean of data set.
If y = f(x) is the fit curve then the SSR is defined as,
`SSR = Σ(f(x(i)) - Mean)^2`
`SSE = Σ(Y(i) - f(x(i)))^2`
Now, SST is given by
SST = SSR + SSE
This give us the value of R-square
3) Adjusted R-square
`R^2adj = 1 - [ ((1-R^2)(n-1)) / (n – k – 1) ]`
Where,
n = no. of data points
k = no. of independent variables
Adjusted R-square disregards infinitesimally small increments to R-square, and only consider the variables which actually contribute to the output of the system in a drastic way.
4) Root Mean Squared Error
`RMSE = sqrt((Σ(Y(i) - f(x(i)))^2) / n) = sqrt ((SSE)/ n)`
Where,
n = total no. of data points available
OBJECTIVES:
1) LINEAR AND CUBIC CURVE FITTING FOR GIVEN Cp VS TEMPERATURE DATA
2) CURVE FITTING USING SPLIT POLYNOMIAL TECHNIQUE
3) TO FIND THE GOODNESS OF THE FIT
The given data file contains 3200 rows and two columns i.e. temperature and specific heat.
Common commands used in starting of every program-
Clear all command for clearing workspace
Close all command for closing plots and figures in background
Clc command for clearing the command window
PROGRAM:
OCTAVE PROGRAM 1- ORIGINAL DATASET
clear all
close all
clc
% Loading the given data
cp_data = load('data1');
% Assigning the given data
temperature = cp_data(:,1);
cp = cp_data(:,2);
% Plotting the given data
plot(temperature, cp, 'linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
title('Original dataset')
Fig.1. Original dataset
Explanation:
Line1- The ‘load’ command imports the data into workspace, make sure the data file is present in the same directory as the current folder.
Line2- The first column data is extracted and stored in the vector called temperature.
Line3- The second column data is extracted and stored in the vector called cp_data.
Line4- Plotting the given data
Line5- Labelling x-axis and y-axis
Line6- Adding title to the plot
OCTAVE PROGRAM 2- LINEAR FIT
clear all
close all
clc
% Preparing the data
cp_data = load('data1');
temperature = cp_data(:,1);
cp = cp_data(:,2);
% Curve fit
% Linear polynomial
% cp = a*T + b
coeffs_linear = polyfit(temperature, cp,1);
linear_fit = polyval(coeffs_linear, temperature);
% Compare the linear fit with original data
plot(temperature, cp, 'linewidth', 4,'color','b');
hold on;
plot(temperature, linear_fit, 'linewidth', 4, 'color','r');
title('Linear fit vs Original data')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original dataset', 'Linear fit' , 'location', 'northwest')
Fig.2. Linear fit Vs Original data
Explanation:
Line1- Importing the data, extracting and storing it in a variable
Line2- Linear coefficient of polynomial which is stored in variable ‘coeffs_linear
Line3- New data set is generated
Line4- Plotting the original data
Line5- Plotting the linear fit which will intersect the graph at two points
Line6- Adding titles and labels to the plot
OCTAVE PROGRAM 3- CUBIC FIT
clear all
close all
clc
% Preparing the data
cp_data = load('data1');
temperature = cp_data(:,1);
cp = cp_data(:,2);
% Curve fit
% Third degree polynonmial
% cp = a*T^3 + b*T^2 + c*T + d
co_effs = polyfit(temperature,cp,3);
predicted_cp = polyval(co_effs, temperature);
% Compare curve fit with original data
plot(temperature, cp, 'linewidth',3)
hold on
plot(temperature, predicted_cp, 'linewidth', 3, 'color', 'r')
title('CURVE FITTING')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original dataset', 'Cubic fit curve' , 'location', 'northwest')
Fig.3. Cubic fit Vs Original data
Explanation:
Line1,2&3- Importing the data, extracting and storing it in a variable
Line4- This will create coefficient for cubic polynomial and stores it in variable ‘co_effs’
Line5- New data set is generated
Line6- Plotting the original data
Line7- Plotting the cubic fit which will intersect the graph at two points
Line8- Adding titles and labels to the plot
OCTAVE PROGRAM 4- SCALING AND CENTERING
clear all
close all
clc
% Preparing the data
cp_data = load('data1');
temperature = cp_data(:,1);
cp = cp_data(:,2);
% Curve fit
% S = Error estimation structure
% mu = Centering and scaling values (Input matrix of mean)
% 'delta' represents standard error for prediction
% First degree polynomial
% cp = a*T + b
[co_effs,S,mu] = polyfit(temperature,cp,1);
[predicted_cp] = polyval(co_effs, temperature, S, mu);
% Compare my curve fit original data
plot(temperature,cp, '--', 'linewidth',3)
hold on
plot(temperature, predicted_cp, 'linewidth',2 , 'color','r')
hold on
xlabel('Temperature [k]')
ylabel('Specific Heat [KJ/Kmol-K]')
% Third degree polynomial
% cp = a*T^3 + b*T^2 + c*T + d
[co_effs,S,mu] = polyfit(temperature,cp,3);
[predicted_cp, delta] = polyval(co_effs, temperature, S, mu);
plot(temperature, predicted_cp, 'linewidth', 3, 'color', 'g');
title('Curve Fitting')
xlabel('Temperature [k]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original dataset', 'linear fit', 'Cubic fit curve' , 'location', 'northwest')
Fig.4. Original data vs linear fit vs cubic fit curve
Explanation:
Line1,2&3- Importing the data, extracting and storing it in a variable
Line4- This will create coefficient for cubic polynomial and stores it in variable ‘co_effs’ . The command ‘polyfit’ computes least squares polynomial for the given set of data. Polyfit generates coefficients of polynomial which is used to model a curve to fit the data.
Now in polyfit command while calculating coefficients, we will also calculate error estimation structure (S) and scaling and centering values (mu).
Line5- New data set is generated
The command ‘polyval’ evaluates the given set of values, generating a curve to fit the data based on the coefficients found using. ‘polyfit’.
Line6- Plotting the original data and linear fit which will intersect the graph at two points
Line7- Plotting the cubic fit
Line8- Adding labels and title to the plot
OCTAVE PROGRAM 5- SPLIT-WISE METHOD
clear all
close all
clc
% Preparing the data
cp_data = load('data1');
% Making sub-intervals of temperature
temperature = cp_data(:,1);
temperature1 = cp_data(1:700,1);
temperature2 = cp_data(701:3200,1);
% Making sub-intervals of specific heat
cp = cp_data(:,2);
cp1 = cp_data(1:700,2);
cp2 = cp_data(701:3200,2);
% Curve fit
% S = Error estimation structure
% mu = Centering and scaling values (Input matrix of mean)
% 'delta' represents standard error for prediction
% Plotting the curve fit with the piecewise splitting method
% First degree polynomial
% cp = a*T + b
[co_effs_1,S,mu] = polyfit(temperature1,cp1,1);
[predicted_cp1] = polyval(co_effs_1, temperature1, S, mu);
plot(temperature,cp, '--', 'linewidth',3 , 'color' , 'b')
hold on
plot(temperature1, predicted_cp1, 'linewidth',2 , 'color','r')
% Third degree polynomial
% cp = a*T^3 + b*T^2 + c*T + d
[co_effs_2,S,mu] = polyfit(temperature2,cp2,3);
[predicted_cp2, delta] = polyval(co_effs_2, temperature2, S, mu);
plot(temperature2, predicted_cp2, 'linewidth', 3, 'color', 'g');
title('Curve Fitting')
xlabel('Temperature [k]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original dataset', 'linear fit', 'Cubic fit curve' , 'location', 'northwest')
Fig.5. Polynomial Splitting method
Explanation:
Line1- Importing the data
Line2,3&4- Partition of temperature domain into 3 sub intervals
Line5,6&7- Partition of specific heat domain into 3 sub intervals
Line8- For first degree polynomial, this will create coefficient for cubic polynomial and stores it in variable ‘co_effs’ . The command ‘polyfit’ computes least squares polynomial for the given set of data. Polyfit generates coefficients of polynomial which is used to model a curve to fit the data.
Now in polyfit command while calculating coefficients, we will also calculate error estimation structure (S) and scaling and centering values (mu)
Line9- New data set is generated
The command ‘polyval’ evaluates the given set of values, generating a curve to fit the data based on the coefficients found using. ‘polyfit’.
Line10- Plotting original data
Line11- Retaining the data
Line12- Plotting linear fit
Line13- For third degree polynomial, this will create coefficient for cubic polynomial and stores it in variable ‘co_effs’ . The command ‘polyfit’ computes least squares polynomial for the given set of data. Polyfit generates coefficients of polynomial which is used to model a curve to fit the data.
Now in polyfit command while calculating coefficients, we will also calculate error estimation structure (S) and scaling and centering values (mu)
Line14- New data set is generated
The command ‘polyval’ evaluates the given set of values, generating a curve to fit the data based on the coefficients found using. ‘polyfit’.
Line15- Plotting the cubic fit
Line16- Adding labels and title to the plot
OCTAVE PROGRAM 6- GOODNESS OF LINEAR FIT
clear all
close all
clc
% Preparing the data
cp_data = load('data1');
temperature = cp_data(:,1);
cp = cp_data(:,2);
% Curve fit
% S = Error estimation structure
% mu = Centering and scaling values (Input matrix of mean)
% 'delta' represents standard error for prediction
% First degree polynomial
% cp = a*T + b
[co_effs,S,mu] = polyfit(temperature,cp,1);
[predicted_cp] = polyval(co_effs, temperature, S, mu);
% Compare my curve fit original data
plot(temperature,cp, '--', 'linewidth',3, 'color' , 'b')
hold on
plot(temperature, predicted_cp, 'linewidth',2 , 'color','r')
hold on
xlabel('Temperature [k]')
ylabel('Specific Heat [KJ/Kmol-K]')
title('Curve Fitting')
legend('Original dataset', 'linear fit', 'location', 'northwest')
% Goodness of fit
% Finding R-square
% Initial value
S = 0;
for i = 1:length(cp)
S = S + cp(i)
endfor
% Mean of cp values
mean = S / 3200;
% Initial values
SSR = 0;
SSE = 0;
for j = 1:length(cp)
SSR = (SSR + (predicted_cp(j) - mean)^2);
SSE = (SSE + (cp(j) - predicted_cp(j))^2);
endfor
SST = SSR + SSE;
R_sq = SSR / SST;
% Finding adjusted R-square
n = 3200;
k = 1;
adj_R_sq = 1 - [ ((1-R_sq)*(n-1)) / (n-k-1) ]
% Finding Root Mean Square
RMSE = (SSE/n)
Fig.6. Goodness of Linear Fit
Fig.7. Output of Linear Fit
Explanation:
Line1,2&3- Importing the data, extracting and storing it in a variable
Line4- Linear coefficient of polynomial which is stored in variable ‘coeffs_linear’. The command ‘polyfit’ computes least squares polynomial for the given set of data. Polyfit generates coefficients of polynomial which is used to model a curve to fit the data.
Line5- New data set is generated.
The command ‘polyval’ evaluates the givenn set of values, generating a curve to fit the data based on the coefficients found using. ‘polyfit’.
Line6- Plotting original data
Line7- Retaining the data
Line8- Plotting linear fit
Line9- Adding labels and title to the plot
Line10- Error estimation initial value
Line11- We use ‘for-loop’ command for iteration of ‘i' from given data
Line12- New value for error estimation structure
Line13- End ‘for-loop’ command for ‘i’
Line14- Finding mean value of specific heat
Line15- Initial value for sum of squared regression
Line16- Initial value for sum of squared errors
Line17- We use ‘for-loop’ command for iteration of ‘j' from given data
Line18- New value for sum of squared regression
Line19- New value for sum of squared errors
Line20- End ‘for-loop’ command for ‘j’
Line21- New value for SST is defined
Line22- R_sq value is defined
Line23- Number of data points
Line24- Number of independent variable
Line25- Adjusted R_sq is defined
Line26- Root Mean Square Error is defined
Root Mean Square Error = 675.95
R_square = 0.92488
Adjusted R_square = 0.92485
OCTAVE PROGRAM 7- GOODNESS OF CUBIC FIT
clear all
close all
clc
% Preparing the data
cp_data = load('data1');
temperature = cp_data(:,1);
cp = cp_data(:,2);
% Curve fit
% S = Error estimation structure
% mu = Centering and scaling values (Input matrix of mean)
% 'delta' represents standard error for prediction
% Third degree polynomial
% cp = a*T^3 + b*T^2 + c*T + d
[co_effs,S,mu] = polyfit(temperature,cp,3);
[predicted_cp, delta] = polyval(co_effs, temperature, S, mu);
plot(temperature, predicted_cp, 'linewidth', 3, 'color', 'g');
% Compare my curve fit original data
plot(temperature,cp, '--', 'linewidth',3)
hold on
plot(temperature, predicted_cp, 'linewidth',2 , 'color','r')
hold on
xlabel('Temperature [k]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original dataset', 'linear fit', 'Cubic fit curve' , 'location', 'northwest')
% Goodness of fit
% Finding R-square
% Initial value
S = 0;
for i = 1:length(cp)
S = S + cp(i)
endfor
% Mean of cp values
mean = S / 3200;
% Initial values
SSR = 0;
SSE = 0;
for j = 1:length(cp)
SSR = (SSR + (predicted_cp(j) - mean)^2);
SSE = (SSE + (cp(j) - predicted_cp(j))^2);
endfor
SST = SSR + SSE;
R_sq = SSR / SST;
% Finding adjusted R-square
n = 3200;
k = 1;
adj_R_sq = 1 - [ ((1-R_sq)*(n-1)) / (n-k-1) ]
% Finding Root Mean Square
RMSE = (SSE/n)
Fig.8. Goodness of Cubic Fit
Fig.9. Output of Cubic Fit
Explanation:
The code is similar as goodness of linear fit but here we use it for finding goodness of cubic fit.
All the formulas are same as above code for finding R_sq,adjusted R_sq,RMSE
Root Mean Square Error = 29.460
R_square = 0.99673
Adjusted R_square = 0.99672
From the above results, the best fit for the given data is cubic fit as the value of R_square (0.99672) defines the goodness of best fit.
CONCLUSION: WE HAVE STUDIED THE IMPORTANANCE AND APPLICATION OF CURVE FITTING USING MATLAB / OCTAVE. WE HAVE SEEN LARGE DATA CAN BE EASILY HANDELED AND GIVEN GRAPHICAL REPRESENTATION TO FIND THE BEST FIT MODEL OF THE RELATIONSHIP FOR THE GIVEN DATA.
REFRENCES:
1. https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
2. https://en.wikipedia.org/wiki/Curve_fitting