function [T] = Tcell(t,params,xu,xl)
load('trialdata.mat','w','time')
load('datafits.mat','fits')
time(1) = 0;
T = zeros(numel(t),1);

%Define function handles for the approximations
fun1 = @(X,x) X(1)*x + X(2);
fun2 = @(X,x) X(1) + X(2)*atan(X(3)*x - X(4));
fun3 = @(X,x) heaviside(X(5) - x)*fun2(X,x);

for ii = 1:numel(t)
    
    %Approximate the weight vector at the current time
    what = interp1(time,w',t(ii));
    what = what/norm(what);
    
    %Calculate the active subspace direction
    xnorm = diag(xu - xl)\(2*params - (xu + xl));
    x = dot(what,xnorm);
    
    %Find the index of the element
    n = find(histc(t(ii),time),1);
    if n == numel(time)
        n = n - 1;
    end
    
    %Define the T-cell approx at the left end of the element
    if fits(n,3:5) == zeros(1,3)
        T1 = @(x) fun1(fits(n,:),x);
    elseif fits(n,5) == 0
        T1 = @(x) fun2(fits(n,:),x);
    else
        T1 = @(x) fun3(fits(n,:),x);
    end
    
    %Define the T-cell approx at the right end of the element
    if fits(n,3:5) == zeros(1,3)
        T2 = @(x) fun1(fits(n,:),x);
    elseif fits(n,5) == 0
        T2 = @(x) fun2(fits(n,:),x);
    else
        T2 = @(x) fun3(fits(n,:),x);
    end
    
    %Define the T-cell approximation
    T(ii) = T1(x)*phi([time(n),time(n),time(n+1)],t(ii)) + T2(x)*phi([time(n),time(n+1),time(n+1)],t(ii));

end
