The second method, based on deﬁning a set of callback functions, oﬀers several distinct advantages, especially for more complex scenarios or for adding a feature for others to use, such as the zonal reserve requirement or the interface ﬂow limits mentioned previously. This approach makes it possible to:

- deﬁne and access variable/constraint/cost sets as individual named blocks
- deﬁne constraints, costs only in terms of variables directly involved
- pre-process input data and/or post-process result data
- print and save new result data
- simultaneously use multiple, independently developed extensions (e.g. zonal reserve requirements and interface ﬂow limits)

With this approach the OPF formulation is modiﬁed in the

Additional variables are added using the

Here

Quadratic Costs are added using theadd _quad _cost method.Here

myQcost is the name of the cost set,Q ,c andk are the , and parameters from (6.47), respectively, andvar _sets is an optional cell array of variable set names. Ifvar _sets is provided, the dimensions ofQ andc will correspond, not to the full optimization vector , but to the subvector formed by stacking only the variable sets speciﬁed invar _sets .General Nonlinear Costs are added using theadd _nln _cost method.Here

myNonlinCost is the name of the cost set,fcn is a function handle andvar _sets is an optional cell array of variable set names. The function handlefcn must point to a function that implements the function from (6.46), returning the value of the function, along with its gradient and Hessian. This function has a format similar to that required by MIPS for its cost function.If

var _sets is provided, instead of the full optimization vector , thex passed to the functionfcn will be a cell array of sub-vectors of corresponding to the speciﬁed variable sets.Legacy Costs are added using theadd _legacy _cost method.Here

myLegacyCost is the name of the cost set,cp is a struct containing the cost parameters , , , , , and from (6.48)–(6.51) in ﬁeldsH ,Cw ,N ,rh ,kk ,dd andmm , respectively, andvar _sets is an optional cell array of variable set names. Ifvar _sets is provided, the number of columns inN will correspond, not to the full optimization vector , but to the subvector formed by stacking only the variable sets speciﬁed invar _sets .

Linear Constraints are added using theadd _lin _constraint method.Here

myLinCons is the name of the constraint set,A ,l andu correspond to additional rows to add to the , and parameters in (6.38), respectively, andvar _sets is an optional cell array of variable set names. Ifvar _sets is provided, the number of columns inA will correspond, not to the full optimization vector , but to the subvector formed by stacking only the variable sets speciﬁed invar _sets .Nonlinear Constraints are added using theadd _nln _constraint method.Here

myNonlinCons is the name of the constraint set,N is the number of constraints being added,iseq is a true or false value indicating whether this is a set of equality (or else inequality) constraints,fcn andhess are function handles andvar _sets is an optional cell array of variable set names. The function handlefcn must point to a function that implements the corresponding constraint function or and its gradients, andhess to one that evaluates the corresponding term in the Hessian of the Lagrangian function. This function has a format similar to that required by MIPS for its cost function. See more details in Section 7.1.2.If

var _sets is provided, instead of the full optimization vector , thex passed to the functionfcn will be a cell array of sub-vectors of corresponding to the speciﬁed variable sets.