function f = cost_c(t,varargin)

parameters_c
switch nargin
    case 2 % Optimization
        X  = t;
        U  = X((NC*NT)+NT+1:end);
        Dd = varargin{1};
        % Splitting the states
        x  = X(1:(NC-1)*NT);
        M  = X((NC-1)*NT+1:NC*NT);
        T  = X((NC*NT)+1:(NC*NT)+NT);
        xB = x(NT+1);
        xx = reshape(x,NT,NC-1);
        xx = [xx,1-sum(xx,2)];
        
        % inputs and disturbances:
        D  = Ds + (M(NT) - MDs)*KcD;
        B  = Bs + (M(1) - MBs)*KcB;
        F  = Dd(1);
        qF = Dd(2);
        zF = Dd(3:end);
        LT = U(1);
        VB = U(2);
        V1 = LT + D;
        % utilities:
        CW = ((1714.3*V1) + 6476.8)/(60*1000);  % cooling water [m^3/min]
        LPS = (2316.9*exp(0.0037*VB))/60;       % low pressure steam [kg/min]
        % post-processing:
        post_processing
        % Cost function
        %epsilon = GL_tot - 0.0025;
%         epsilon = 0 - LT;
        cost_feed = (F*zF(1,1)*Mw(1,1)*pMetOH) + (F*zF(1,2)*Mw(1,2)*pTG);
        val_prod  = (pBD*EP_tot) + (pGL*CGL_tot) + (D*Mw(1,1)*pMetOH);
        cost_util = (pLPS*LPS) + (pCW*CW);
        f = cost_feed + cost_util - val_prod;
    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

