function [Ze,Zs] = ZT(Tn, Zn, Ti)
% Octave/MATLAB function to compute Zero Coupon
% Bond spot discounts by interpolation.
% INPUTS:                              (Example)
%   Tn = maturities with known discounts   (1:5)
%   Zn = spot discount at each Tn   (cos(Tn/10))
%   Ti = evaluation points   ([0.5,1.5,3.2,4.7])
% OUTPUTS:
%   Ze =  piecewise exponential values at Ti
%   Zs =  spline interpolation values at Ti
% EXAMPLE:
%   Tn=1:5; Zn= cos(Tn/10); Ti=[0.5,1.5,3.2,4.7];
%   [Ze,Zs]=ZT(Tn,Zn,Ti)
%   plot(Tn,Zn,"k",Ti,Ze,"r",Ti,Zs,"b");
%
  [Tn,nsort]=sort(Tn); Zn=Zn(nsort); % 0<T1<T2<..
  Ze = zeros(size(Ti)); % initialize Ze
  for i=1:length(Ti)  % evaluation times
    T = Ti(i);
    t1=0; t2=Tn(1); z1=1; z2=Zn(1); % 0<T<T1
    for j=length(Tn):-1:2 % seek from high end
      if(T>Tn(j-1))
        t1=Tn(j-1); t2=Tn(j);
        z1=Zn(j-1); z2=Zn(j);
        break;
      end
    end
    alpha=(t1-T)/(t1-t2); % Compute Ze by exp()
    Ze(i)=z1^(1-alpha)*z2^alpha; % interpolation
  end              % Then, compute Zs by spline
  Zs=spline([0,Tn],[1,Zn],Ti); % interpolation
  return; 
end
