.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_gallery/mplot3d/plot_lorenz_attractor.f90" .. 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_auto_gallery_mplot3d_plot_lorenz_attractor.f90: Lorenz Attractor: 3D Visualization ================================== The Lorenz attractor is a classic example of chaotic dynamics. This program solves the Lorenz differential equations and visualizes the resulting 3D trajectory. The famous "butterfly" shape emerges from the chaotic behavior. The Lorenz system is .. math:: \begin{aligned} \frac{dx}{dt} &= \sigma (y - x), \\ \frac{dy}{dt} &= x (\rho - z) - y, \\ \frac{dz}{dt} &= x y - \beta z, \end{aligned} where :math:`\sigma`, :math:`\rho` and :math:`\beta` are parameters. The classic values :math:`\sigma = 10`, :math:`\rho = 28` and :math:`\beta = 8/3` give chaotic behaviour. .. GENERATED FROM PYTHON SOURCE LINES 21-31 .. code-block:: Fortran program plot_lorenz_attractor use fplotlib, only: dp, figure, grid, plot3d, savefig, title, xlabel, ylabel, & zlabel implicit none integer, parameter :: n_steps = 10000 real(dp) :: x(n_steps), y(n_steps), z(n_steps) real(dp) :: x_curr(3), dx(3), dt integer :: i real(dp), parameter :: sigma = 10.0_dp, rho = 28.0_dp, beta = 8.0_dp/3.0_dp .. GENERATED FROM PYTHON SOURCE LINES 32-34 Set the initial conditions and the time step :math:`\Delta t` for numerical integration .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: Fortran :dedent: 1 x_curr = 1.0_dp dt = 0.01_dp .. GENERATED FROM PYTHON SOURCE LINES 39-42 Integrate the Lorenz equations with the explicit Euler method, :math:`\mathbf{r}_{k+1} = \mathbf{r}_k + \Delta t \, \dot{\mathbf{r}}_k`. Each iteration computes the derivatives and updates the position. .. GENERATED FROM PYTHON SOURCE LINES 44-57 .. code-block:: Fortran :dedent: 1 do i = 1, n_steps x(i) = x_curr(1) y(i) = x_curr(2) z(i) = x_curr(3) ! Compute derivatives (Lorenz equations) dx(1) = sigma * (x_curr(2) - x_curr(1)) dx(2) = x_curr(1) * (rho - x_curr(3)) - x_curr(2) dx(3) = x_curr(1) * x_curr(2) - beta * x_curr(3) ! Update positions using Euler method x_curr = x_curr + dt * dx end do .. GENERATED FROM PYTHON SOURCE LINES 58-61 Create a 3D plot of the Lorenz attractor trajectory. The :f:subr:`plot3d` function draws a 3D line through the points. The characteristic double-loop "butterfly" shape should be clearly visible. .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Fortran :dedent: 1 call figure(figsize=[10.0_dp, 8.0_dp]) call plot3d(x, y, z, "b-", lw=0.5_dp, label="Lorenz trajectory") .. GENERATED FROM PYTHON SOURCE LINES 66-67 Configure the 3D view and add labels .. GENERATED FROM PYTHON SOURCE LINES 68-78 .. code-block:: Fortran call xlabel("X") call ylabel("Y") call zlabel("Z") call title("Lorenz Attractor") call grid(.true.) call savefig("lorenz_attractor.svg") print *, "wrote lorenz_attractor.svg" end program plot_lorenz_attractor .. image-sg:: /auto_gallery/mplot3d/images/sphx_glr_plot_lorenz_attractor_001.svg :alt: plot lorenz attractor :srcset: /auto_gallery/mplot3d/images/sphx_glr_plot_lorenz_attractor_001.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none wrote lorenz_attractor.svg .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.044 seconds) .. _sphx_glr_download_auto_gallery_mplot3d_plot_lorenz_attractor.f90: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Fortran source code: plot_lorenz_attractor.f90 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_lorenz_attractor.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_