MATLAB: How to get the derivative of a complex function including reshape, max operation

chain rulederivativefmincongradientjacobianmaxminoptimizationreshape

I have a function name I want to optimize with to minimize this function
And I want to provide the gradient of function to speed up the optimization.
The definition of is
function [F] = f(x)
% size(x) = [a*b, 1]
Z = reshape(x, [a, b]); % size(Z)= [a, b]
% size(A) = [c, a]
B = A * Z; % size(B) = [c, b]
% size(C) = [c, b]
D = sum(B.*C, 2); % [c, 1]

E = abs(D).^2; % [c, 1]
F = max(E); % scalar, my objective
end
I want to use chain rules, but I have the following questions:
  1. I cannot figure an easy way to get the jacobian of , i.e. the reshape function.
  2. the jacobian of
  3. the jacobian of function.
Thank you for your help!!

Best Answer

The Jacobian of B(x) is
J1=kron(speye(b),A);
The Jacobian of D(B) is,
J2=kron(ones(1,b),speye(c));
J2(logical(J2))=C;
The max(E) function is not differentiable everywhere, so it is not clear how well fmincon will handle this problem, but at the points where it is differentiable, the Jacobian is,
N=length(E);
e=zeros(1,N);
[~,idx]=max(E);
e(idx)=1;
J3=spdiags(e(:),0,N,N);