function out=basisfunc(i,p,t,I)

% BASISFUNC evaluates the B-splines basis function of degree p at a
% given parameter value t. Only the functions that span the interval
% where t is located are evaluated.

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

if (size(I) < ((2 * p) + 2))
  error('Inconsistency between number of knots and basis degree');
end

% Allocate memory for the p + 1 basis functions
N = zeros(1,p+1);
left = zeros(1,p+2);
right = zeros(1,p+2);

% Evaluate the basis functions at t
N(1) = 1.0;
for j = 2 : (p+1)
  left(j)  = t - I(i + 2 - j);
  right(j) = I(i + j - 1) - t;
  temp1 = 0;
  for r = 1 : (j - 1)
      temp2 = N(r) / (right(r + 1) + left(j - r + 1));
      N(r)  = temp1 + (right(r + 1) * temp2);
      temp1 = left(j - r + 1) * temp2;
  end
  N(j) = temp1;
end

out = N;

return