Wednesday, 28 May 2025

Rossler Attractor Pattern using Python

 


import matplotlib.pyplot as plt

import numpy as np

from scipy.integrate import solve_ivp

def rossler(t,state,a=0.2,b=0.2,c=5.7):

    x,y,z=state

    dx=-y-z

    dy=x+a*y

    dz=b+z*(x-c)

    return[dx,dy,dz]

initial_state=[0.0,1.0,0.0]

t_span=(0,100)

t_eval=np.linspace(t_span[0],t_span[1],10000)

solution=solve_ivp(rossler,t_span,initial_state,t_eval=t_eval,rtol=1e-8)

x=solution.y[0]

y=solution.y[1]

z=solution.y[2]

fig=plt.figure(figsize=(6,6))

ax=fig.add_subplot(111,projection='3d')

ax.plot(x,y,z,lw=0.5,color='purple')

ax.set_title('Rossler Attractor')

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

plt.show()

#source code --> clcoding.com 

Code Explanation:

1. Import Required Libraries

import numpy as np

import matplotlib.pyplot as plt

from scipy.integrate import solve_ivp

numpy for numerical operations.

matplotlib.pyplot for plotting.

solve_ivp from SciPy to numerically solve differential equations.

2. Define the Rössler System Differential Equations

def rossler(t, state, a=0.2, b=0.2, c=5.7):

    x, y, z = state

    dx = -y - z

    dy = x + a * y

    dz = b + z * (x - c)

    return [dx, dy, dz]

Function rossler returns derivatives [dx/dt, dy/dt, dz/dt] for given state (x, y, z).

Parameters a, b, c control system behavior.

The Rössler system is a famous chaotic system.

3. Set Initial Conditions

initial_state = [0.0, 1.0, 0.0]

Starting point of the system in 3D space (x=0, y=1, z=0).

4. Define Time Interval and Evaluation Points

t_span = (0, 100)

t_eval = np.linspace(t_span[0], t_span[1], 10000)

t_span is time range from 0 to 100.

t_eval defines 10,000 evenly spaced points where the solution is calculated.

5. Numerically Solve the ODE System

solution = solve_ivp(rossler, t_span, initial_state, t_eval=t_eval, rtol=1e-8)

Uses solve_ivp to integrate the Rössler system over time.

High accuracy is requested with rtol=1e-8.

6. Extract Solutions

x = solution.y[0]

y = solution.y[1]

z = solution.y[2]

Extract arrays of x, y, z values from the solution at each time point. 

7. Create 3D Plot

fig = plt.figure(figsize=(10,7))

ax = fig.add_subplot(111, projection='3d')

Creates a new figure with size 10x7 inches.

Adds a 3D subplot for plotting. 

8. Plot the Rössler Trajectory

ax.plot(x, y, z, lw=0.5, color='purple')

Plots the 3D curve traced by (x, y, z) over time.

 Line width is thin (0.5), color purple.

9. Set Plot Titles and Axis Labels

ax.set_title('Rössler Attractor')

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

Sets the plot title and labels for the x, y, and z axes.

 10. Show the Plot

plt.show()

Displays the interactive 3D plot window with the attractor curve.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (217) Data Strucures (13) Deep Learning (68) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) Python Coding Challenge (884) Python Quiz (342) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)