clc
clear all
clf
%% Example 1 - Chapter 11 Polynomial Interpolation (pg 117)
disp('Example 1 - Chapter 11 Interpolation (pg 117)')
disp('Conduct polynomial interpolation given three data pairs')
disp(' ')
% INPUTS
disp('INPUTS')
xd  = [1 4 8];   % x data points
yd  = [2 5 7];   % y data points
xval= 4.5;       % predict y at this x value
data=[xd;yd]';
disp('     X     Y ')
% Printing the input data as a table
disp(data)
fprintf('  Predict the value of y when x is %g\n\n',xval)
% CODE
n   =length(xd);  
or  =n-1;                % order of interpolation model
coef=polyfit(xd,yd,or);  % conducting polynomial interpolation 
ypre=polyval(coef,xval); % predicting y
xp  =[min(xd):0.1:max(xd)];% setting up domain
yp  =polyval(coef,xp);   % evaluating over domain xp
disp('OUTPUTS')
syms x
fun =char(vpa(coef(1)*(x)+coef(2),5));
disp('  See figure(1)')
fprintf('  Predicted value of y is %g\n',ypre)
fprintf('  The interpolation model is:\n  %s\n\n',fun)
% Example 1 - PLOTTING
figure(1)
hold on
plot(xd,yd,'rs','MarkerSize',10)
plot(xp,yp,'k-.','LineWidth',2)
plot(xval,ypre,'bo','MarkerSize',10)
title('Interpolation Model for Data Points')
legend('Given Points','Interpolation Model','Predicted Point',0)
xlabel('x')
ylabel('y')
hold off
