필터 지우기
필터 지우기

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

답변 (2개)

Walter Roberson
Walter Roberson 2015년 11월 16일
vars = symvar(YourMatrix);
for K = length(vars) : -1 : 1
MatrixGradient(:,:,K) = gradient(YourMatrix, vars(K));
end
  댓글 수: 2
Nikhil
Nikhil 2015년 11월 16일
Hey, Thank you for your answer! But I tried this approach on simple example. It is giving me an error as follows:
clear all;
vars=symvar('x1 2*(x2)+2;(x1)^2 (x2)^3');
syms x1 x2
M=[x1 2*(x2)+2;(x1)^2 (x2)^3];
for K=length(vars):-1:1
MatrixGrad(:,:,K)=gradient(M,vars(K));
end
The error that I got is as follows: ??? Undefined function or method 'mtimes' for input arguments of type 'cell'.
Error in ==> gradient>parse_inputs at 130 loc(k) = {h*(1:indx(k))};
Error in ==> gradient at 49 [msg,f,ndim,loc,rflag] = parse_inputs(f,varargin);
Looking forward to your assistance in debugging this error, Thank you again,
Sincerely, Nikhil
Walter Roberson
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

댓글을 달려면 로그인하십시오.


Nikhil
Nikhil 2015년 11월 16일
even following code is giving an error message
vars=symvar([x1 2*(x2)+2;(x1)^2 (x2)^3]);
for K=length(vars):-1:1
MatrixGrad(:,:,K)=gradient([x1 2*(x2)+2;(x1)^2 (x2)^3],vars(K));
end
error:
??? Error using ==> zeros
Trailing string input must be a valid numeric class name.
Error in ==> gradient at 64
g = zeros(size(f),class(f)); % case of singleton dimension
Is it because I am using older version of matlab (r2011) ??

카테고리

Help CenterFile Exchange에서 Formula Manipulation and Simplification에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by