clc
clear all
clf
%% Cubic Spline interpolation- Chapter 11 Interpolation (pg 121)
clear all
disp('Example 2 - Chapter 11 Interpolation (pg 121)')
disp('Conduct cubic-spline interpolation given data pairs')
disp(' ')
disp('INPUTS')
% Inputs
xd  =[2 5.1 7.7 9.2 10.3];
yd  =[1.4 3.3 5.7 10.4 12.5];
xval=6.5;
% 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
fprintf('  Predict the value of y when x is %g\n\n',xval)
% code
xp  =[min(xd):0.1:max(xd)];
yp  =spline(xd,yd,xp);
yval=spline(xd,yd,xval);
disp('OUTPUTS')
disp('  See figure(2)')
fprintf('  Predicted value of y is %g\n',yval)
% plotting
figure(2)
hold on
plot(xd,yd,'r*','MarkerSize',10)
plot(xp,yp,'b-','LineWidth',2)
plot(xval,yval,'kd','MarkerSize',10)
title('Interpolation Model for Data Points')
legend('Given Points','Interpolation Model','Predicted Point',0)
xlabel('x')
ylabel('y')
hold off
