% Input:
% L: Matrix (Direction vectors - rowwise)
% A: Matrix A (of the continuous dynamics)
% B: Matrix B (of the continuous dynamics)
% delta: double (stepsize)
% U: function handle (Support function of free input)
% X0: function handle (Support function of initial set)
% N: Integer (number of sets to generate)
% Scenario: String (values: Scale, NoScale)
% options: cell array (parameters)

% Output:
% results: Matrix (results of the evaluation of the support functions
% generated in directions L)

% Beschreibung:
% Computes the support functions representing the reachable sets using algo1 from "Reachability Analysis of Linear
% Systems using Support Functions". The Scenario determines the overapproximation to use for the first set.

function [results] = algoOld(L,A,B,delta,U,X0,N,Scenario,options)

starttime = tic;

% initial values
normv = Inf;
pB = @(l) norm(l,2);
startv = zeros(size(L,2),1);
   
    if strcmp(Scenario,'Scale')
        
        % scale alpha
        Alphascale = options{13};
        Betascale = 1;
        
        fprintf('alpha scaled with %f \n beta scaled with %f\n', Alphascale, Betascale);
    else
        % dont scale alpha
        Alphascale = 1;
        Betascale = 1;
        
        fprintf('without alpha scale\n', Alphascale, Betascale);
    end;
         
    pV = calcpV(B,U);
    PhiTau = expm(delta*A);
    Rv = calcR(startv,normv,pV,L,options{26},-1,-1);
    Beta = calcBetaTau(delta,A,Rv,normv)*Betascale;
    pW = calcPw(delta,pV,Beta,pB);
        
    Rx0 = calcR(startv,normv,X0,L,options{26},-1,-1);
        
    Alpha = calcAlphaTau(delta,A,Rx0,Rv,normv)*Alphascale;
    pOmega0 = calcPinit(X0,PhiTau,pV,Alpha,pB,delta);    
 
fprintf('initial values calculated after: %f \n\n',toc(starttime));

starttime = tic;

% reachability analysis
results = zeros(size(L,1),N);
    parfor i = 1:size(L,1)
        fprintf('evaluate support function for l%d ...',i);
        l = L(i,:)';
        pv = algo1(PhiTau,pOmega0,pW,l,N);
        results (i,:) = pv;
        fprintf('\t done.\n');
    end;
fprintf('reachable sets evaluated after: %f \n\n',toc(starttime));