Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. import torch
  2.  
  3. dt = torch.tensor([0.02]).double()
  4. m = torch.tensor([1.0]).double()
  5. l = torch.tensor([1.0]).double()
  6. g = torch.tensor([9.80665]).double()
  7. pi = torch.tensor([3.14159265359]).double()
  8.  
  9. def pendulom (x, u):
  10. theta = torch.atan2(x[0], x[1])
  11. theta_dot = x[2]
  12. next_theta = theta + theta_dot*dt
  13.  
  14. theta_dot_dot = -3.0 * g / (2 * l) * tr.sin(theta + pi)
  15. theta_dot_dot += 3.0 / (m * l**2) * u.double()
  16. next_theta_dot = theta_dot + theta_dot_dot * dt
  17.  
  18. return torch.stack((
  19. torch.sin(next_theta),
  20. torch.cos(next_theta),
  21. next_theta_dot
  22. ),0)
  23.  
  24. x = tr.tensor([0., 1., 0.],dtype=torch.float64, requires_grad=True)
  25. u = tr.tensor([-1.0], dtype=torch.float64, requires_grad=True)
  26. y = pendulom(x,u)
  27. y.backward(torch.ones_like(y))
  28. x.grad
  29.  
  30. output:
  31. tensor([1.2942, 0.0000, 1.0200], dtype=torch.float64)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement