.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/ct_lqr.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_ct_lqr.py: Continuous Time LQR =================== .. GENERATED FROM PYTHON SOURCE LINES 5-44 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from _sgm_test_util import LTI_plot import condor as co class DblInt(co.ODESystem): A = np.array([[0.0, 1.0], [0.0, 0.0]]) B = np.array([[0.0], [1.0]]) K = parameter(shape=(1, B.shape[0])) x = state(shape=A.shape[0]) dynamic_output.u = -K @ x dot[x] = A @ x + B @ u class DblIntLQR(DblInt.TrajectoryAnalysis): tf = 32.0 initial[x] = [1.0, 0.1] Q = np.eye(2) R = np.eye(1) cost = trajectory_output(integrand=(x.T @ Q @ x + u.T @ R @ u) / 2) class Options: state_rtol = 1e-8 adjoint_rtol = 1e-8 ct_sim = DblIntLQR(K=[1.0, 0.1]) LTI_plot(ct_sim) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/images/sphx_glr_ct_lqr_001.png :alt: DblIntLQR DblIntLQRState.x :srcset: /examples/images/sphx_glr_ct_lqr_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/images/sphx_glr_ct_lqr_002.png :alt: DblIntLQR DblIntLQRDynamicOutput.u :srcset: /examples/images/sphx_glr_ct_lqr_002.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 45-47 Determine the optimal gain by embedding the trajectory analysis in an optimization problem: .. GENERATED FROM PYTHON SOURCE LINES 47-63 .. code-block:: Python class CtOptLQR(co.OptimizationProblem): K = variable(shape=DblIntLQR.K.shape) sim = DblIntLQR(K=K) objective = sim.cost class Options: __implementation__ = co.implementations.ScipyCG lqr_sol = CtOptLQR() print(lqr_sol.K) .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/hostedtoolcache/Python/3.12.11/x64/lib/python3.12/site-packages/condor/implementations/iterative.py:481: RuntimeWarning: Method CG cannot handle bounds. min_out = minimize( [[0.99997598 1.73199404]] .. GENERATED FROM PYTHON SOURCE LINES 64-65 Compare with the solution from the continuous algebraic Riccati equation: .. GENERATED FROM PYTHON SOURCE LINES 65-73 .. code-block:: Python from scipy import linalg S = linalg.solve_continuous_are(DblIntLQR.A, DblIntLQR.B, DblIntLQR.Q, DblIntLQR.R) K = linalg.solve(DblIntLQR.R, DblIntLQR.B.T @ S) print(K) .. rst-class:: sphx-glr-script-out .. code-block:: none [[1. 1.73205081]] .. GENERATED FROM PYTHON SOURCE LINES 74-78 .. code-block:: Python sim_are = DblIntLQR(K=K) LTI_plot(sim_are) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/images/sphx_glr_ct_lqr_003.png :alt: DblIntLQR DblIntLQRState.x :srcset: /examples/images/sphx_glr_ct_lqr_003.png :class: sphx-glr-multi-img * .. image-sg:: /examples/images/sphx_glr_ct_lqr_004.png :alt: DblIntLQR DblIntLQRDynamicOutput.u :srcset: /examples/images/sphx_glr_ct_lqr_004.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 79-81 .. code-block:: Python plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.405 seconds) .. _sphx_glr_download_examples_ct_lqr.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ct_lqr.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ct_lqr.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ct_lqr.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_