.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "dynamics/floquet_tutorial.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_dynamics_floquet_tutorial.py: Floquet Tutorial ================== .. GENERATED FROM PYTHON SOURCE LINES 6-11 .. code-block:: default import torch, pypose as pp import math, matplotlib.pyplot as plt device = torch.device("cuda" if torch.cuda.is_available() else "cpu") .. GENERATED FROM PYTHON SOURCE LINES 12-16 Preparation ------------- We consider a Floquet system, which is periodic and an example of time-varying systems .. GENERATED FROM PYTHON SOURCE LINES 16-46 .. code-block:: default class Floquet(pp.module.NLS): ''' Floquet system is periodic and time-varying. ''' def __init__(self): super().__init__() def state_transition(self, state, input, t): cc = (2 * math.pi * t / 100).cos() ss = (2 * math.pi * t / 100).sin() A = torch.tensor([[ 1., cc/10], [cc/10, 1.]], device=t.device) B = torch.tensor([[ss], [1.]], device=t.device) return A @ state + B @ input def observation(self, state, input, t): return state + t def subPlot(ax, x, y, xlabel=None, ylabel=None): x = x.detach().cpu().numpy() y = y.detach().cpu().numpy() ax.plot(x, y) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) .. GENERATED FROM PYTHON SOURCE LINES 47-49 Number of time steps --------------------- .. GENERATED FROM PYTHON SOURCE LINES 49-53 .. code-block:: default N = 100 .. GENERATED FROM PYTHON SOURCE LINES 54-55 Time, Input, Initial state .. GENERATED FROM PYTHON SOURCE LINES 55-63 .. code-block:: default time = torch.arange(0, N, device=device) input = (2 * math.pi * time / 50).sin() state = torch.zeros(N, 2, device=device) state[0] = torch.tensor([1., 1.], device=device) obser = torch.zeros(N, 2, device=device) .. GENERATED FROM PYTHON SOURCE LINES 64-65 Create dynamics solver object .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: default model = Floquet().to(device) .. GENERATED FROM PYTHON SOURCE LINES 68-70 Calculate trajectory .. GENERATED FROM PYTHON SOURCE LINES 70-74 .. code-block:: default for i in range(N - 1): state[i + 1], obser[i] = model(state[i], input[i]) .. GENERATED FROM PYTHON SOURCE LINES 75-76 Jacobian computation - Find jacobians at the last step .. GENERATED FROM PYTHON SOURCE LINES 76-81 .. code-block:: default vars = ['A', 'B', 'C', 'D', 'c1', 'c2'] model.set_refpoint() [print(v, getattr(model, v)) for v in vars] .. rst-class:: sphx-glr-script-out .. code-block:: none A tensor([[1.0000, 0.0998], [0.0998, 1.0000]], device='cuda:0') B tensor([[-0.0628], [ 1.0000]], device='cuda:0') C tensor([[1., 0.], [0., 1.]], device='cuda:0') D tensor([[0.], [0.]], device='cuda:0') c1 tensor([ 2.9802e-08, -1.4901e-08], device='cuda:0') c2 tensor([99., 99.], device='cuda:0') [None, None, None, None, None, None] .. GENERATED FROM PYTHON SOURCE LINES 82-83 Jacobian computation - Find jacobians at the 5th step .. GENERATED FROM PYTHON SOURCE LINES 83-87 .. code-block:: default idx = 5 model.set_refpoint(state=state[idx], input=input[idx], t=time[idx]) [print(v, getattr(model, v)) for v in vars] .. rst-class:: sphx-glr-script-out .. code-block:: none A tensor([[1.0000, 0.0951], [0.0951, 1.0000]], device='cuda:0') B tensor([[0.3090], [1.0000]], device='cuda:0') C tensor([[1., 0.], [0., 1.]], device='cuda:0') D tensor([[0.], [0.]], device='cuda:0') c1 tensor([-1.4901e-08, 0.0000e+00], device='cuda:0') c2 tensor([5., 5.], device='cuda:0') [None, None, None, None, None, None] .. GENERATED FROM PYTHON SOURCE LINES 88-89 Create time plots to show dynamics .. GENERATED FROM PYTHON SOURCE LINES 89-95 .. code-block:: default f, ax = plt.subplots(nrows=4, sharex=True) subPlot(ax[0], time, state[:, 0], ylabel='State[0]') subPlot(ax[1], time, state[:, 1], ylabel='State[1]') subPlot(ax[2], time[:-1], obser[:-1, 0], ylabel='Observe[0]') subPlot(ax[3], time[:-1], obser[:-1, 1], ylabel='Observe[1]', xlabel='Time') plt.show() .. image-sg:: /dynamics/images/sphx_glr_floquet_tutorial_001.png :alt: floquet tutorial :srcset: /dynamics/images/sphx_glr_floquet_tutorial_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.101 seconds) .. _sphx_glr_download_dynamics_floquet_tutorial.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: floquet_tutorial.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: floquet_tutorial.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_