吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 626|回复: 20
收起左侧

[学习记录] 混沌蝴蝶-洛伦兹吸引子图形动态化展现

  [复制链接]
矢岛舞美 发表于 2024-11-15 10:56
本帖最后由 矢岛舞美 于 2024-11-15 15:53 编辑

别问,问就是上班无聊
QQ截图20241115105509.jpg
朱利亚集合
QQ截图20241115113345.jpg
曼德博集合
QQ截图20241115113201.jpg
[Python] 纯文本查看 复制代码
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[0], ys[0], zs[0] = (0.0, 1.0, 1.05)

# 计算轨迹
for i in range(num_steps):
    dx, dy, dz = lorenz(xs[i], ys[i], zs[i])
    xs[i + 1] = xs[i] + dx * dt
    ys[i + 1] = ys[i] + dy * dt
    zs[i + 1] = zs[i] + 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[step], ys[step])
        # 绘制白色的点
        canvas.create_oval(x, y, x+1, y+1, fill='white', outline='white')
        root.after(1, update, step + 1)

# 开始动画
update()

# 运行Tk主循环
root.mainloop()


3d坐标版本
QQ截图20241115154729.jpg
[Python] 纯文本查看 复制代码
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[0], ys[0], zs[0] = (0.0, 1.0, 1.05)

# 计算轨迹
for i in range(num_steps):
    dx, dy, dz = lorenz(xs[i], ys[i], zs[i])
    xs[i + 1] = xs[i] + dx * dt
    ys[i + 1] = ys[i] + dy * dt
    zs[i + 1] = zs[i] + 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()

亥姆霍兹吸引子
QQ截图20241115155048.jpg
[Python] 纯文本查看 复制代码
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([dxdt, dydt, dzdt])

# 使用欧拉法进行数值积分
def euler_integration(initial_state, a, b, c, t_span, dt):
    num_steps = int(t_span / dt)
    states = np.zeros((num_steps, 3))
    states[0] = initial_state

    for i in range(1, num_steps):
        states[i] = states[i - 1] + helmholtz_attractor(states[i - 1], a, b, c) * dt

    return states

# 初始条件和参数
initial_state = [0.1, 0.0, 0.0]
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()


罗斯勒吸引子
QQ截图20241115155239.jpg
[Python] 纯文本查看 复制代码
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([dxdt, dydt, dzdt])

# 使用欧拉法进行数值积分
def euler_integration(initial_state, a, b, c, t_span, dt):
    num_steps = int(t_span / dt)
    states = np.zeros((num_steps, 3))
    states[0] = initial_state

    for i in range(1, num_steps):
        states[i] = states[i - 1] + rossler_attractor(states[i - 1], a, b, c) * dt

    return states

# 初始条件和参数
initial_state = [0.1, 0.0, 0.0]
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()

免费评分

参与人数 3吾爱币 +3 热心值 +2 收起 理由
75506164 + 1 热心回复!
tianqizhineng + 1 + 1 用心讨论,共获提升!
woyucheng + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

9898 发表于 2024-11-15 11:00
羡慕无聊的大神
waweiggfnh 发表于 2024-11-15 11:07
fanmobook 发表于 2024-11-15 11:07
Lanthanum 发表于 2024-11-15 11:11
还挺好看的,感谢分享
Hmily 发表于 2024-11-15 11:16
无聊也别刷这么多主题,你这些主题都是一个类型,可以放到学习记录,以后在一个帖子里更新,不需要开多个主题。
huoxx007 发表于 2024-11-15 11:40
摸鱼学习
chunhwa 发表于 2024-11-15 11:45
很漂亮,感谢分享。
wonder2018 发表于 2024-11-15 12:44
厉害了!陪一个js版本

[HTML] 纯文本查看 复制代码
<!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 [dx_dt, dy_dt, dz_dt];
        }

        // 初始化轨迹
        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);
        // 初始条件
        [xs[0], ys[0], zs[0]] = [0.0, 1.0, 1.05];

        // 计算轨迹
        for (let i = 0; i < num_steps; i++) {
            const [dx, dy, dz] = lorenz(xs[i], ys[i], zs[i]);
            xs[i + 1] = xs[i] + dx * dt;
            ys[i + 1] = ys[i] + dy * dt;
            zs[i + 1] = zs[i] + 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 [width / 2 + Math.floor(x * scale), height / 2 - Math.floor(y * scale)];
        }

        //# 动画更新函数
        function update(step = 0) {
            for (let i = 0; i < speed; i++) {
                if (step < num_steps) {
                    const [x, y] = transform(xs[step], ys[step]);
                    //# 绘制白色的点
                    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>
syh415 发表于 2024-11-15 13:00
很漂亮,学习一下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-1-5 06:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表