How do I use my own function in CVX model

Given that tvTest is called with a CVX variable argument img', all the operations on img within that function must satisfy CVX’s rules. In particular, the MATLAB function gradient is not supported by CVX; therefore it can not be applied to the argument img.

However, I believe gradient is just performing numerical differentiation, so you could implement it yourself, and it should result in an x,y vector of affine CVX expressions. (Note that unfortunately, diff is not supported by CVX either). Then ds can be computed using norm, which can be applied to affine CVX expressions. You can not compute ds as you have done, because although convex, it violates CVX’s rules (that’s why you use norm instead).

You can see a couple of different TV Norm implementations in CVX looking at ncg’s first post at Total Variation (TV) Norm Function Input: Vector or Matrix? , as well as the Royi/mu solution at How to Formulate in CVX - 1D Total Variation Extension . It should be easy to adapt the ideas in these to fit your needs.

So this is fixable. But you must follow CVX’s rule. I recommend you (re-)read the CVX Users’ Guide http://cvxr.com/cvx/doc/ and also read Why isn't CVX accepting my model? READ THIS FIRST!