% Input:
% start: vector (start for the optimization algorithm)
% norm: 1/2/Inf (norm to use)
% x0 : function handles (support function)
% L : Matrix (containing row-wise direction vectors)
% algorithm: string (algorithm to use)
% TolFun: double
% Maxiter: integer

% Output:
% Rx0: vector

% Description:
% Computes Rx0 = max ||x|| for x in X0 (for algo1) using the specified
% norm. If the Inf norm is used the computation will be made without use of
% an optimization

function[Rx0] = calcR(start,norm,x0,L,algorithm,TolFun,MaxIter)

b = zeros( size(L,1),1);
for i = 1:1:size(L,1)
b(i,1) = x0(L(i,:)');

    % avoid []
    if( size(b(i,1),1) == 0 )
        b(i,1) = 0;
    end;
end;

if isinf(norm)
    b = abs(b);
    Rx0 = max(b);
    
else


%optimization options
options = optimset(@fmincon);
options.Display = 'on';
options.Algorithm = algorithm;

if( TolFun > 0 )
    options.TolFun = TolFun;
end;

if MaxIter >0
    options.MaxIter = MaxIter;
end;

[~,Rx0] = fmincon(@(x) mymax(x,norm),start,L,b,[],[],[],[],[], options);
if isnan(Rx0)
    fprintf('WARNING: calcR - fmincon returned NaN assuming 0');
    Rx0 = 0;
end;
Rx0 = -Rx0;

end;