好友
阅读权限20
听众
最后登录1970-1-1
|
矢岛舞美
发表于 2024-11-15 10:56
本帖最后由 矢岛舞美 于 2024-11-15 15:53 编辑
别问,问就是上班无聊
朱利亚集合
曼德博集合
[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坐标版本
[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()
亥姆霍兹吸引子
[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()
罗斯勒吸引子
[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() |
免费评分
-
查看全部评分
|