%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB.  Most of the questions relate to a mathematical 
% procedure.

%% TOPIC
% How do I conduct polynomial regression for given data.
%% SUMMARY
% Language : Matlab 2010a; 
% Authors : Autar Kaw and Sri Harsha Garapati; 
% Mfile available at
% http://numericalmethods.eng.usf.edu/blog/regression_sec002_blog.m
% Last Revised : January 23, 2012; 
% Abstract: This program shows you how to perform polynomial regression
% in MATLAB
clc
clear all
clf
%% INTRODUCTION

disp('ABSTRACT')
disp('   This program shows you how to perform polynomial regression')

disp(' ')
disp('AUTHOR')
disp('   Autar Kaw and Sri Harsha Garapati of http://autarkaw.wordpress.com')
disp(' ')
disp('MFILE SOURCE')
disp('   http://numericalmethods.eng.usf.edu/blog/regression_sec002_blog.m')
disp(' ')
disp('LAST REVISED')
disp('   January 23, 2012')
disp(' ')

%% INPUTS
  
% Inputs
% x cordinates of data
xd    =[325 265 185 105 25 -55 -70];
% y cordinates of date
yd    =[2.6 3.8 4.8 5.0 5.72 6.4 7.0];
% xvalue at which y is to be predicted using polynomial regression
xval  =[55 20 -10];
% Order of polynomial regression. Here 1 stands for linear regression
or    =1;                  
%% DISPLAYING INPUTS
disp('  ')
disp('INPUTS')
% Creating a matrix to print input data as a table
data=[xd;yd]';
disp('   X Data     Y Data')
% Printing the input data as a table
disp(data)
% Printing the input value of X at which Y is to be predicted using 
% polynomial regression
disp('  Predict y @ these x values:')
disp(xval)
% Printing the order of polyomial chosen for regressing the data
fprintf('Order of Polynomial chosen for regression is %g\n',or)

%% THE CODE

% fitting the data to order of polynomial regression
% ce returns the coefficents of the polynomial used for regression
ce    =polyfit(xd,yd,or);

% Predicting the value of y at xval using the coefficents from polynomial
% regression
ypre  =polyval(ce,xval);  

% setting up the domain to plot the polynomial regression 
xp    =[min(xd):0.1:max(xd)];
% evaluating over domain xp for plotting the regression function
yp    =polyval(ce,xp);

% defining the variable as symbolic
syms x
% creating the function using the coefficients of polynomial regression
eqn=ce(1)*x+ce(2);
% convert the exact model to a decimal
eqn1=vpa(eqn,6);  
% convert the symbolic expression to a string ( Useful for printing in the
% output section)
eqn1=char(eqn1);   
%% DISPLAYING OUTPUTS AND PLOTS
disp('  ')
disp('OUTPUTS')
fprintf(' Regression model:\n %s\n\n',eqn1)
disp('  The predicted y values are:')
disp(ypre)

disp('  See figure(1)')
% Telling Matlab to name the figure as Figure-1
figure(1)
% Matlab command so that it does hold on to all the plots
hold on
% plotting the data points
plot(xd,yd,'ro','MarkerSize',8)
% plotting the interpolation function
plot(xp,yp,'g-','LineWidth',1)
% plotting the predicted data point value
plot(xval,ypre,'bs','MarkerSize',7)
% Title of the Plot
title('Regression Model for Data Points')
% Legend to distunguish different plots
legend('Given Points','Regression Model','Predicted Point',0)
% Labeling the X-axis
xlabel('x')
% Labeling the Y-Axis
ylabel('y')
% Matlab command to exit the hold-on option
hold off



