How do I use my own function in CVX model

I was implementing image denoising based on TV minimisation. So the code goes like:
variable xrec(n,m)
minimise (norm(xrec-A))
subject to

Here A is the noisy image. (n,m) is the size of the image, and tvTest is a function that evaluates the total variation of the image:

function [s] = tvTest(img)
%% Computes the total variation of the image.
    [ux, uy] = gradient(img);           % ux <- x-gradient of the image
                                        % uy <- y-gradient of the image
    ds = sqrt(abs(ux).^2 + abs(uy).^2); % Square of both ux and uy
    s = sum(ds(:));                     % Total Variation of the image;

The code gives error:
Error using zeros CLASSNAME input must be a valid numeric or logical class name.

Error in gradient (line 56) g = zeros(size(f),class(f)); % case of singleton dimension

I understand that it is not able to apply the gradient on xrec. But then what could be the solution. Can’t I use a function on xrec?

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 and also read Why isn't CVX accepting my model? READ THIS FIRST!