Proper usage of dlgradient

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 ] )
y =
10×1 dlarray 1.0e+04 * -1.2000 -1.2000 4.0973 0 0 0 0 0 0.2199 -0.4084
x = dlarray( [ 10; 10; 1100; 0; 0; 0; 563; 0; 10; 10 ] )
x =
10×1 dlarray 10 10 1100 0 0 0 563 0 10 10
grad = dlfeval(@dlJacobian,y,x)
grad =
10×10 dlarray 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
function [Jac] = dlJacobian(y,x)
for i = 1 : length(y)
Jac(i,:) = dlgradient(y(i),x);
end
end

 Accepted Answer

Matt J
Matt J on 9 Feb 2024
Edited: Matt J on 9 Feb 2024
Is my understanding correct or is there a way to find dlgradient with numerical values as input?
You haven't made it clear what the relation between y and x are supposed to be. If you are trying to find the Jacobian of a function z(x,y) that is a function of both x and y, at the fixed values,
x0 = dlarray( [1 ; 2] );
y0 = dlarray( [3 ; 4 ] );
then you can do,
Jac0=dlfeval(@explicitFun, x0,y0)
Jac0 =
2×4 dlarray 1 0 1 0 0 1 0 1
If you are trying to find dy/dx when y is an implicit function of x, as in g(x,y)=0, then dlgradient will not do implicit derivatives for you. However, you can still use dlgradient as a tool to implement the steps of implicit differentiation, e.g.,
dy_dx=dlfeval(@implicitFun, x0,y0)
dy_dx =
2×1 dlarray 0.3333 0.5000
function Jac = explicitFun(x,y)
% Jacobian of z=x+y
z=x+y; %made-up example
for i=numel(z):-1:1
Jac(i,:)=[dlgradient(z(i),x) ; dlgradient(z(i),y) ]';
end
end
function dy_dx = implicitFun(x,y)
%Differentiate the implicit function g(x,y) = x.^2+ y.^2 = constant
%
%The result will be dy_dx = dg_dx ./ dg_dy
g=x.^2+y.^2;
for i=numel(g):-1:1
tmp = dlgradient(g(i),x) ; dg_dx(i,1)=tmp(i);
tmp = dlgradient(g(i),y) ; dg_dy(i,1)=tmp(i);
end
dy_dx=dg_dx./dg_dy;
end

4 Comments

Hello,
Thank you for the answer. I understand it.
To explain my case, y and x are properties of a class and y is dependent on x.
To avoid symbolic representation, I pass the values of x inside this class and calculate y.
Then I try to do automatic differentiation y with respect to x, by passing the objects of the class as arguments of dlgradient function.
My assumption is that when passed as arguments, the object of the class become mere numbers and as a result, gradient of it becomes zero.
Is there any way to keep the link between the y and x intact, while trying to do automatic differentiation?
Since you accept-clicked my answer, I assume you've already resolved your questions?
No. I understand why the gradient becomes all zeroes. But my expectation about the gradient is different. So, I explained my case in the previous comment.
Automatic differentiation requires that
(1) The computation of y from x be launched using dlfeval(Fun,x)
(2) dlgradient be applied in the workspace of Fun() or a function called by Fun
So, if you only wanted the Jacobian of y(x), you could do as below:
x0=[1;2]
x0 = 2×1
1 2
Jacobian=dlfeval(@Fun,dlarray(x0))
Jacobian =
2×2 dlarray 11 0 0 12
function Jacobian = Fun(x)
% Jacobian of y=x.^2/2 + 10*x
y=x.^2/2 + 10*x; %made-up
for i=numel(y):-1:1
Jacobian(i,:)=dlgradient(y(i),x)';
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Products

Release

R2023b

Asked:

on 8 Feb 2024

Edited:

on 21 Feb 2024

Community Treasure Hunt

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

Start Hunting!