Project

Curve Fitting using MATLAB/OCTAVE

Aim :- Curve Fitting Using MATLAB/OCTAVE

Software Used :- OCTAVE GUI

Objective:-

  1. Write a program code to fit linear and cubic polynomial for the given data consisting of Specific heat and Temperature.
  2. To plot linear and cubic fit curves along with the original data curve.
  3. To explain statistics and methods used for the goodness of fit.

Introduction :-

Curve fitting is the process of constructing a curve or mathematical function that has the best fit to a series of data points, 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. A related topic is regression analysis, which focuses more on questions of statistical inference such as how much uncertainty is present in a curve that is fit to data observed with random errors. Fitted curves can be used as an aid for data visualization, to infer values of a function where no data are available, and to summarize the relationships among two or more variables.

The first-degree polynomial equation could also be an exact fit for a single point and an angle while the third-degree polynomial equation could also be an exact fit for two points, an angle constraint, and a curvature constraint. Many other combinations of constraints are possible for these and for higher-order polynomial equations.

Regression method

In statistics, regression validation is the process of deciding whether the numerical results quantifying hypothesized relationships between variables, obtained from regression analysis, are acceptable as descriptions of the data. The validation process can involve analyzing the goodness of fit of the regression, analyzing whether the regression residuals are random, and checking whether the model's predictive performance deteriorates substantially when applied to data that were not used in model estimation.

In statistics, polynomial regression is a form of regression analysis in which the relationship between the independent variable x and the dependent variable y is modeled as an nth degree polynomial in x. Polynomial regression fits a nonlinear relationship between the value of x and the corresponding conditional mean of y, denoted E(y |x).

Goodness of Fit :-

The goodness of fit of a statistical model describes how well it fits a set of observations. Measures of goodness of fit typically summarize the discrepancy between observed values and the values expected under the model in question. Such measures can be used in statistical hypothesis testing, for example to test for normality of residuals, to test whether two samples are drawn from identical distributions or whether outcome frequencies follow a specified distribution.

Splitting method- in this method different mathematical equations are used for different intervals of input data to obtain precise curve fitting.

Governing Equations :-

These statistics for parametric models help us to measure the goodness of fit criteria.

  • The sum of squares due to error (SSE)
  • R-square
  • Adjusted R-square
  • Root mean squared error (RMSE)

Polynomial p(x) of degree 'n' is as follows:

p(x) =`p1*x^n + p2*x^(n-1) + ... + p_n*x + p(n+1)`

The error for a general 'i' point can be given as:

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

  1. The sum of squares due to error (SSE)

SSE=`sum _(i=1)^n` Error `(i^2)`

  1. R-square

In statistics, the coefficient of determination denoted R2 or r2 and pronounced "R squared", is the proportion of the variance in the dependent variable that is predictable from the independent variable(s).

Sum of square roots, SSR=`sum_(i=1)^n` `(f(x(i))-Mean)^2`

Sum of squares with error, SSE=`sum_(i=1)^n` `(y(i) - f(x(i)))^2`

Total sum of squares, SST=SSR+SSE

`R^2`=`(SSR)/(SST)`

Typically if `R^2` is greater than 0.95, the curve fit is considered to be good.

  1. Adjusted R-square

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

here

n=number of datapoints

k=number of independent variables

  • If the added variable is insignificant,`R^2` will change very less and 'k' increases; as a result `R^2(adj)` will decrease.
  • If the added variable is significant, 'k' increases and `R^2` will increase greatly; as a result `R^2(adj)` will also increase.
  1. Root mean squared error (RMSE)

The root-mean-square deviation (RMSD) or root-mean-square error (RMSE) is a frequently used measure of the differences between values predicted by a model and the values observed. RMSD is a measure of accuracy, to compare forecasting errors of different models for a particular dataset and not between datasets, as it is scale-dependent.

RMSD is always non-negative, and a value of 0 (almost never achieved in practice) would indicate a perfect fit to the data. In general, a lower RMSD is better than a higher one. However, comparisons across different types of data would be invalid because the measure is dependent on the scale of the numbers used.

RMSE=`sqrt((sum_(i=1)^n (y(i) -f(x(i)))^2)/n`=`sqrt((SSE)/n)`

here n is number of data

Work flow :-

  1. A stored data file containing a set of data for specific heat and temperatures is loaded.
  2. Individual column matrices of specific heat and temperatures are extracted using the function.
  3. The obtained data is then substituted in different polynomial equations that are linear, second degree, and third-degree polynomial equations and then plotted individually with the original data points for comparison.
  4. After that goodness of fit for linear and third-degree polynomials is calculated and plotted using the goodness of fit statistics and methods.
  5. By comparing the obtained variables and plots, we observed that the third-degree polynomial is a good fit than the linear data curve because `R^2` is greater than 0.95.

Program code :-

(1) Program Code to Compare the Original Data Curve with Predicted Linear, Second-degree and Third-degree Polynomial Data Curves

%a program to compare original data curve with n degree polynomial curve
%preparing data
cp_data=load ('data'); %data file values of temp vs cp
temp=cp_data(:,1); %calling temperature data points
cp=cp_data(:,2); %calling specific heat data points

%curve fit
%linear polynomial - cp=a*T + b
coeff1=polyfit(temp,cp,1);
predicted_cp1=polyval(coeff1,temp);

%second degree polynomial - cp=a*T^2 + b*T + c
coeff2=polyfit(temp,cp,2);
predicted_cp2=polyval(coeff2,temp);

%third degree polynomial - cp=a*T^3 + b*T^2 + c*T + d
coeff3=polyfit(temp,cp,3);
predicted_cp3=polyval(coeff3,temp);

% plotting basic data curve
figure(1)
plot(temp,cp,'linewidth',2)
xlabel("Temperature (K)")
ylabel("Specific Heat (Kj/Kmol-K)")
title("Basic Curve")
hold off

%compare actual with predicted curves
%plotting linear curve with basic curve
figure(2)
plot(temp,cp,'linewidth',2);
hold on
plot(temp,predicted_cp1,'linewidth',2,'color','m')
legend('Original data','Linear data','location','northwest')
xlabel("Temperature (K)")
ylabel("Specific Heat (Kj/Kmol-K)")
title("Basic curve vs Linear data curve")
hold off

%plotting second degree polynomial curve with basic curve
figure(3)
plot(temp,cp,'linewidth',2);
hold on
plot(temp,predicted_cp2,'linewidth',2,'color','g')
legend('Original data','Second degree data','location','northwest')
xlabel("Temperature (K)")
ylabel("Specific Heat (Kj/Kmol-K)")
title("Basic curve vs Second degree data curve")
hold off

%plotting third degree polynomial curve with basic curve
figure(4)
plot(temp,cp,'linewidth',2);
hold on
plot(temp,predicted_cp3,'linewidth',2,'color','r')
legend('Original data','Third degree data','location','northwest')
xlabel("Temperature (K)")
ylabel("Specific Heat (Kj/Kmol-K)")
title("Basic curve vs Third degree data curve")

Results/Outputs :-

Workspace :-

Plots :-

Original Data Curve :-

Original Data Curve vs Linear Data Curve :-

Original Data Curve vs Second-degree Data Curve :-

Original Data Curve vs Third-degree Data Curve :-

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

(2) Program Code to Compare and Plot Goodness of Fit of Linear Polynomial and Third-degree Polynomial Data Curves

close all
clear all
clc
% determining goodness of fit for linear fit curve and third degree curve
%preparing data
cp_data=load ('data'); %data file values of temp vs cp
temp=cp_data(:,1);  %calling temperature data points
cp=cp_data(:,2);  %calling specific heat data points

% s=error estimation structure
% mu=centering and scaling values (input matrix of the mean)


% first degree polynomial
% cp=a*T + b
[coeff1,s1,mu1]=polyfit(temp,cp,1);
[predicted_cp1]=polyval(coeff1,temp,s1,mu1);

% third degree polynomial
% cp=a*T^3 + b*T^2 + c*T + d
[coeff3,s3,mu3]=polyfit(temp,cp,3);
[predicted_cp3]=polyval(coeff3,temp,s3,mu3);

% comparing linear curve with original data
plot(temp,cp,'--','linewidth',2)
hold on
plot(temp,predicted_cp1,'linewidth',2,'color','b')
plot(temp,predicted_cp3,'linewidth',2,'color','m')
xlabel("Temperature (K)")
ylabel("Specific Heat (Kj/Kmol-K)")
title("Goodness of Fit(Curve Fitting)")
legend('Original dataset','Linear fit curve','Cubic fit curve','location','northwest')


% goodness of fit linear curve data
% R-square methods
%initial values
s1=0;
for i=1:length(cp)
  s1=s1+cp(i);
end

%mean of cp values
mean1=s1/3200;
% sum of squares of regression
SSR1=0;
% sum of squares due to error
SSE1=0;

for j=1:length(cp)
  SSR1=(SSR1+(predicted_cp1(j)-mean1)^2);
  SSE1=(SSE1+(cp(j)-predicted_cp1(j))^2);
end

% total sum of squares
SST1=SSR1+SSE1;
R_sq1=SSR1/SST1

%R-square adjusting methods
% number of datapoints
n1=3200;
%number of independent variables
k1=1;
R_sq_adj1=1-(((1-R_sq1)*(n1-1))/(n1-k1-1))

%Root mean square methods
RMSE1=sqrt(SSE1/n1)


% goodness of fit third degree polynomial curve data
% R-square methods
%initial values
s3=0;
for i=1:length(cp)
  s3=s3+cp(i);
end

%mean of cp values
mean3=s3/3200;
% sum of squares of regression
SSR3=0;
% sum of squares due to error
SSE3=0;

for j=1:length(cp)
  SSR3=(SSR3+(predicted_cp3(j)-mean3)^2);
  SSE3=(SSE3+(cp(j)-predicted_cp3(j))^2);
end

% total sum of squares
SST3=SSR3+SSE3;
R_sq3=SSR3/SST3

%R-square adjusting methods
% number of datapoints
n3=3200;
%number of independent variables
k3=1;
R_sq_adj3=1-(((1-R_sq3)*(n3-1))/(n3-k3-1))

%Root mean square methods
RMSE3=sqrt(SSE3/n3)

Description of Functions Used in Program Code :-

  • [p,s,mu] = polyfit(x,y,n) also returns mu, which is a two-element vector with centering and scaling values. mu(1) is mean(x), and mu(2) is std(x). Using these values, polyfit centers x at zero and scales it to have a unit standard deviation. This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm.
  • y = polyval(p,x,[],mu) = polyval(p,x,s ,mu) use the optional output mu produced by polyfit to center and scale the data.

Here,

s=error estimation structure

mu=centering and scaling values (input matrix of the mean)

Results/Outputs :-

Workspace :-

Command Window :-

For Linear Polynomial, `R^2` is 0.92485 , whereas for Third-degree Polynomial, `R^2` is 0.99673.

Criteria for a good fit is that `R^2`>0.95.

Hence, the Third-degree Polynomial data curve is a good fit.

Plots :-

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

(3) Program Code to Compare and Plot Linear and Third-degree Polynomial Data Curves using Splitting Method

close all
clear all
clc
%splitting method(linear and third degree polynomials)
%preparing data
cp_data=load ('data'); %data file values of temp vs cp
temp=cp_data(:,1);
cp=cp_data(:,2);

temp1=cp_data(1:580,1);
cp1=cp_data(1:580,2);

temp2=cp_data(581:3200,1);
cp2=cp_data(581:3200,2);

% curve fit using the splitting method
% s=error estimation structure
% mu=centering and scaling values (input matrix of the mean)


% first degree polynomial
% cp=a*T + b
[coeff1,s,mu]=polyfit(temp1,cp1,1);
[predicted_cp1]=polyval(coeff1,temp1,s,mu);
plot(temp,cp,'linewidth',3,'color','m')
hold on
plot(temp1,predicted_cp1,'linewidth',2,'color','b')

% third degree polynomial
% cp=a*T^3 + b*T^2 + c*T + d
[coeff2,s,mu]=polyfit(temp2,cp2,3);
[predicted_cp2,delta]=polyval(coeff2,temp2,s,mu);
plot(temp2,predicted_cp2,'linewidth',2,'color','k')
xlabel("Temperature (K)")
ylabel("Specific Heat (Kj/Kmol-K)")
title("Splitting Method Curve")
legend('Original dataset','Linear fit curve','Cubic fit curve','location','northwest')

Results/Outputs :-

Workspace :-

Plots :-

Splitting Method Curve :-

Some Answers to the Questions :-

1. How to make a curve fit perfectly?

  • Increasing the degree of a polynomial
  • The splitting method can be adopted
  • `R^2` should be 1 or greater than 0.95
  • Centering and scaling errors should be negligible

2. How to get the best fit?

  • Root mean square method can be used
  • Having less residue error and mean square error
  • Greater the value of `R^2`, better will be the fit(`R^2`=1 for a perfect fit)

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

  • Reducing the residue error in the curve fitting.
  • Increasing the number of iterations
  • Splitting the data and implementing it for different subsets

Conclusion :-

  1. The greater the value of `R^2`, the better will be the fit. R ranges from value 0 to 1. 0 means the worst fit and 1 means perfect fit for the data set.
  2. In the adjusted `R^2` method, `R^2`(adj) increases if an added variable is significant and decreases if added variables are insignificant.

Reference :-

  1. https://en.wikipedia.org/wiki/Curve_fitting
  2. http://www.math.utah.edu/~palais/pcr/spike/Evaluating%20the%20Goodness%20of%20Fit%20--%20Fitting%20Data%20(Curve%20Fitting%20Toolbox)%20copy.html
  3. https://in.mathworks.com/help/curvefit/evaluating-goodness-of-fit.html
  4. https://statisticsbyjim.com/regression/interpret-r-squared-regression/
  5. https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
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.