.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/ode/van_der_pol.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_ode_van_der_pol.py: Solve an ODE: the Van der Pol problem ===================================== .. GENERATED FROM PYTHON SOURCE LINES 22-31 .. code-block:: default from __future__ import annotations import matplotlib.pyplot as plt from gemseo.algos.ode.ode_problem import ODEProblem from gemseo.algos.ode.ode_solvers_factory import ODESolversFactory from gemseo.problems.ode.van_der_pol import VanDerPol from numpy import array from numpy import zeros .. GENERATED FROM PYTHON SOURCE LINES 32-78 This tutorial describes how to solve an ordinary differential equation (ODE) problem with |g|. A first-order ODE is a differential equation that can be written as .. math:: \frac{ds(t)}{dt} = f(t, s(t)) where the right-hand side of the equation :math:`f(t, s(t))` is a function of time :math:`t` and of a state :math:`s(t)` that returns another state :math:`\frac{ds(t)}{dt}` (see Hartman, Philip (2002) [1964], Ordinary differential equations, Classics in Applied Mathematics, vol. 38, Philadelphia: Society for Industrial and Applied Mathematics). To solve this equation, initial conditions must be set: .. math:: f(t_0, s_0) = y_0 For this example, we consider the Van der Pol equation as an example of a time-independent problem. Solving the Van der Pol time-independent problem ------------------------------------------------ The Van der Pol problem describes the position over time of an oscillator with non-linear damping: .. math:: \frac{d^2 x(t)}{dt^2} - \mu (1-x^2(t)) \frac{dx(t)}{dt} + x(t) = 0 where :math:`x(t)` is the position coordinate at time :math:`t`, and :math:`\mu` is the stiffness parameter. To solve the Van der Pol problem with |g|, we first need to model this second-order equation as a first-order equation. Let :math:`y = \frac{dx}{dt}` and :math:`s = \begin{pmatrix}x\\y\end{pmatrix}`. Then the Van der Pol problem can be rewritten: .. math:: \frac{ds(t)}{dt} = f(t, s(t)) = \begin{pmatrix} y(t) \\ \mu (1-x^2(t)) y(t) - x(t) \end{pmatrix} .. GENERATED FROM PYTHON SOURCE LINES 81-83 Step 1 : Defining the problem ............................. .. GENERATED FROM PYTHON SOURCE LINES 83-97 .. code-block:: default mu = 1e3 def evaluate_f(time, state): return state[1], mu * state[1] * (1 - state[0] ** 2) - state[0] initial_state = array([2, -2 / 3 + 10 / (81 * mu) - 292 / (2187 * mu * mu)]) initial_time = 0 final_time = 0.5 ode_problem = ODEProblem(evaluate_f, initial_state, initial_time, final_time) .. GENERATED FROM PYTHON SOURCE LINES 98-102 By default, the Jacobian of the problem is approximated using the finite difference method. However, it is possible to define an explicit expression of the Jacobian and pass it to the problem. In the case of the Van der Pol problem, this would be: .. GENERATED FROM PYTHON SOURCE LINES 102-116 .. code-block:: default def evaluate_jac(time, state): jac = zeros((2, 2)) jac[1, 0] = -mu * 2 * state[1] * state[0] - 1 jac[0, 1] = 1 jac[1, 1] = mu * (1 - state[0] * state[0]) return jac ode_problem = ODEProblem( evaluate_f, initial_state, initial_time, final_time, jac=evaluate_jac ) .. GENERATED FROM PYTHON SOURCE LINES 117-122 Step 2: Solving the ODE problem ............................... Whether the Jacobian is specified or not, once the problem is defined, the ODE solver is called on the :class:`.ODEProblem` by using the :class:`.ODESolversFactory`: .. GENERATED FROM PYTHON SOURCE LINES 122-124 .. code-block:: default ODESolversFactory().execute(ode_problem, first_step=1e-6) .. GENERATED FROM PYTHON SOURCE LINES 125-142 By default, the Runge-Kutta method of order 4(5) (``"RK45"``) is used, but other algorithms can be applied by specifying the option ``algo_name`` in :meth:`ODESolversFactory().execute`. See more information on available algorithms in `the SciPy documentation `_. Step 3: Examining the results ............................. The convergence of the algorithm can be known by examining :attr:`.ODEProblem.is_converged` and :attr:`.ODEProblem.solver_message`. The solution of the :class:`.ODEProblem` on the user-specified time interval can be accessed through the vectors :attr:`.ODEProblem.states` and :attr:`.ODEProblem.times`. .. GENERATED FROM PYTHON SOURCE LINES 142-147 .. code-block:: default plt.plot(ode_problem.time_vector, ode_problem.result.state_vector[0]) plt.plot(ode_problem.time_vector, ode_problem.result.state_vector[1]) plt.show() .. GENERATED FROM PYTHON SOURCE LINES 148-153 Shortcut ........ The class :class:`.VanDerPol` is available in the package :mod:`gemseo.problems.ode`, so it just needs to be imported to be used. .. GENERATED FROM PYTHON SOURCE LINES 153-155 .. code-block:: default ode_problem = VanDerPol() ODESolversFactory().execute(ode_problem) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_examples_ode_van_der_pol.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: van_der_pol.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: van_der_pol.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_