.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\plot_betts_10_113_opty.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_plot_betts_10_113_opty.py: Mixed State-Control Constraints (betts-10-113) ============================================== This is example 10.113 from *John T. Betts, Practical Methods for Optimal Control Using NonlinearProgramming*, 3rd edition, Chapter 10: Test Problems. More details are in section 4.14, example 4.10 of the book. **States** - :math:`y_1, y_2` : state variables **Controls** - :math:`u` : control variable .. GENERATED FROM PYTHON SOURCE LINES 20-29 .. code-block:: Python import numpy as np import sympy as sm import sympy.physics.mechanics as me import time from opty.direct_collocation import Problem from opty.utils import create_objective_function, MathJaxRepr .. GENERATED FROM PYTHON SOURCE LINES 31-33 Equations of Motion ------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-49 .. code-block:: Python t = me.dynamicsymbols._t # Parameter p = 0.14 y1, y2 = me.dynamicsymbols('y1, y2') u = me.dynamicsymbols('u') eom = sm.Matrix([ -y1.diff(t) + y2, -y2.diff(t) - y1 + y2*(1.4 - p * y2**2) + 4 * u, u + y1/6 ]) MathJaxRepr(eom) .. raw:: html
$$\left[\begin{matrix}y_{2} - \dot{y}_{1}\\\left(1.4 - 0.14 y_{2}^{2}\right) y_{2} + 4 u - y_{1} - \dot{y}_{2}\\u + \frac{y_{1}}{6}\end{matrix}\right]$$


.. GENERATED FROM PYTHON SOURCE LINES 50-52 Define and Solve the Optimization Problem ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 52-59 .. code-block:: Python num_nodes = 10001 t0, tf = 0.0, 4.5 interval_value = (tf - t0) / (num_nodes - 1) state_symbols = (y1, y2) unkonwn_input_trajectories = (u,) .. GENERATED FROM PYTHON SOURCE LINES 60-61 Specify the objective function and form the gradient. .. GENERATED FROM PYTHON SOURCE LINES 61-72 .. code-block:: Python objective = sm.Integral(u**2 + y1**2, t) obj, obj_grad = create_objective_function( objective, state_symbols, unkonwn_input_trajectories, tuple(), num_nodes, interval_value ) .. GENERATED FROM PYTHON SOURCE LINES 73-79 .. code-block:: Python instance_constraints = ( y1.func(t0) + 5, y2.func(t0) + 5, ) .. GENERATED FROM PYTHON SOURCE LINES 80-81 Bound for the algebraic eom. .. GENERATED FROM PYTHON SOURCE LINES 81-83 .. code-block:: Python eom_bounds = {2: (-np.inf, 0.0)} .. GENERATED FROM PYTHON SOURCE LINES 84-85 Set up Problem .. GENERATED FROM PYTHON SOURCE LINES 85-98 .. code-block:: Python prob = Problem( obj, obj_grad, eom, state_symbols, num_nodes, interval_value, instance_constraints=instance_constraints, eom_bounds=eom_bounds, time_symbol=t, ) .. GENERATED FROM PYTHON SOURCE LINES 99-100 Rough initial guess .. GENERATED FROM PYTHON SOURCE LINES 100-102 .. code-block:: Python initial_guess = np.ones(prob.num_free) * 0.1 .. GENERATED FROM PYTHON SOURCE LINES 103-104 Find the optimal solution. .. GENERATED FROM PYTHON SOURCE LINES 104-114 .. code-block:: Python start = time.time() solution, info = prob.solve(initial_guess) print(f"Solved in {time.time() - start:.2f} seconds.") print(info['status_msg']) Jstar = 44.8044433 print(f"Objective value achieved: {info['obj_val']:.4f}, as per the book " f"it is {Jstar}, so the difference to the value in the book is: " f"{(-info['obj_val'] + Jstar) / Jstar * 100:.3f} % ") .. rst-class:: sphx-glr-script-out .. code-block:: none Solved in 3.68 seconds. b'Algorithm terminated successfully at a locally optimal point, satisfying the convergence tolerances (can be specified by options).' Objective value achieved: 44.7911, as per the book it is 44.8044433, so the difference to the value in the book is: 0.030 % .. GENERATED FROM PYTHON SOURCE LINES 115-116 Plot the optimal state and input trajectories. .. GENERATED FROM PYTHON SOURCE LINES 116-118 .. code-block:: Python _ = prob.plot_trajectories(solution) .. image-sg:: /examples/images/sphx_glr_plot_betts_10_113_opty_001.png :alt: State Trajectories, Input Trajectories :srcset: /examples/images/sphx_glr_plot_betts_10_113_opty_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 119-120 Plot the constraint violations. .. GENERATED FROM PYTHON SOURCE LINES 120-122 .. code-block:: Python _ = prob.plot_constraint_violations(solution, subplots=True, show_bounds=True) .. image-sg:: /examples/images/sphx_glr_plot_betts_10_113_opty_002.png :alt: Constraint violations Values of bounded EoMs :srcset: /examples/images/sphx_glr_plot_betts_10_113_opty_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 123-124 Plot the objective function. .. GENERATED FROM PYTHON SOURCE LINES 124-125 .. code-block:: Python _ = prob.plot_objective_value() .. image-sg:: /examples/images/sphx_glr_plot_betts_10_113_opty_003.png :alt: Objective Value :srcset: /examples/images/sphx_glr_plot_betts_10_113_opty_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.906 seconds) .. _sphx_glr_download_examples_plot_betts_10_113_opty.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_betts_10_113_opty.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_betts_10_113_opty.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_betts_10_113_opty.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_