function [f,dF] = mpcCustomCostFcnWrapper(z,rseq,old_u,vseq,utarget,xQP,...
    I2Jm,SuJm,Su1,Sx,Hv,nu,ny,m,p,yoff,uoff,voff,uscale,yscale,vscale)
% Wrapper to custom cost function to optimize by nonlinear programming

% Author:  A. Bemporad
% Copyright 2017 The MathWorks, Inc.

degrees = numel(z);
% Reconstruct sequence of Y,U,DU
[Y,U,DU,slack,vseq] = mpcCustomFcnMap(z,xQP,old_u,vseq,uscale,yscale,vscale,uoff,yoff,voff,p,m,nu,ny,I2Jm,SuJm,Hv,Sx,Su1,degrees);
% Evaluate cost
[f,dfdy,dfdu,dfddu,dfdslack] = mpcCustomCostFcn(Y,rseq,U,utarget,DU,vseq(:,1:end-1),slack);
% Remap Jacobian in z-coordinates
if ~isempty(dfdy) && ~isempty(dfdu) && ~isempty(dfddu) && ~isempty(dfdslack)
    dF = mpcCustomGradMap(dfdy,dfdu,dfddu,dfdslack,uscale,yscale,SuJm,I2Jm,p,m,nu,degrees);
else
    dF = [];
end
