function f = cost(t,varargin)

parameters
switch nargin
    case 2 % Optimization
        X  = t;
        U  = X(NC*NT+1:end);
        Dd = varargin{1};
        % Splitting the states
        x  = X(1:(NC-1)*NT);
        M  = X((NC-1)*NT+1:NC*NT);
        xB = x(NT+1);
        D  = Ds + (M(NT) - MDs)*KcD;
        B  = Bs + (M(1) - MBs)*KcB;
        F  = Dd(1);
        VB = U(2);
        f  = pV*VB + pF*F - pD*D - (pB1-pB2*xB)*B;
%         f  = VB; % Minimization of energy use only
    case 3 % Steady state
        X  = t;
        U  = varargin{1};
        Dd = varargin{2};
        f  = 1;
    case 4 % Dynamic
        X  = varargin{1};
        U  = varargin{2};
        Dd = varargin{3};
end

