How can I get the derivative of a complex function including reshape, max operation?
2 views (last 30 days)
Show older comments
I have a function name
I want to optimize with
to minimize this function 
I want to optimize with 
And I want to provide the gradient of function
to speed up the optimization.
The definition of
is
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:
- I cannot figure an easy way to get the jacobian of
, i.e. the reshape function. - the jacobian of

- the jacobian of
function.
Thank you for your help!!
0 Comments
Accepted Answer
Matt J
on 28 Jan 2021
Edited: Matt J
on 29 Jan 2021
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);
8 Comments
Matt J
on 29 Jan 2021
Edited: Matt J
on 29 Jan 2021
Thank you very much!!! I'm already statisfied.
I'm glad, but I think my other answer is better. As you'll see there, it reaches a much more compact and efficient expression for the final gradient.
can verify your answer for other jacobians and I derive J1 by myself. Can you provide some material for me?
I already did in my earlier comments. It's just a basic Kronecker product theorem that I gave you a wiki link for.
Although my objective F and variable x is real, but the middle consequences can be complex. So if I have complex numbers, how can I compute the jacobian?
The caclulus will depend on the expression.
More Answers (1)
Matt J
on 29 Jan 2021
Edited: Matt J
on 29 Jan 2021
Another way to see the calculation is that if
[F,i]=max(E)
then, assuming we are at a point of differentiability, F is given locally by F=D(i)^2, or,
ai=A(i,:).';
ci=C(i,:).';
F=( ai.'*Z*ci )^2
Since this is satisfied in a local neighborhood of Z, we can readily take the gradient of the expression on the right-hand side. The result, when shaped as a size [a,b] matrix, is,
Fgradient = 2*(ai.'*Z*ci)*ai*ci.'
3 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!