Performing curve fitting in octave
AIM:
To write a program in MATLAB/Octave to perform curve fitting.
OBJECTIVES:
- To write a program in MATLAB/Octave to perform curve fitting for the data points stored in the file named ‘data’.
- To write a program in MATLAB/Octave to fit a linear and cubic curve for the given data points.
- To explain the methods used to evaluate the goodness of fit.
THEORY:
Curve fitting is a technique to model or represent a data spread by assigning a ‘best fit’ function (curve) along the entire range. The mathematical function established from a given set of data points provides relationship between dependent and independent variables.
Types of Curve fitting:
- Interpolation: In this, a function discovered is the exact fit to the data points. Since this assumes no measurement error, it has limited applicability to the real life scenarios.
- Smoothing: In this, a function discovered is the approximate fit to the data points and aim is to minimize the total error.
The data points can be fitted to the curve through the polynomial functions such as linear, quadratic, cubic and so on and also through trigonometric function, logarithmic function and exponential function.
METHODS TO EVALUATE THE GOODNESS OF FIT:
- Sum of squares due to error (SSE): It is the sum of squares of residuals. (Deviations predicted from actual empirical values of data.) It is also known as the least square fitting method. A value closer to 0 indicates a better fit.
- 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 coefficients and the coefficient of multiple determinations. R-square is defined as the ratio of the sum of squares of the regression (SSE) and the total sum of squares (SST).
Mathematically,
`R^2= bb"SSE/SST"`
Now Since `SST = SSR+ SSE`
Therefore,
`R^2 = 1-bb"SSR/SST"`
Where,
SST (Total sum of squares) = `∑(Y_i-barY )^2`
SSR (Regression sum of squares) = `∑(hatY _i-barY )^2`
SSE (Error sum of squares) `∑(Y_i-barY _i )^2`
`barY =`Average value of the dependent variable.
`Y_i =`Observed value of dependent variable.
`hat Y _i =`Predicted value for Y for the given `X_i` value.
R square can take on any values between 0 to 1.The value closer to 1 indicate a better fit. For example, `R^2`value of 0.5 means that the fit explains 80.45% of the total variation in the data about the average.
LIMITATION OF `R^2`: R-squared does not indicate whether a regression model is adequate. You can have a low R-squared value for a good model, or a high R-squared value for a model that does not fit the data. For this, Adjusted R square method should be used.
- Adjusted R-Square: Adjusted R-squared value can be calculated based on value of r-squared, number of independent variables (predictors), total sample size.
`R^2 adjusted =``1-((1-R^2 )(N-1))/(N-P-1)`
Where,
`R^2`= Samples of R-square
P = Number of predictors (independent variables)
N = Total sample size
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.
- Root mean square error (RSME): It is the standard deviation of the residuals (prediction error). Residuals measures how far are the data points from the regression line.
A RSME value closer to 0 indicates a better fit.
`RSME = sqrtbb"SSE"/n`
Where,
n = Number of data points.
PROGRAM: To perform curve fitting (linear and cubic) for the data points stored in the file named ‘data’.
%Program to perform curve fitting (linear and cubic) and to compare original
%datasheet with the fitted curve.
close all
clear all
clc
%loading the data
cp_data = load('data');
%getting temperature and specific heat data from column 1 and column 2
%respectively.
temperature= cp_data(:,1);
cp = cp_data(:,2);
%Curve fit
% linear curve fit (cp = aT+c)
% Obtaining the coefficients(a&c) and predicting the cp for linear curve fit.
coeff1=polyfit(temperature,cp,1);
predicted_cp1=polyval(coeff1,temperature);
% cubic curve fit (cp = aT^3+bT^2+cT+d)
% Obtaining the coefficients(a,b,c&d) and predicting the cp for cubic curve fit.
coeff3= polyfit(temperature,cp,3);
predicted_cp3= polyval(coeff3,temperature);
%plotting linear curve fit
figure(1)
hold on
plot(temperature,cp,'linewidth',3,'color','r');
plot(temperature,predicted_cp1,'linewidth',3,'color','g');
xlabel('Temperature [K]');
ylabel('Specific heat[KJ/Mol-K');
legend('origional data values','linear curve fit');
title('Linear curve fit');
%plotting cubic curve fit
figure(2)
hold on
plot(temperature,cp,'linewidth',3,'color','r');
plot(temperature,predicted_cp3,'linewidth',3,'color','b');
xlabel('Temperature [K]');
ylabel('Specific heat[KJ/Mol-K');
legend('original data values','Cubic curve fit');
title('Cubic curve fit');
%Comparison between linear curve fit and cubic curve fit
figure(3)
hold on
plot(temperature,cp,'linewidth',3,'color','r');
plot(temperature,predicted_cp1,'linewidth',3,'color','g');
plot(temperature,predicted_cp3,'linewidth',3,'color','b');
xlabel('Temperature [K]');
ylabel('Specific heat[KJ/Mol-K');
legend('original data values','linear curve fit','Cubic curve fit');
title('Comparison between linear curve fit and cubic curve fit');
% dividing the data points into sub regions and fitting the curve separately
% for each regions
%loading the data
cp_data=load('data');
%splitting the data points into two regions having 1600 data points each
temp_1= cp_data(1:1600,1);
cp_split1=cp_data(1:1600,2);
temp_2 = cp_data(1600:3200,1);
cp_split2=cp_data(1600:3200,2);
% Fitting cubic curve separately in each split region.
%Defining coefficients and predicted cp for first split region
coeff_1=polyfit(temp_1,cp_split1,3);
cp_3_1=polyval(coeff_1,temp_1);
%Defining coefficients and predicted cp for first split region
coeff_2=polyfit(temp_2,cp_split2,3);
cp_3_2=polyval(coeff_2,temp_2);
%Cubic curve fitting for split regions and making comparison between simple
%cubic curve fit and cubic curve fit for split regions
figure(4)
hold on
plot(temp_1,cp_split1,'linewidth',3,'color','r',";original data points;");
plot(temp_2,cp_split2,'linewidth',3,'color','r');
plot(temperature,predicted_cp3,'linewidth',3,'color','b',";simple cubic curve fit;");
plot(temp_1,cp_3_1,'linewidth',3,'color','k');
plot(temp_2,cp_3_2,'linewidth',3,'color','k',";cubic curve fit by splitting regions;");
xlabel('Temperature [K]');
ylabel('Specific heat[KJ/Mol-K');
title('Simple cubic curve fit v/s Cubic curve fit by splitting regions');
EXPLANATION:
Loading the data values stored in the file named ‘data’ by using ‘load’ command. The data points from the file ‘data’ are stored in the variable named ‘cp_data’
Note: The file from which we are loading the data points should be present in the same directory where the MATLAB/Octave programs are saved.
%Program to perform curve fitting (linear and cubic) and to compare original
%datasheet with the fitted curve.
close all
clear all
clc
%loading the data
cp_data = load('data');
First column of ‘cp_data’ is defined as ‘temperature’ and second column is defined as specific heat ‘cp’.
%getting temperature and specific heat data from column 1 and column 2
%respectively.
temperature= cp_data(:,1);
cp = cp_data(:,2);
Obtaining the coefficients through ‘polyfit’ command and then predicting the specific heats for linear and cubic polynomial function through ‘polyval’ command.
‘polyfit’ command is used for generating the coefficient of the polynomial which is used to model the curve to fit the data.
y = polyval(p,x) returns the value of polynomial of degree n evaluated at x. The input argument p is a vector of length n+1. In case of linear polynomial function, degree (n) equals to 1 therefore coefficient (p) equals to 2. Similarly for cubic polynomial function, degree (n) equals to 3 therefore coefficient (p) equals to 4.
%Curve fit
% linear curve fit (cp = aT+c)
% Obtaining the coefficients(a&c) and predicting the cp for linear curve fit.
coeff1=polyfit(temperature,cp,1);
predicted_cp1=polyval(coeff1,temperature);
% cubic curve fit (cp = aT^3+bT^2+cT+d)
% Obtaining the coefficients(a,b,c&d) and predicting the cp for cubic curve fit.
coeff3= polyfit(temperature,cp,3);
predicted_cp3= polyval(coeff3,temperature);
Plotting linear curve fit and cubic curve fit on figure (1) and figure (2) respectively.
%plotting linear curve fit
figure(1)
hold on
plot(temperature,cp,'linewidth',3,'color','r');
plot(temperature,predicted_cp1,'linewidth',3,'color','g');
xlabel('Temperature [K]');
ylabel('Specific heat[KJ/Mol-K');
legend('origional data values','linear curve fit');
title('Linear curve fit');
%plotting cubic curve fit
figure(2)
hold on
plot(temperature,cp,'linewidth',3,'color','r');
plot(temperature,predicted_cp3,'linewidth',3,'color','b');
xlabel('Temperature [K]');
ylabel('Specific heat[KJ/Mol-K');
legend('original data values','Cubic curve fit');
title('Cubic curve fit');
In order to compare the perfectness of fit, linear curve fit and cubic curve fit are plotted on the same graph. (Figure 3)
%Comparison between linear curve fit and cubic curve fit
figure(3)
hold on
plot(temperature,cp,'linewidth',3,'color','r');
plot(temperature,predicted_cp1,'linewidth',3,'color','g');
plot(temperature,predicted_cp3,'linewidth',3,'color','b');
xlabel('Temperature [K]');
ylabel('Specific heat[KJ/Mol-K');
legend('original data values','linear curve fit','Cubic curve fit');
title('Comparison between linear curve fit and cubic curve fit');
CURVE FITTING BY SPLITTING THE DATA
Data is split down into two regions and curve is plotted separately for each region. In order to compare the perfectness of fit, Simple cubic curve fit and cubic curve fit obtained by splitting the regions are plotted on the same graph. (Figure 4)
% dividing the data points into sub regions and fitting the curve separately
% for each regions
%loading the data
cp_data=load('data');
%splitting the data points into two regions having 1600 data points each
temp_1= cp_data(1:1600,1);
cp_split1=cp_data(1:1600,2);
temp_2 = cp_data(1600:3200,1);
cp_split2=cp_data(1600:3200,2);
% Fitting cubic curve separately in each split region.
%Defining coefficients and predicted cp for first split region
coeff_1=polyfit(temp_1,cp_split1,3);
cp_3_1=polyval(coeff_1,temp_1);
%Defining coefficients and predicted cp for first split region
coeff_2=polyfit(temp_2,cp_split2,3);
cp_3_2=polyval(coeff_2,temp_2);
%Cubic curve fitting for split regions and making comparison between simple
%cubic curve fit and cubic curve fit for split regions
figure(4)
hold on
plot(temp_1,cp_split1,'linewidth',3,'color','r',";original data points;");
plot(temp_2,cp_split2,'linewidth',3,'color','r');
plot(temperature,predicted_cp3,'linewidth',3,'color','b',";simple cubic curve fit;");
plot(temp_1,cp_3_1,'linewidth',3,'color','k');
plot(temp_2,cp_3_2,'linewidth',3,'color','k',";cubic curve fit by splitting regions;");
xlabel('Temperature [K]');
ylabel('Specific heat[KJ/Mol-K');
title('Simple cubic curve fit v/s Cubic curve fit by splitting regions');
OUTPUT GRAPHS:
GRAPH 1: Linear curve fit.
GRAPH 2 : Cubic curve fit
GRAPH 3: Comparison between linear curve fit and cubic curve fit.
GRAPH 4: Comparison between simple cubic curve fit and cubic curve fit obtained by splitting the data.
RESULT: Graph 3 clearly shows that the cubic curve fit is much more perfect than linear curve fit. Therefore to make the curve perfectly fit, degree of polynomial should be increased but blindly increasing it doesn’t result in better fit in all cases. Therefore data points should be divided into sub regions and curve to be fitted for each region separately.
From Graph 4 clearly shows that the cubic curve obtained by splitting the data points into two regions is better fit when compared with the simple cubic curve fit.
CONCLUSION:
- Linear curve fitting and cubic curve fitting have been successfully performed through the program written in octave.
- Different methods to evaluate the perfectness of fit are explained.