I am unable to take a second derivative of the following function. When I want the second derivative with respect to u_s it works but with x_s it doesn't work.
Does anyone know what I have done wrong here?
def cost(xt, x_goal, u, Q, R):
return (xt - x_goal).matmul(Q).matmul((xt - x_goal).transpose(0,1)) + u.matmul(R).matmul(u)
x_s = tr.tensor([ 0.0000, -1.0000, 0.0000], dtype=torch.float64, requires_grad=True)
u_s = tr.tensor([-0.2749], dtype=torch.float64, requires_grad=True)
c = cost(x_s, x_Goal, u_s, tr.tensor(Q), tr.tensor(R))
c
output:
tensor([[4.0076]], dtype=torch.float64, grad_fn=<ThAddBackward>)
Cu = grad(c, u_s, create_graph=True)[0]
Cu
output:
tensor([-0.0550], dtype=torch.float64, grad_fn=<ThAddBackward>)
Cuu = grad(Cu, u_s, allow_unused=True)[0]
Cuu
output:
tensor([0.2000], dtype=torch.float64)
Cux = grad(Cu, x_s, allow_unused=True)
Cux
output:
(None,)
I am guessing the Cu itself is completely independent of x_s, but then the derivative should be zero at least, not None!