Proper usage of dlgradient
Show older comments
I have two 10×1 dlarrays, namely y and x.
I am trying to find gradient of (y,x) by running dlgradient command inside a for-loop.
Inside the for-loop, I execute dlgradient(y(i),x). But the loop always returns 0 as output of dlgradient(dx(i),x). So, as output, I get zero matrix of order 10×10.
After a thorough internet search, my understanding is that dlgradient requires y to be a function of x and if we provide constant values for y, it differentiates the constant and returns 0.
Is my understanding correct or is there a way to find dlgradient with numerical values as input?
The code and its output are below:
y = dlarray( [ -12000; -12000; 40973; 0; 0; 0; 0; 0; 2199.1; -4084.1 ] )
x = dlarray( [ 10; 10; 1100; 0; 0; 0; 563; 0; 10; 10 ] )
grad = dlfeval(@dlJacobian,y,x)
function [Jac] = dlJacobian(y,x)
for i = 1 : length(y)
Jac(i,:) = dlgradient(y(i),x);
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Just for fun in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!