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)