I trying to implement a problem where the use of a storage device is optimized over a certain time period. I read through the documentation and the forum, but so far I did not stumble across the correct way to implement the following constraint:

with P{bat}(\tau)_{+} = max(0,P{bat}) and P{bat}(\tau)_{-} = min(0,P{bat})

My first question is how to I implement the dependency between (\tau) and ((\tau)+1) for the SOC, the State-of-Charge of the storage device.
Furthermore, as I was a concave component with the min. I would know that I am performing an illegal operation by adding a convex and a concave term. Does CVX have the ability to handle that anyways or do I have to find a work around by introducing two variables for Pbat?

Now of course, my instinct is just to mark this “nonconvex” and send you to the Garbage Bin of Nonconvexity. After all, it’s not right to think of this as an “illegal operation”. It is nonconvex, and no amount of rearrangement can change that.

But if you are willing to add a binary variable, you can indeed handle a concave min term. This requires MOSEK, Gurobi, or GLPK to solve. The key is to recognize that
$$\min{x,y} \leq z \quad\Longrightarrow\quad
\begin{matrix}
x \leq z + t_1 M \ y \leq z + t_2 M \ t_1+t_2=1 \ t_1,t_2\in{0,1}
\end{matrix}

where $M$ is large enough to make this work---but not so large that you introduce numerical issues with the solver.
So here is a function, ``midcp_min``, that would implement this. It assumes that both x and y are the same size.
function z = midcp_min(x,y,M)
sx = max(size(x),size(y));
cvx_begin
variable z(sx)
variable t1(sx) binary
variable t2(sx) binary
minimize( z )
t1 + t2 == 1
x <= z + t1 * M
y <= z + t2 * M
cvx_end
I can offer *no warranty or further support for this.* Mixed-integer problems are not something I spend time with. You're going to be responsible henceforth for making this work: fixing any bugs in my model, and selecting a good value of $M$.