%% philint.m is my version of interp1.m.
%% It only does linear interpolation. It
%% expects row vector inputs (bar y0 which
%% is n by length(x0) matrix), and outputs
%% a n by length(xv) matrix.
%%

function yc=philint(x0,y0,xv)

% x0 - x values of data
% y0 - y values of data (size is n by lenght(x0))
% xv - x values for which y0 needs to be calculated
% yc - interpolated y values (size is n by lenght(xv))

%check to make sure that xv values are not outside
%range given by x0
if min(xv)<min(x0) | max(xv)>max(x0)                       
 disp('Error with philint, xv values outside x0 range')
 keyboard
end

x1=sum(x0'*ones(1,length(xv))<=ones(length(x0),1)*xv);      %find index of largest x0 values below or equal to xv values
x2=x1+1;                                                    %find index of smallest x0 values above xv values
x2=x2-2*(x1==length(x0));                                   %catch to fix problem of xv=max(x0)

yc=y0(:,x1)+(ones(size(y0,1),1)*(xv-x0(x1))).*(y0(:,x2)-y0(:,x1))./(ones(size(y0,1),1)*(x0(x2)-x0(x1)));
