Data: Curve Fit

General data fitting is typically done using polynomials (also in Polynomials).  The problem can be specified as

Given a set of  x - y data find the best polynomial of given order that will best match the data
For a polynomial of order n, this is an n+1 variable problem.  The problem is an example of unconstrained optimization. In numerical analysis course this can be shown to be a linear matrix solution involving the Vandermonde matrix. In addition MATLAB provides a statistical evaluation that provides information on the quality of the fit

In this example


MATLAB Code

% Data Analysis - Polynomial Curve fit
% Dr. P.venkataraman
% ---------------------create sample data
x = linspace(0,3,31);
y = 3*exp(-0.5*x).*sin(2*x);
n = 4;    % order of data
%-------------------------------------
format compact
% formatting figure-------------------
set(gcf,'Name','Polynomial Fit', ...
    'NumberTitle','off','Position',[10,350,400,350], ...
    'Color',[0.5 0.8 0.5]);
%-----------------------------------
plot(x,y,'ro','MarkerFaceColor','y', ...
    'MarkerSize',4,'MarkerEdgeColor','k')

% formatting figure-----------
title('\bf\itPolynomial fit','Color','k', ...
    'VerticalAlignment','middle')
ylabel('y', ...
    'FontWeight','b','Color','k', ...
    'VerticalAlignment','bottom');

xlabel('x', ...
    'FontWeight','b','Color','k', ...
    'VerticalAlignment','top');
grid
%---------------------------------