How to use the gradient with respect to a given vector?

6 ビュー (過去 30 日間)
Zac Minson
Zac Minson 2021 年 12 月 13 日
回答済み: Shishir Reddy 2025 年 1 月 10 日
I am trying to implement an algorithm that uses the matrices R - ( m x m matrix) and K - ( n x n ) that are the inputs of a function f(R,K). I am trying to use a starting point z = [r,n] where r is the vectorization of R and n is the vectorization of K. Starting values of R and K are given thus z is also known. I need to calculate r(z) = [gradient_r(f(R,K)) gradient_n(f(R,K))].
Once this is calculated I will do more calculations and use my next starting point in the algorithm, so z is trying to optimize to r(z) = 0 which is the Kurush-Khan-Tucker condition. Thus optimizing my matrices R and K in the process.
I do not know whether I should use numerical or symbolic gradient or even if what I am trying to do is possible. The paper I am referencing appears to use this method.
I have been trying something like this:
syms z [48 1]
r = z([1:16]);
n = z([17:48]);
R_hat = Dr*r;
R = reshape(R_hat, [4,4]);
K_hat = Dn*n;
K = reshape(K_hat, [8,8]) + eye(8);
g = solve(gradient(ft_RK));
Any guidance would be greatly appreciated.

回答 (1 件)

Shishir Reddy
Shishir Reddy 2025 年 1 月 10 日
Hi Zac
To implement the algorithm you're describing, the gradient of the function f(R, K) should be computed with respect to the vectorized forms of R and K. The symbolic computation ismore accurate than numerical approximationsto find these gradients.
Kindly refer to the folloowing steps to understand the workflow of the solution -
1. Define the symbolic variables to represent the vectorized forms of ( R ) and ( K ).
syms z [48 1] real
r = z(1:16);
n = z(17:48);
2. Convert the vectorized forms back into matrices ( R ) and ( K ).
Dr = eye(16);
Dn = eye(32);
R_hat = Dr * r;
R = reshape(R_hat, [4, 4]);
K_hat = Dn * n;
K = reshape(K_hat, [8, 8]) + eye(8);
3. Symbolically define your function ( f(R, K) ).
f_RK = trace(R * K); % example function
4. Use symbolic differentiation to compute the gradient with respect to ( r ) and ( n ).
grad_r = gradient(f_RK, r);
grad_n = gradient(f_RK, n);
r_z = [grad_r; grad_n];
5. Use the gradient results to solve for the conditions
solutions = solve(r_z == 0, z);
disp(solutions);
I hope this helps.

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by