.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/plot_models.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_gallery_plot_models.py: Using Models ============ Parameter sets can be associated with a model and the model can be used to compute and visualize properties of the model's dynamics. .. GENERATED FROM PYTHON SOURCE LINES 8-15 .. code-block:: Python import numpy as np from bicycleparameters.parameter_dicts import meijaard2007_browser_jason from bicycleparameters.parameter_sets import Meijaard2007ParameterSet from bicycleparameters.models import Meijaard2007Model np.set_printoptions(precision=3, suppress=True) .. GENERATED FROM PYTHON SOURCE LINES 16-17 Start with a parameter and construct a model with it: .. GENERATED FROM PYTHON SOURCE LINES 17-20 .. code-block:: Python par_set = Meijaard2007ParameterSet(meijaard2007_browser_jason, True) model = Meijaard2007Model(par_set) .. GENERATED FROM PYTHON SOURCE LINES 21-24 Model Attributes ---------------- Models have a set of ordered state variables: .. GENERATED FROM PYTHON SOURCE LINES 24-26 .. code-block:: Python model.state_vars .. rst-class:: sphx-glr-script-out .. code-block:: none ['phi', 'delta', 'phidot', 'deltadot'] .. GENERATED FROM PYTHON SOURCE LINES 27-29 .. code-block:: Python model.state_units .. rst-class:: sphx-glr-script-out .. code-block:: none ['rad', 'rad', 'rad/s', 'rad/s'] .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: Python model.state_vars_latex .. rst-class:: sphx-glr-script-out .. code-block:: none ['\\phi', '\\delta', '\\dot{\\phi}', '\\dot{\\delta}'] .. GENERATED FROM PYTHON SOURCE LINES 33-34 Models have a set of ordered input variables: .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python model.input_vars .. rst-class:: sphx-glr-script-out .. code-block:: none ['Tphi', 'Tdelta'] .. GENERATED FROM PYTHON SOURCE LINES 37-39 .. code-block:: Python model.input_units .. rst-class:: sphx-glr-script-out .. code-block:: none ['N-m', 'N-m'] .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: Python model.input_vars_latex .. rst-class:: sphx-glr-script-out .. code-block:: none ['T_\\phi', 'T_\\delta'] .. GENERATED FROM PYTHON SOURCE LINES 43-47 State Space ----------- All linear models have a method that generates the state and input matrices. The rows and columns match the order of the state and input variables. .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python A, B = model.form_state_space_matrices() A .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 0. , 0. , 1. , 0. ], [ 0. , 0. , 0. , 1. ], [ 8.262, -0.947, -0.03 , -0.214], [17.665, 26.246, 1.993, -2.844]]) .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python B .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 0. , 0. ], [ 0. , 0. ], [ 0.011, -0.066], [-0.066, 4.426]]) .. GENERATED FROM PYTHON SOURCE LINES 54-57 Most all methods on models accept override values for the parameters. So if you want the state and input matrices with :math:`v=14,g=5.1` and :math:`w=2` pass them in as keyword arguments: .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: Python A, B = model.form_state_space_matrices(v=14.0, g=5.1, w=2.0) A .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 0. , 0. , 1. , 0. ], [ 0. , 0. , 0. , 1. ], [ 4.418, -79.135, 0.027, -2.049], [ -1.456, 17.075, 2.721, 2.831]]) .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python B .. rst-class:: sphx-glr-script-out .. code-block:: none array([[0. , 0. ], [0. , 0. ], [0.01 , 0.005], [0.005, 0.454]]) .. GENERATED FROM PYTHON SOURCE LINES 64-67 Eigenmodes ---------- All linear models can produce its eigenvalues and eigenvectors: .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python evals, evecs = model.calc_eigen() evals .. rst-class:: sphx-glr-script-out .. code-block:: none array([-6.744+0.j , -2.915+0.j , 3.392+0.611j, 3.392-0.611j]) .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: Python evecs .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 0.002+0.j , -0.295+0.j , 0.043-0.075j, 0.043+0.075j], [ 0.147+0.j , 0.134+0.j , -0.261+0.047j, -0.261-0.047j], [-0.013+0.j , 0.861+0.j , 0.193-0.229j, 0.193+0.229j], [-0.989+0.j , -0.392+0.j , -0.913+0.j , -0.913-0.j ]]) .. GENERATED FROM PYTHON SOURCE LINES 74-77 Parameters can be overridden as before, but the following line shows how you can pass in an iterable of values for any parameter override and get back an iterable of eigenvalues: .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: Python evals, evecs = model.calc_eigen(v=[2.0, 5.0]) evals .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ -8.143+0.j , -2.967+0.j , 2.681+1.378j, 2.681-1.378j], [-12.638+0.j , -1.726+0.j , -0.003+2.35j , -0.003-2.35j ]]) .. GENERATED FROM PYTHON SOURCE LINES 81-83 The root locus with respect to any parameter, for example here speed ``v``, can be plotted, which uses the iterable of eigenvalues shown above: .. GENERATED FROM PYTHON SOURCE LINES 83-86 .. code-block:: Python speeds = np.linspace(-10.0, 10.0, num=200) _ = model.plot_eigenvalue_parts(v=speeds) .. image-sg:: /gallery/images/sphx_glr_plot_models_001.png :alt: plot models :srcset: /gallery/images/sphx_glr_plot_models_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-88 There are several common customization options available for this plot: .. GENERATED FROM PYTHON SOURCE LINES 88-97 .. code-block:: Python speeds = np.linspace(0.0, 10.0, num=100) ax = model.plot_eigenvalue_parts(v=speeds, colors=['C0', 'C0', 'C1', 'C2'], show_stable_regions=False, hide_zeros=True) ax.set_ylim((-10.0, 10.0)) _ = ax.legend(['Weave Im', None, None, None, 'Weave Re', None, 'Caster', 'Capsize'], ncol=4) .. image-sg:: /gallery/images/sphx_glr_plot_models_002.png :alt: plot models :srcset: /gallery/images/sphx_glr_plot_models_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 98-100 You can choose any parameter in the dictionary to generate the root locus and also override other parameters. .. GENERATED FROM PYTHON SOURCE LINES 100-103 .. code-block:: Python wheelbases = np.linspace(0.2, 5.0, num=50) _ = model.plot_eigenvalue_parts(v=6.0, w=wheelbases) .. image-sg:: /gallery/images/sphx_glr_plot_models_003.png :alt: plot models :srcset: /gallery/images/sphx_glr_plot_models_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 104-106 It is also possible to pass iterables for multiple parameters as long as the length is the same for all. Only the scale for the first will be shown. .. GENERATED FROM PYTHON SOURCE LINES 106-110 .. code-block:: Python trails = np.linspace(-0.2, 0.2, num=50) wheelbases = np.linspace(0.2, 5.0, num=50) _ = model.plot_eigenvalue_parts(c=trails, w=wheelbases) .. image-sg:: /gallery/images/sphx_glr_plot_models_004.png :alt: plot models :srcset: /gallery/images/sphx_glr_plot_models_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 111-113 The eigenvector components can be created for each mode and for a series of parameter values: .. GENERATED FROM PYTHON SOURCE LINES 113-115 .. code-block:: Python _ = model.plot_eigenvectors(v=[1.0, 3.0, 5.0, 7.0]) .. image-sg:: /gallery/images/sphx_glr_plot_models_005.png :alt: Eigenvalue: -6.744+0.000j, Eigenvalue: -2.915+0.000j, Eigenvalue: 3.392+0.611j, Eigenvalue: 3.392-0.611j, Eigenvalue: -9.579+0.000j, Eigenvalue: -2.925+0.000j, Eigenvalue: 1.941+1.796j, Eigenvalue: 1.941-1.796j, Eigenvalue: -12.638+0.000j, Eigenvalue: -1.726+0.000j, Eigenvalue: -0.003+2.350j, Eigenvalue: -0.003-2.350j, Eigenvalue: -15.929+0.000j, Eigenvalue: -2.084+4.205j, Eigenvalue: -2.084-4.205j, Eigenvalue: -0.021+0.000j :srcset: /gallery/images/sphx_glr_plot_models_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 116-117 The eigenmodes can be simulated for specific parameter values: .. GENERATED FROM PYTHON SOURCE LINES 117-120 .. code-block:: Python times = np.linspace(0.0, 5.0, num=100) _ = model.plot_mode_simulations(times, v=6.0) .. image-sg:: /gallery/images/sphx_glr_plot_models_006.png :alt: Eigenvalue: -14.258+0.000j, Eigenvalue: -14.258+0.000j, Eigenvalue: -1.292+3.209j, Eigenvalue: -1.292+3.209j, Eigenvalue: -1.292-3.209j, Eigenvalue: -1.292-3.209j, Eigenvalue: -0.402+0.000j, Eigenvalue: -0.402+0.000j :srcset: /gallery/images/sphx_glr_plot_models_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 121-124 Simulation ---------- A general simulation from initial conditions can also be run: .. GENERATED FROM PYTHON SOURCE LINES 124-127 .. code-block:: Python x0 = np.deg2rad([5.0, -3.0, 0.0, 0.0]) _ = model.plot_simulation(times, x0, v=6.0) .. image-sg:: /gallery/images/sphx_glr_plot_models_007.png :alt: plot models :srcset: /gallery/images/sphx_glr_plot_models_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 128-131 Inputs can be applied in the simulation, for example a simple positive feedback derivative controller on roll shows that the low speed bicycle can be stabilized: .. GENERATED FROM PYTHON SOURCE LINES 131-135 .. code-block:: Python x0 = np.deg2rad([5.0, -3.0, 0.0, 0.0]) _ = model.plot_simulation(times, x0, input_func=lambda t, x: np.array([0.0, 50.0*x[2]]), v=2.0) .. image-sg:: /gallery/images/sphx_glr_plot_models_008.png :alt: plot models :srcset: /gallery/images/sphx_glr_plot_models_008.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.207 seconds) .. _sphx_glr_download_gallery_plot_models.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_models.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_models.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_models.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_