function [v, dvdp]=pieceWiseLinearNonParam(t, tData, pData)
% function [v, dvdp]=pieceWiseLinearNonParam(t, tData, pData)

% This work is licensed under the Creative Commons Attribution-Noncommercial-No Derivative 
% Works 3.0 United States License. To view a copy of this license, visit 
% http://creativecommons.org/licenses/by-nc-nd/3.0/us/ or send a letter to Creative Commons,
% 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

len = length(pData);

if(nargout>1)
    dvdp = zeros(length(t),0);
end


pData = [pData; 0];
tData = [tData 2*tData(end)-(tData(end-1))];

for ind = 1:length(t)
    k  = find( t(ind) >= tData , 1, 'last');
    if(isempty(k))
        error('t outside range');
    end

    dt = t(ind) - tData(k);
    T  = tData(k+1) - tData(k);

    v(ind) = pData(k)*(1-dt/T) + pData(k+1)*(dt/T);

%     if(nargout>1)
%         
%         dvdp(ind, k) = (1-dt/T);
%         if(k < len)
%             dvdp(ind, k+1) = (dt/T);
%         end
%     end
end

end