Figure 2.21:

A strange attractor for the R\"ossler system, a=b=0.2, c=5.7.

Code for Figure 2.21

Text of the GNU GPL.

main.py


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt

# Define parameters
a = 0.2
b = 0.2
c = 5.7

# Create a parameter dictionary
p = {'a': a, 'b': b, 'c': c}

# Define the time span and initial conditions
tfin = 750
npts = 10 * tfin
time = np.linspace(0, tfin, npts)
w0 = [1, 1, 1]

# Define the right-hand side (RHS) function for the ODE
def rhs(t, w, p):
    x, y, z = w
    a, b, c = p['a'], p['b'], p['c']
    wdot = [-y - z, x + a * y, b + z * (x - c)]
    return wdot

# Solve the differential equations using solve_ivp
solution = solve_ivp(lambda t, w: rhs(t, w, p), [0, tfin], w0, t_eval=time, method='LSODA')

# Extract the solution
w = solution.y.T

# Save the results to a .dat file using np.savetxt with formatting
with open("rosslerattractor.dat", "w") as f:
    np.savetxt(f, w, fmt='%f', header='x y z')

# Plot the solution to visualize the attractor
plt.plot(w[:, 0], w[:, 1])
plt.xlabel('x')
plt.ylabel('y')
plt.title('Rossler Attractor')
plt.grid(True)
plt.show(block=False)