% % Mfile name
%   mtl_inp_sim_speshortestpath.m

% Language: Matlab 7.4.0 (R2007a)
% Revised: 
%   August 8, 2008

% Authors:
%   Nathan Collier, Luke Snyder, Autar Kaw
%   University of South Florida
%   kaw@eng.usf.edu
%   Website: http://numericalmethods.eng.usf.edu
       
% Purpose
%   To compare spline interpolation with polynomial interpolation in a real
%   world application.

% Keywords
%   Higher order Interpolation
%   Spline Interplolation

% Clearing all data, variable names, and files from any other source and
% clearing the command window after each sucessive run of the program.
clc
clf
clear all

% INPUTS: This is the only section in the program where the user can make
% changes to the program.
% The following is the data (x-y) coordinate data of the center of the six holes.

x=[2 4.5 5.25 7.81 9.2 10.6];

y=[7.2 7.1 6 5 3.5 5];

% *************************************************************************

disp(sprintf('\n\nAdditional Interpolation Topics: Comparing Polynomial and Spline'))
disp(sprintf('Interpolation'))
disp(sprintf('\nUniversity of South Florida'))
disp(sprintf('United States of America'))
disp(sprintf('kaw@eng.usf.edu'))
disp(sprintf('\nNOTE: This worksheet utilizes Matlab to compare the differences'))
disp(sprintf('between spline interpolation and polynomial interpolation in a real')) 
disp(sprintf('world application.'))

disp(sprintf('\n*****************************Introduction******************************'))

disp(sprintf('A rapid robot arm with a  laser is used to make seven holes on a '))
disp(sprintf('rectangular plate 12" X 8" at six points as shown.  The path of the'))
disp(sprintf('robot going from one indentation point to another needs to be smooth'))
disp(sprintf('so as to avoid sharp jerks in the arm that can otherwise create premature'))
disp(sprintf('wear on the arm.  One suggestion has been to fit a fifth order polynomial'))
disp(sprintf('throught the six points.  Another suggestion was to fit a quadratic spline'))
disp(sprintf('through the points.  Which suggestion is better so that the robot path is'))
disp(sprintf('shorter but also smooth?'))

disp(sprintf('\n******************************Simulation*********************************'))    

% SOLUTION:

%%%%%% Polynomial interpolation %%%%%
figure(1)
p=polyfit(x,y,numel(x)-1);
x1=2:0.01:10.6;
y1=polyval(p,x1);
plot(x1,y1,'b','LineWidth',3)

% Approximates length of curve
Ll=0;
for i=2:numel(x1)
    % Below is the formula for the length of a curve.
    Ll=Ll+((x1(i)-x1(i-1))^2+(y1(i)-y1(i-1))^2)^0.5;
end

% Displaying the length in the command window.
disp(sprintf('\nThe length of the curve using polynomial interpolation, Length = %g',Ll))

hold on
plot(x,y,'ro','MarkerSize',6,'MarkerFaceColor',[1,0,0])
title('\bfPath of robot arm using polynomial interpolation','FontSize',10)
xlabel('\bfx')
ylabel('\bff(x)')
text(4,4,['Length of curve = ',num2str(Ll)])
xlim([1 11])
ylim([0 10])

%%%%%% Spline interpolation %%%%%
figure(2)
xx=2:0.01:10.6;
yy=spline(x,y,xx);
plot(xx,yy,'g','LineWidth',3)

% Approximates length of curve
Ls=0;
for i=2:numel(xx)
    Ls=Ls+((xx(i)-xx(i-1))^2+(yy(i)-yy(i-1))^2)^0.5;
end

% Displaying the length in the command window.
disp(sprintf('\nThe length of the curve using spline interpolation, Length = %g',Ls))

hold on
plot(x,y,'ro','MarkerSize',6,'MarkerFaceColor',[1,0,0])
title('\bfPath of robot arm using cubic spline interpolation','FontSize',10)
xlabel('\bfx')
ylabel('\bff(x)')
text(4,4,['Length of curve = ',num2str(Ls)])
xlim([1 11])
ylim([0 10])
hold off

% Both imposed on each other
figure(3)
hold on
plot(x1,y1,'b','LineWidth',3)
plot(xx,yy,'g','LineWidth',3)
plot(x,y,'ro','MarkerSize',6,'MarkerFaceColor',[1,0,0])
str1 = 'Path of robot arm compared using polynomial interpolation and';
str2 = 'cubic spline interpolation';
title({str1,str2},'Fontweight','bold','FontSize',10)
xlabel('\bfx')
ylabel('\bff(x)')
xlim([1 11])
ylim([0 10])
hold off

disp(sprintf('\nClearly the use of the quadratic spline interpolation method is preferred'))
disp(sprintf('in this case as the curve is smoother and shorter in length.'))


