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