import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# 设置参数
num_steps = 1000 # 时间步数
dt = 1.0 / num_steps # 每个时间步的长度
t = np.linspace(0, 1, num_steps + 1) # 时间序列
# 初始化维纳过程
W1 = np.zeros(num_steps + 1)
W2 = np.zeros(num_steps + 1)
W3 = np.zeros(num_steps + 1)
# 设置图形
fig, ax = plt.subplots()
ax.set_xlim(0, 1)
ax.set_ylim(-3, 3)
line1, = ax.plot(t, W1, label='Wiener Process 1', linestyle='-',color='k', linewidth=2)
line2, = ax.plot(t, W2, label='Wiener Process 2', linestyle='--')
line3, = ax.plot(t, W3, label='Wiener Process 3', linestyle='--')
# 初始化函数,用于动画的开始
def init():
line1.set_data([], [])
line2.set_data([], [])
line3.set_data([], [])
return line1, line2, line3
# 更新函数,用于动画的每一帧
def update(frame):
# 生成正态分布的随机增量
delta_W1 = np.random.normal(0, np.sqrt(dt))
delta_W2 = np.random.normal(0, np.sqrt(dt))
delta_W3 = np.random.normal(0, np.sqrt(dt))
# 更新维纳过程
W1[frame] = W1[frame - 1] + delta_W1 if frame > 0 else delta_W1
W2[frame] = W2[frame - 1] + delta_W2 if frame > 0 else delta_W2
W3[frame] = W3[frame - 1] + delta_W3 if frame > 0 else delta_W3
line1.set_data(t[:frame+1], W1[:frame+1])
line2.set_data(t[:frame+1], W2[:frame+1])
line3.set_data(t[:frame+1], W3[:frame+1])
return line1, line2, line3
# 创建动画
ani = FuncAnimation(fig, update, frames=num_steps, init_func=init, blit=True, interval=10)
# 显示图形
plt.title('Dynamic Wiener Processes')
plt.xlabel('Time')
plt.ylabel('W(t)')
plt.legend()
plt.show()
标签:维动图,set,frame,维纳,W3,W2,delta,np,过程
From: https://www.cnblogs.com/redufa/p/18537748