混沌蝴蝶-洛伦兹吸引子图形动态化展现
本帖最后由 矢岛舞美 于 2024-11-15 15:53 编辑别问,问就是上班无聊
朱利亚集合
曼德博集合
import numpy as np
from tkinter import Tk, Canvas
# 洛伦兹系统的参数
sigma = 10.0
rho = 28.0
beta = 8.0 / 3.0
# 洛伦兹系统的微分方程
def lorenz(x, y, z, sigma=sigma, rho=rho, beta=beta):
dx_dt = sigma * (y - x)
dy_dt = x * (rho - z) - y
dz_dt = x * y - beta * z
return dx_dt, dy_dt, dz_dt
# 初始化状态
dt = 0.01
num_steps = 10000
# 初始化轨迹
xs = np.empty(num_steps + 1)
ys = np.empty(num_steps + 1)
zs = np.empty(num_steps + 1)
# 初始条件
xs, ys, zs = (0.0, 1.0, 1.05)
# 计算轨迹
for i in range(num_steps):
dx, dy, dz = lorenz(xs, ys, zs)
xs = xs + dx * dt
ys = ys + dy * dt
zs = zs + dz * dt
# 创建Tk窗口
root = Tk()
root.title("Lorenz Attractor")
# 创建Canvas,背景设置为蓝色
width, height = 800, 600
canvas = Canvas(root, width=width, height=height, bg="black")
canvas.pack()
# 坐标转换函数
def transform(x, y):
"""将3D坐标转换为2D坐标"""
scale = 10
return width // 2 + int(x * scale), height // 2 - int(y * scale)
# 动画更新函数
def update(step=0):
if step < num_steps:
x, y = transform(xs, ys)
# 绘制白色的点
canvas.create_oval(x, y, x+1, y+1, fill='white', outline='white')
root.after(1, update, step + 1)
# 开始动画
update()
# 运行Tk主循环
root.mainloop()
3d坐标版本
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
# 洛伦兹系统的参数
sigma = 10.0
rho = 28.0
beta = 8.0 / 3.0
# 洛伦兹系统的微分方程
def lorenz(x, y, z, sigma=sigma, rho=rho, beta=beta):
dx_dt = sigma * (y - x)
dy_dt = x * (rho - z) - y
dz_dt = x * y - beta * z
return dx_dt, dy_dt, dz_dt
# 初始化状态
dt = 0.01
num_steps = 30000
# 初始化轨迹
xs = np.empty(num_steps + 1)
ys = np.empty(num_steps + 1)
zs = np.empty(num_steps + 1)
# 初始条件
xs, ys, zs = (0.0, 1.0, 1.05)
# 计算轨迹
for i in range(num_steps):
dx, dy, dz = lorenz(xs, ys, zs)
xs = xs + dx * dt
ys = ys + dy * dt
zs = zs + dz * dt
# 创建Tkinter窗口
root = tk.Tk()
root.title("Lorenz Attractor")
# 创建Matplotlib图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(min(xs), max(xs))
ax.set_ylim(min(ys), max(ys))
ax.set_zlim(min(zs), max(zs))
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title("Lorenz Attractor")
# 将Matplotlib图形嵌入到Tkinter窗口中
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# 动画更新函数
def update(step=0):
if step < num_steps:
ax.clear()
ax.scatter(xs[:step], ys[:step], zs[:step], c='red', s=0.5)
ax.set_xlim(min(xs), max(xs))
ax.set_ylim(min(ys), max(ys))
ax.set_zlim(min(zs), max(zs))
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title("Lorenz Attractor")
canvas.draw()
root.after(10, update, step + 10)# 每10步更新一次
# 开始动画
update()
# 运行Tkinter主循环
root.mainloop()
亥姆霍兹吸引子
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
# 定义亥姆霍兹吸引子的微分方程(示例)
def helmholtz_attractor(state, a, b, c):
x, y, z = state
dxdt = a * (y - x)
dydt = x * (b - z) - y
dzdt = x * y - c * z
return np.array()
# 使用欧拉法进行数值积分
def euler_integration(initial_state, a, b, c, t_span, dt):
num_steps = int(t_span / dt)
states = np.zeros((num_steps, 3))
states = initial_state
for i in range(1, num_steps):
states = states + helmholtz_attractor(states, a, b, c) * dt
return states
# 初始条件和参数
initial_state =
a, b, c = 10.0, 28.0, 8.0/3.0
t_span = 50
dt = 0.01
# 求解微分方程
solution = euler_integration(initial_state, a, b, c, t_span, dt)
# 创建Tkinter窗口
root = tk.Tk()
root.title("亥姆霍兹吸引子")
# 创建Matplotlib图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(solution[:, 0], solution[:, 1], solution[:, 2])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 将Matplotlib图形嵌入到Tkinter窗口中
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# 运行Tkinter主循环
root.mainloop()
罗斯勒吸引子
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
# 定义罗斯勒吸引子的微分方程
def rossler_attractor(state, a, b, c):
x, y, z = state
dxdt = -y - z
dydt = x + a * y
dzdt = b + z * (x - c)
return np.array()
# 使用欧拉法进行数值积分
def euler_integration(initial_state, a, b, c, t_span, dt):
num_steps = int(t_span / dt)
states = np.zeros((num_steps, 3))
states = initial_state
for i in range(1, num_steps):
states = states + rossler_attractor(states, a, b, c) * dt
return states
# 初始条件和参数
initial_state =
a, b, c = 0.2, 0.2, 5.7
t_span = 100
dt = 0.01
# 求解微分方程
solution = euler_integration(initial_state, a, b, c, t_span, dt)
# 创建Tkinter窗口
root = tk.Tk()
root.title("罗斯勒吸引子")
# 创建Matplotlib图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(solution[:, 0], solution[:, 1], solution[:, 2])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 将Matplotlib图形嵌入到Tkinter窗口中
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# 运行Tkinter主循环
root.mainloop() 羡慕无聊的大神:Dweeqw 很漂亮,学习一下 搞的还可以,确实无聊 还挺好看的,感谢分享 无聊也别刷这么多主题,你这些主题都是一个类型,可以放到学习记录,以后在一个帖子里更新,不需要开多个主题。 摸鱼学习{:1_921:} 很漂亮,感谢分享。 厉害了!陪一个js版本{:301_1003:}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lorenz Attractor</title>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
body {
position: relative;
background-color: #000;
}
canvas {
width: 800px;
height: 600px;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<canvas></canvas>
</body>
<script>
// 洛伦兹系统的参数
const dftSigma = 10.0;
const dftRho = 28.0;
const dftBeta = 8.0 / 3.0;
// 初始化状态
const speed = 3;
const width = 800;
const height = 600;
const dt = 0.01;
const num_steps = 10000;
// 洛伦兹系统的微分方程
function lorenz(x, y, z, sigma = dftSigma, rho = dftRho, beta = dftBeta) {
const dx_dt = sigma * (y - x);
const dy_dt = x * (rho - z) - y;
const dz_dt = x * y - beta * z;
return ;
}
// 初始化轨迹
const xs = new Array(num_steps + 1).fill(0);
const ys = new Array(num_steps + 1).fill(0);
const zs = new Array(num_steps + 1).fill(0);
// 初始条件
, ys, zs] = ;
// 计算轨迹
for (let i = 0; i < num_steps; i++) {
const = lorenz(xs, ys, zs);
xs = xs + dx * dt;
ys = ys + dy * dt;
zs = zs + dz * dt;
}
const cvs = document.querySelector("canvas");
const ctx = cvs.getContext("2d");
cvs.width = width;
cvs.height = height;
function transform(x, y) {
scale = 10;
return ;
}
//# 动画更新函数
function update(step = 0) {
for (let i = 0; i < speed; i++) {
if (step < num_steps) {
const = transform(xs, ys);
//# 绘制白色的点
ctx.beginPath();
ctx.fillStyle = "white";
ctx.fillRect(x, y, 1, 1);
ctx.closePath();
}
step++;
}
if (step < num_steps) requestAnimationFrame(() => update(step + 1));
}
requestAnimationFrame(() => update());
</script>
</html>
很漂亮,学习一下