Project

Study of the Curve Fitting using Matlab and Validation using Matlab Toolbox

Aim:- To perform curve fitting using Linear fitting and Cubic fitting from a given dataset. And find the best fit and perfect fit.

Objective:-

  • Write code to fit a linear and cubic polynomial for the Cp data.
  • Plot the linear and cubic fit curves along with the raw data points. Title and axes labels are a must, legends could be shown if necessary.
  • Explain the parameters used to measure the fitness characteristics for both the curves.

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

Extrapolation refers to the use of a fitted curve beyond the range of the observed data, and is subject to a degree of uncertainty since it may reflect the method used to construct the curve as much as it reflects the observed data.

Parameters used to measure the fitness characteristics for both the curves:-

  • The sum of squares due to error (SSE)
  • R-square
  • Adjusted R-square
  • 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.

SSE=n`sum`i=1wi(yi−ˆyi)2

A value closer to 0 indicates that the model has a smaller random error component, and that the fit will be more useful for prediction.

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). SSR is defined as

SSR=n'sum'i=1wi(ˆyi−‾y)2

SST is also called the sum of squares about the mean, and is defined as

SST=n'sum'i=1wi(yi−‾y)2

where SST = SSR + SSE. Given these definitions, R-square is expressed as

R-square=SSR/SST=1−(SSE/SST)

R-square can take on any value between 0 and 1, with a value closer to 1 indicating that a greater proportion of variance is accounted for by the model.

Degrees of Freedom Adjusted R-Square:- This statistic uses the R-square statistic defined above, and adjusts it based on the residual degrees of freedom. The residual degrees of freedom is defined as the number of response values n minus the number of fitted coefficients m estimated from the response values.

v = n – m

v indicates the number of independent pieces of information involving the n data points that are required to calculate the sum of squares.

Root Mean Squared Error:- This statistic is also known as the fit standard error and the standard error of the regression.

RMSE=s=sqrt(MSE)

where MSE is the mean square error or the residual mean square

MSE=SSE/v

Just as with SSE, an MSE value closer to 0 indicates a fit that is more useful for prediction.

CODE:-

Linear fit:-

clear all
close all
clc

% Loading Data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);

% Linear fit
%cp=a*T+b
coeff = polyfit(temperature,cp,1);   % Function used to find the value of a and b
cp_pred = polyval(coeff,temperature);  %Function used to predict cp

%plotting the data
plot(temperature,cp,'linewidth',2,'color','b')  %original curve
hold on
plot(temperature,cp_pred,'linewidth',2,'color','r')  % predicted curve
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ/K-mole)')
title('Linear Fitting')
axis([0 5000 800 1500])
legend('Original data','Predicted data')

Cubic Fit:-

clear all
close all
clc

% Loading Data
cp_data=load('data');
temperature=cp_data(:,1);  
cp=cp_data(:,2);

% Cubic fit
%cp=a*T^3+b*T^2+c*T+d
coeff = polyfit(temperature,cp,3);   % Function used to find the value of a and b
cp_pred = polyval(coeff,temperature);  %Function used to predict cp

%plotting the data
plot(temperature,cp,'linewidth',2,'color','b')  %original curve
hold on
plot(temperature,cp_pred,'linewidth',2,'color','r')  % predicted curve
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ/K-mole)')
title('Cubic Fitting')
axis([0 5000 800 1500])
legend('Original data','Predicted data')

Steps of Program:-

  • Firstly, we will load data using load command and then we assign the temperature column data and cp column data.
  • Now we will fit the data using two functions that are polyfit and polyval command.
  • Polyfit command used to find the constant values of the degree of polynomials and polyval command used to find the predicted value of specific heat.
  • Then we will plot the original data and then we will plot the predicted data.

Error faced during execution:-

Written wrong spelling of the temperature while plotting as assigned before in the data and Now it is corrected

Assigned the incorrect vlaue input in the axis which is greater and inaccurate and above the axis limit which undefines the curve fitting and then it is corrected.

Results:-

Linear Fit:-

Cubic Fit:-

Validation using Curve Fitting Toolbox:-

Curve Fitting Toolbox™ provides an app and functions for fitting curves and surfaces to data. The toolbox lets you perform exploratory data analysis, preprocess and post-process data, compare candidate models, and remove outliers.

Results:-

Linear Fit:-

Cubic Fit:-

How to make a curve fit perfectly?

To fit the curve perfectly we have to increase the degree of order of the polynomial to get perfect fit of ASME=1.

How to get the best fit?

The degree of the polynomial affects the ‘detail’ to which it approximates your data. A first-degree polynomial is a simple linear fit: y=b(1)*x+b(2), the degree being one less than the number of parameters the polynomial estimates. The higher the polynomial degree, the more closely the polynomial fits the data. Lower degrees have the effect of ‘smoothing’ the fit, acting as a sort of lowpass filter. The highest possible degree is one less than the number of data points to be fit, although a polynomial degree higher than about 7 does not add any significant information. Choosing the polynomial degree (as with most other aspects of signal processing) is a matter of experimenting to determine what works best.

What could be done to improve the cubic fit?

During cubic fitting we the matlab throws as error that is:-

This error can be corrected by using center and scale during fitting of our curve.

To improve the cubic fitting we will split the data into small subparts. In our program we will make 4 subparts.

clear all
close all
clc

% Loading Data
cp_data = load('data');
temperature = cp_data(:,1);  
cp = cp_data(:,2);

% Spliting data into subparts
temperature1 = cp_data(1:800,1);
cp1 = cp_data(1:800,2);

temperature2 = cp_data(801:1600,1);
cp2 = cp_data(801:1600,2);

temperature3 = cp_data(1601:2400,1);
cp3 = cp_data(1601:2400,2);

temperature4 = cp_data(2401:3200,1);
cp4 = cp_data(2401:3200,2);

% Cubic fit
%cp=a*T^3+b*T^2+c*T+d
coeff1 = polyfit(temperature1,cp1,3);   % Function used to find the value of a and b
cp_pred1 = polyval(coeff1,temperature1);  %Function used to predict cp

coeff2 = polyfit(temperature2,cp2,3);   % Function used to find the value of a and b
cp_pred2 = polyval(coeff2,temperature2);  %Function used to predict cp

coeff3 = polyfit(temperature3,cp3,3);   % Function used to find the value of a and b
cp_pred3 = polyval(coeff3,temperature3);  %Function used to predict cp

coeff4 = polyfit(temperature4,cp4,3);   % Function used to find the value of a and b
cp_pred4 = polyval(coeff4,temperature4);  %Function used to predict cp

%plotting the data
plot(temperature,cp,'linewidth',2,'color','b')  %original curve
hold on
plot(temperature1,cp_pred1,'linewidth',2,'color','r')  % predicted curve
hold on
plot(temperature2,cp_pred2,'linewidth',2,'color','r')
hold on
plot(temperature3,cp_pred3,'linewidth',2,'color','r')
hold on
plot(temperature4,cp_pred4,'linewidth',2,'color','r')
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ/K-mole)')
title('Cubic Fitting')
axis([0 5000 800 1500])
legend('Original data','Predicted data')

Result:-

The result of the above whole process predict that splitting data for cubic polynomial is the best method to get perfect fit and best fit.

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.