How to take gradient of matrix(21X21 dimension) with respect to a vector of(1X8) dimensions?
1 次查看(过去 30 天)
显示 更早的评论
Hello all,
I have a function which is represented in matrix format with 21X21 components. It depends on 8 design variables and I want to calculate the gradient of this function with respect to these 8 design variables. So can somebody guide on how to calculate gradient of 21X21 matrix with respect to 1X8 dimension vector in Matlab? The expected answer is 21X21X8 dimension matrix. Any help in this matter would be appreciated, Thank you in advance,
Sincerely, Nikhil
0 个评论
回答(2 个)
Walter Roberson
2015-11-16
vars = symvar(YourMatrix);
for K = length(vars) : -1 : 1
MatrixGradient(:,:,K) = gradient(YourMatrix, vars(K));
end
2 个评论
Walter Roberson
2015-11-16
syms x1 x2
M = [x1 2*(x2)+2;(x1)^2 (x2)^3];
vars = [x1 x2];
for K = length(vars):-1:1
MatrixGrad(:,:,K) = diff(M,vars(K));
end
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!