Project

Curve Fitting using MATLAB

AIM:

To write a code to perform curve fitting in MATLAB.

OBJECTIVES:

  1. Write code to fit a linear and cubic polynomial for the Cp data provided.
  2. Plot the linear and cubic fit curves along with the raw data points.
  3. Explain the parameters used to measure the fitness characteristics for both the curves.

THEORY:

Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values, etc. It is highly effective in the mathematical modelling of some natural processes.

Curve fit are important to know the trend followed by data and also allow additional mathematical operation which might not be possible in the initial data set. For example, the population data in country. There is no formula to find what the population is going to be in next year. However, if we fit a curve, we had a function which say how population is going to change with respect to time. The accuracy of results depends on the quality of curve fit but at least we can have a rough trend.

Goodness of a fit:

Upon finding a curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equation is a representing the datapoints. They are,

  1. The sum of squares due to error (SSE)
  2. R-square
  3. Adjusted R-square
  4. Root mean squared error (RMSE)

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

The Error Sum of Squares(SSE) is the sum of squares of the deviations of the data values from the predicted values (typically predicted from a Least Squares) I.e. it's a measure of how well the independent variable fit the data.

`Err(i)=Y_i-f(x(i))`

SSE(sum of squared errrors) = `∑_(i=1)^nErr(i)^2`

When we differentiate the SSE with each and every coefficient of the proposed equation f(x) we and equate it to zero, we can find the minima and the value of that coefficient.

Upon solving the multiple system of equations we can find the values of all the coefficients.

2 . R-square

This quantity that 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.

That is the higher the value of R-square the better the fit, the lower it is the lousier the fit, R-square ranges from a value of 0 to 1

  • 0 for the worst possible fit for the data set
  • 1 for the best possible fit for the data set

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

Where,

SSR = Sum of squares of the regression

SSR = `∑_(i=1)^n(f(x(i))-Mean)^2`

SSE = `∑_(i=1)^n(Y_i-f(x(i)))^2`

SST = SSR + SSE

3 . Adjusted R-square

Adjusted R-square is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variables.

  • If value of contributes to the output, then is a significant dependent variable
  • If value of does not contribute to the output, then is an insignificant dependent variable

R-square is formulated as shown below,

`R_(adj)^2=1-((1-R^2)(n-1))/(n-k-1)`

where,

n = the number of datapoints

k = the number of independent variables

4 . Root mean squared error (RMSE)

This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data, and is defined as,

`RMSE =√((SSE)/n)`

where,

n is the total number of datapoints available.

PROGRAM:

Programming language used it MATLAB.

clear all
close all 
clc 
 
% Loading the data into a variable called cp_data from he file called
% 'data' which stores data for Cp at various temperatures
cp_data=load('data'); 
temperature=cp_data(:,1);       % Storing the temparatures
cp=cp_data(:,2);                % Storing the value of cp at each temparature 
 
% Performing curve fitting
% First we will do it for linear function (that is polynomial with degree=1)
% Cp=a*t+b 
 
co_effs1 = polyfit(temperature,cp,1);               % Using polyfit command to calculate values of the coefficients a ,b
predicted_cp1 = polyval(co_effs1,temperature);      % Using polyval command to calculate the predicted value od Cp using linear equation 

% Then we will do it for cubic function (that is polynomial with degree=3) 
% Cp=a*t^3+b*t^2+ct+d

co_effs2 = polyfit(temperature,cp,3);               % Using polyfit command to calculate values of the coefficients a, b, c, d 
predicted_cp2 = polyval(co_effs2,temperature);      % Using polyval command to calculate the predicted value od Cp using cubic equation 

% Now we will compare the original plot with our linear curve fit
figure(1)
subplot(2,1,1)
% Plotting original plot
plot(temperature,cp,'linewidth',2); 
hold on 

% Plotting linear curve fit
plot(temperature,predicted_cp1,'linewidth',2,'color','k'); 
xlabel('Temperature [K]');
ylabel('Specific Heat (Cp) [KJ/Kmol-k]');
legend('Original Data Set','Linear Curve Fit');
title('Linear Curve Fit'); 
hold off 
 
% Now we will compare the original plot with our cubic curve fit

subplot(2,1,2)
% Plotting original plot
plot(temperature,cp,'linewidth',2); 
hold on 

% Plotting cubic curve fit
plot(temperature,predicted_cp2,'linewidth',2,'color','r');
xlabel('Temperature [K]');
ylabel('Specific Heat (Cp) [KJ/Kmol-k]');
legend('Original Data Set','Cubic Curve Fit');
title('Cubic Curve Fit'); 
hold off

% Now we will compare all the plots (original plot ,linear fit, cubic fit)
figure(2)
plot(temperature,cp,'linewidth',2); 
hold on 
plot(temperature,predicted_cp1,'linewidth',2,'color','k'); 
plot(temperature,predicted_cp2,'linewidth',2,'color','r');
xlabel('Temperature [K]');
ylabel('Specific Heat (Cp) [KJ/Kmol-k]');
legend('Original Data Set','Linear Curve Fit','Cubic Curve Fit');
title('Linear vs Cubic Fit');
hold off


% Calculating goodness parameters
%   1. Sum of squares due to error (SSE)
%   2. R-square
%   3. Adjusted R-square
%   4. Root mean squared error (RMSE)

% Calculating, Sum of squares due to error (SSE)
SSE_L = sum((cp-predicted_cp1).^2);     % For linear curve fit
SSE_C = sum((cp-predicted_cp2).^2);     % For cubic curve fit 

% Calculating , R-square
M=mean(cp);         % Mean for the R-square calculation                            
% For linear fit  
SSE_linear=sum((cp - predicted_cp1).^2);      % sum of squared errors                
SSR_linear=sum((predicted_cp1 - M).^2);       % sum of squares of regression       

SST_linear = SSE_linear+SSR_linear;             % Total of sum of squares 
 
% Calculating R-square for linear fit
Rsquare_L = SSR_linear/SST_linear;

% For cubic fit
SSE_cubic=sum((cp - predicted_cp2).^2);      % sum of squared errors           
SSR_cubic=sum((predicted_cp2 - M).^2);       % sum of squares of regression        

SST_cubic = SSE_cubic+SSR_cubic;            % Total of sum of squares 
 
% Calculating R-square for cubic fit
Rsquare_C = SSR_cubic/SST_cubic;


% Calculating Adjusted R-square 
k=1;        % No. of independent variables
AdRsquare_L = 1- (((1-Rsquare_L)*(length(cp)-1))/(length(cp)-k-1));     % For linear fit
AdRsquare_C = 1- (((1-Rsquare_C)*(length(cp)-1))/(length(cp)-k-1));     % For cubic fit

% Calculating Root mean squared error (RMSE)
RMSE_L = sqrt(SSE_L/length(cp));        % For linear fit
RMSE_C = sqrt(SSE_C/length(cp));        % For cubic fit


% Putting Errors in form of an Table
Name_of_errors = {'SSE'; 'R_Square';'AdjustedR_Sqaure'; 'RMSE'} ;
 
Linear_fit_error = [SSE_L; Rsquare_L; AdRsquare_L; RMSE_L];
Cubic_fit_error = [SSE_C; Rsquare_C; AdRsquare_C; RMSE_C];

disp('Goodness Parameter for Linear and Cubic Curve fit')
Goodness_Parameters = table(Linear_fit_error,Cubic_fit_error,'RowNames',Name_of_errors)

EXPLANATION OF THE PROGRAM:

  1. Program starts with loading all the data into a variable called cp_data from he file called 'data' which stores data for Cp at various temperatures.
  2. For the purpose of the curve fitting the two commands are used, the ployfit and polyval. The command polyfit returns the coefficients in the equation like considering the equation y= Ax^2+Bx+C, here the coefficients A, B and C are calculated.
  3. The degree of the polynomial is choosen based on the goodness of the fit. Based on the degree of the polynomial the number of the coefficients also changes and thier values also changes.
  4. The command polyval returns the predicted value of Cp based on the coefficients calculated.
  5. We calculate the predicted Cp for both linear and cubic curves.
  6. Then graph is ploted for the original and predicted values using linear and cubic curves.
  7. Then we compare all the plots (original plot, linear fit, cubic fit).
  8. After that we calculate the goodness parameters for both the fits and display the parameters in form of a table. The table contains SSE, R-squre, Adjusted R_squre and RMSE data for both the curves.
  9. Also to check the goodness of fit the curve fitting app is used from the apps section, The required data is provided like name, X data, Y data, ploynomial, the degree of the polynomial is filled and output containing the graph, coefficients and goodness of fit data is obtained.

RESULT:

1. This plot shows the comparison of the linear fit with original plot and also shows comparison of cubic fit with original plot .

2. This plot shows direct comparison between linear fit , cubic fit and original plot.

3. This is the output in the command window , which shows the goodness factors for each curve fit in form of a table.

4. These are variables stored in the workspace after running the program.

5. Using the Curve fitting toolbox in MATLAB to validate our fitness characteristics.

5.1. For linear fit

5.2. For cubic fit

QUESTIONS:

1. How to make a curve fit perfectly?

Perfect fit means, the curve should fit the original curve without showing any errors in that particular degree of polynomial. This means that every data point in the original plot should be on the curve. Perfect fit can always be a best fit but best fit cannot be a perfect fit.

We can fit a curve perfectly only by using interpolation. It is not necessary that we get a single function representing complete trend.

2. How to get the best fit?

Best fit” simply means that the differences between the actual measured Y values and the Y values predicted by the model equation are minimized. It does not mean a "perfect" fit; in most cases, a least-squares best fit does not go through all the points in the data set. Above all, a least-squares fit must conform to the selected model.

We can get a best fit by:

  1. Using higher degree polynomial.
  2. Using split wise curve fitting.

To compare the accuracy of curve fit, we can evaluate the goodness parameter. Once the improvement in the goodness parameter become stagnant, best fit curve is obtained.

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

To improve the cubic fit we can do split wise fitting for the curve. That is splitting the curve into multiple parts and using a different cubic polynomial for different sets of temperature and specific heat values. This reduces the errors and best fit is obtained.

Here i have splitted the data into 5 regions and performed a split fitting for the cubic funtion .

MATLAB program for split wise fitting of cubic function:

clear all
close all
clc 
 
% loading data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2); 
 
% Spliting data into 5 parts
temperature1=cp_data(1:640,1);
cp1 = cp_data(1:640,2);

temperature2=cp_data(641:1280,1); 
cp2 = cp_data(641:1280,2);

temperature3=cp_data(1281:1920,1); 
cp3 = cp_data(1281:1920,2);
 
temperature4=cp_data(1921:2560,1); 
cp4 = cp_data(1921:2560,2); 

temperature5=cp_data(2561:3200,1);
cp5 = cp_data(2561:3200,2);

% Considering cubic polynomial and calculating predicted value of Cp for 5 parts
% Cp=a*t^3+b*t^2+ct+d 
co_eff1 = polyfit(temperature1,cp1,3); 
pred_cp1 = polyval(co_eff1,temperature1); 
 
co_eff2 = polyfit(temperature2,cp2,3); 
pred_cp2 = polyval(co_eff2,temperature2); 
 
co_eff3 = polyfit(temperature3,cp3,3); 
pred_cp3 = polyval(co_eff3,temperature3); 
 
co_eff4 = polyfit(temperature4,cp4,3); 
pred_cp4 = polyval(co_eff4,temperature4);  

co_eff5 = polyfit(temperature5,cp5,3); 
pred_cp5 = polyval(co_eff5,temperature5);

% Plotting
% Plotting original plot
plot(temperature,cp,'linewidth',3,'color','g') 
hold on 
% Plotting the cubic curve for the 5 regions
plot(temperature1,pred_cp1,'linewidth',1,'color','k')
plot(temperature2,pred_cp2,'linewidth',1,'color','k') 
plot(temperature3,pred_cp3,'linewidth',1,'color','k')
plot(temperature4,pred_cp4,'linewidth',1,'color','k'); 
plot(temperature5,pred_cp5,'linewidth',1,'color','k'); 
xlabel('Temparature [K]');  
ylabel('Cp [KJ/K-mol K]'); 
axis([0 4000 900 1400]);    
legend('Original data','Cubic Curve Fit') ;

RESULT OF THE SPLIT WISE FITTING PROGRAM:

The green line (thicker line) represents the original plot and the black line (thin line) represents the cubic fit.

As we can see by using splitwise fitting we have improved the cubic fit substantially. We can check by comparing it with the earlier cubic plot which we got without using splitwise fitting.

CONCLUSION:

  1. The linear and polynomial curves are fitted to specific heat data provided using MATLAB.
  2. Goodness parameters for each curve is evaluated and are compared with the results using MATLAB tool box.
  3. The Cubic curve fit is good fit than the linear curve fit, and this is verified by the goodness parameters calculated as we can see the R-square value for the Linear fit is 0.92488 and for Cubic fit is 0.99673 which is more close to 1.

REFERENCE:

https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860

https://in.mathworks.com/matlabcentral/answers/349776-how-to-find-the-best-curve-fit-for-a-set-of-data

https://in.mathworks.com/help/curvefit/interactive-curve-and-surface-fitting.html

https://en.wikipedia.org/wiki/Curve_fitting

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.