Stack Exchange
Calculate gradient for vector function
How can I calculate the gradient for a vector function?
I have
U = sym('U',[5 1]);
X = sym('X',[5 5]);
Y = sym('Y',[5 1]);
f = Cost(U,X,Y)
gradient(f,[U,X,Y])

And I've got 

Error using sym/gradient (line 35)
  The second argument must be a vector of variables.

PS:
function J = Cost(U,X,Y)
 thr = calc_threshold(X,Y,U);
 Y_pred = X*U;
 Y_pred = (Y_pred <= thr);
 J=sum((Y_pred - Y).^2)/(2*size(Y,1));
end
function [threshold] = calc_threshold(X,Y,U)
 Q = X*U;
 Q0 = Q(Y == 0); %non target
 Q1 = Q(Y == 1); %target
 ths = Q + eps;
 ths = sort(ths);
 N1 = sum(Y == 1);
 N0 = sum(Y == 0);
 for k = 1:length(ths)                
    spec_all(k) = length(find(Q0 > ths(k)));       
 end;
 idx = find(spec_all >= 0.95*N0, 1, 'last');
 threshold = ths(idx);  
end


Do you have the Symbolic toolbox? More importantly, do you intend to use the Symbolic toolbox?

No, I haven't. And I prefer not to use this toolbox. Is any different way to calculate this gradient?

You want to compute the gradient of a symbolic matrix as far as I understood. Is that correct ?

Cost function J return scalar value. But inputs of this faunction  - matrices (U,X), so I need dJ/dW, and dJ/dX
