Project

Curve Fitting using MATLAB

AIM: To write a code to perform curve fitting uning MATLAB.

OBJECTIVES:

  1. To write code to fit a linear and cubic polynomial for the given Cp data.

  2. To Plot the linear and cubic fit curves along with the raw data points.

  3. To validate fitness characteristics by using Curve fitting toolbox in MATLAB

INTRODUCTION:

What is Curve Fitting?

Curve fitting is one of the most powerful and most widely used analysis tools in Origin. Curve fitting examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a "best fit" model of the relationship. [1]

Curve fits are very important and useful because it helps to describe how data changes in mathematically. In curve fit we can do additional mathematical operations which might not be possible in the initial data set. For example let us take population data in a particular country. There is no know formula that can tell you what’s the population of the particular country going to be in the next 10 years. However, if do a curve fit you will basically get a mathematical function which says how the population is going to change as a function of time in other words you can put in a future time and predict population. Now based upon the accuracy of curve fit final result is going to change but even than in the curve fit one can actually get some rough trends as a curve fit is a mathematical function you can do additional operations such as take derivatives and so on.

Programme to fit linear & Cubic polynomial for Cp data:

% Programme to find Curve fitting

clear all
close all
clc

% Preparing the data

cp_data = load('data');         % loading the data file from computer to program
temperature = cp_data(:,1);     % first column and all rows of cp_data
cp = cp_data(:,2);              % second column and all rowws of cp_data


% CURVE FIT FOR QUADRATIC POLYNOMIAL

% After plotting the curve we need to fit the curve. we have the data set but we dont konw mathematical nature of this dataso this is
% where we are going to use curve fit the logic behind curve fit is quit simple We can assume that the function is either qudratic 
% function or a cubic function or a nth order polynomial doesnt matter so i can just say that my arbitary function is 
% cp = a*T^2+b*T+c
% now the task is figure out what is the best vaule for a & b so that ican use this particular formula to closely represent my data.
% so determine a & b we need two equations, what we can do is in temperature curve we can take two points for example when temperature
% is 300 you know the value for cp and when the temperature 1000 you know the value of cp you can use these two  data sets and figure 
% out the value for a & b. that is going to be a very simple curve fit but a more robust curve fit is to pick a & b value in such a way 
% that the error that you get between your orignal value in data set and your fitted data set is minimum and that is the power of curve 
% fit and in MATLAB you can do it using something called as poly fit command

co_effs = polyfit(temperature,cp,2);            % This is going to give a set of coefficient a & b
predicted_cp_quad = polyval(co_effs,temperature);    % Above coefficients can be used to predict the value for cp


% COMPARE THE CURVE FIT WITH ORIGIONAL DATA

figure(1)
plot(temperature, cp, 'linewidth',3)
hold on 
plot(temperature,predicted_cp_quad,'color','r','linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat in [KJ/Kmol-k]')
legend('Original Data Set', 'Curve fit')
title('QUADRATIC FIT')
grid on

% CURVE FIT FOR CUBIC POLYNOMIAL
co_effs = polyfit(temperature,cp,3);            % This is going to give a set of coefficient a & b
predicted_cp_cubic = polyval(co_effs,temperature);    % Above coefficients can be used to predict the value for cp


% COMPARE THE CURVE FIT WITH ORIGIONAL DATA

figure(2)
plot(temperature, cp, 'linewidth',3)
hold on 
plot(temperature,predicted_cp_cubic,'color','g','linewidth',3)
xlabel('Temperature [K]')
ylabel('Specific Heat in [KJ/Kmol-k]')
legend('Original Data Set', 'Curve fit')
title('CUBIC FIT')
grid on 

Output for linear fit:

Output for cubic fit

Calculation of R square

% CALCULATION OF R^2 

% Calculating mean value of cp from data
M = mean(cp);

% FOR LINEAR CURVE

for i = 1:length(cp)
    
    % Sum of Squared Errors (SSE)
    SSE_L = sum((cp(i)-predicted_cp_quad(i)).^2);
    
    % Sum of Squares of Regression (SSR)
    SSR_L = sum((predicted_cp_quad(i)-M).^2);
    
end

% Total Sum of Squares (SST)
SST_L = SSE_L + SSR_L;

% Calculating R^2
R_Q = SSR_L/SST_L %for Linear Fit


% FOR CUBIC CURVE

for j = 1:length(cp)
    
    % Sum of Squared Errors (SSE)
    SSE_C = sum((cp(i)-predicted_cp_cubic(i)).^2);
    
    % Sum of Squares of Regression (SSR)
    SSR_C = sum((predicted_cp_cubic(i)-M).^2);
    
end

% Total Sum of Squares (SST)
SST_C = SSE_C + SSR_C;

% Calculating R^2
R_C = SSR_C/SST_C %for Cubic Fit

OUTPUT: R square values are above 0.95

To validate fitness characteristics by using Curve fitting toolbox in MATLAB

Validation of fitness characteristics of linear by using Curve fitting toolbox in MATLAB

Validation of fitness characteristics of Cubic by using Curve fitting toolbox in MATLAB

How to make a curve fit perfectly?

In order to make perfect fit, we must consider error estimates as well. Perfect fit means, the curve should fit the original curve without showing any errors (such as centering and scaling erros) in that perticular degree of polynomial. Perfect fit can always be a best fit but best fit can not be a perfect fit.

How to get the best fit?

To determine the best fit, you should examine both the graphical and numerical fit results. Determine the best fit by examining the graphs of the fits and residuals.

The graphical fit results indicate that (1) The fits and residuals for the polynomial equations are all similar, making it difficult to choose the best one. (2) The fit and residuals for the single-term exponential equation indicate it is a poor fit overall. Therefore, it is a poor choice and you can remove the exponential fit from the candidates for best fit.

When you can no longer eliminate fits by examining them graphically, you should examine the numerical fit results. The Curve Fitting app displays two types of numerical fit results:
• Goodness-of-fit statistics
• Confidence bounds on the fitted coefficients
The goodness-of-fit statistics help you determine how well the curve fits the data. The confidence bounds on the coefficients determine their accuracy.
Examine the sum of squares due to error (SSE) and the adjusted R-square statistics to help determine the best fit. The SSE statistic is the least-squares error of the fit, with a value closer to zero indicating a better fit. The adjusted R-square statistic is generally the best indicator of the fit quality when you add additional coefficients to your model. The largest SSE for exp1 indicates it is a poor fit, which you already determined by examining the fit and residuals. The lowest SSE value is associated with poly6. However, the behavior of this fit beyond the data range makes it a poor choice for extrapolation, so you already rejected this fit by examining the plots with new axis limits. The next best SSE value is associated with the fifth-degree polynomial fit, poly5, suggesting it might be the best fit. However, the SSE and adjusted R-square values for the remaining polynomial fits are all very close to each other. Which one should you choose?
Resolve the best fit issue by examining the confidence bounds for the remaining fits in the Results pane. Double-click a fit in the Table of Fits to open (or focus if already open) the fit figure and view the Results pane. A fit figure displays the fit settings, results pane and plots for a single fit. Display the fifth-degree polynomial and the poly2 fit figures side by side. Examining results side by side can help you assess fits.

What could be done to improve the cubic fit?

To improve Cubic or higher polynomial fit we can use splitwise curve fit In this method we split the data in various data folds.

COCLUSIONs:

  1. After plotting the curve we need to fit the curve. we have the data set but we don’t know mathematical nature of this data so this is where we are going to use curve fit the logic behind curve fit is quite simple We can assume that the function is either quadratic function or a cubic function or a nth order polynomial.

  2. Result from coading can be validated validate by using Curve fitting toolbox in MATLAB.

REFRENCES

  1. https://statisticsbyjim.com/regression/curve-fitting-linear-nonlinear-regression/
  2. https://skill-lync.com/projects/Week-4-Curve-fitting-31550
  3. https://in.mathworks.com/matlabcentral/answers/349776-how-to-find-the-best-curve-fit-for-a-set-of-data

Re

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.