function out=findinterval(n,p,t,I)

% FINDINTERVAL computes the knot interval of an affine frame I = [r,s]
% of A that contains a given parameter value t.

if (nargin ~= 4)
  error('Incorrect number of arguments');
end

if (size(I) ~= (n + p + 1))
  error('Incorrect number of knots');
end

if (t >= I(n + p - 1))
  out = n + p - 2;
elseif (t <= I(p+1))
  out = p+1;
else
  i1 = p + 1;
  i2 = n + p - 1;
  
  mp = fix((i1 + i2) / 2.);
  while ((t < I(mp)) || (t >= I(mp + 1)))
    if (t < I(mp))
      i2 = mp;
    else
      i1 = mp;
    end
    mp = fix((i1 + i2) / 2.);
  end
  out = mp;  
end

return