% -----------------------------------------------------
% interpolation1.m
% -----------------------------------------------------
% Write a program that uses given data to predict
% the interpolated values using: linear, cubic and spline
% interpolation. 
% -----------------------------------------------------
% Notes: None. 
% -----------------------------------------------------
% Copyright and license details available at matlabmarina.com
%------------------------------------------------------

clear
clc

% Given set of data points
x = [0,1,2,3,4,5,6,7,8,9,10];
y = [0, 0.8415, 0.9093, 0.1411, -0.7568, -0.9589, ...
        -0.2794, 0.6570, 0.9894, 0.4121, -0.5440];
xi_given = 5.6;

% Linear Interpolation

% Given xi=5.6, what is the value of y at xi?
yi_given_linear = interp1(x,y,xi_given,'linear');

% Given a set of values for xi, what are the values of y at these xi?
xp_linear = [0:0.25:10];
yp_linear = interp1(x,y,xp_linear,'linear');

% Cubic Interpolation
% Given xi=5.6, what is the value of y at xi?

yi_given_pchip = interp1(x,y,xi_given,'pchip');

% Given a set of values for xi, what are the values of y at these xi?
xp_pchip = [0:0.25:10];
yp_pchip = interp1(x,y,xp_pchip,'pchip');

% Spline Interpolation

% Given xi=5.6, what is the value of y at xi?
% using spline interpolation
yi_given_spline = interp1(x,y,xi_given,'spline');

% Given a set of values for xi, what are the values of y at these xi?
xp_spline = [0:0.25:10];
yp_spline = interp1(x,y,xp_spline,'spline');

% displaying outputs
yi_given_all=[yi_given_linear, yi_given_pchip, yi_given_spline];
interptype={'Linear','Cubic','Spline'};

fprintf('The value of y at x = %.1f is\n\n',xi_given) 
for k=1:1:length(yi_given_all)
    fprintf('%f using %s interpolation \n\n',yi_given_all(k),interptype{k})
end

