我当时在做一些数学作业,使用matplotlib进行可视化。然后我决定为它添加滑块(所有都是根据matplotlib demo完成的)。
这是最初的程序
import numpy as np
import matplotlib.pyplot as plt
a = 1
b = 1
start = 0
end = 1
tau_1 = 0.5
PRECISE_TIME = np.linspace(start, end + tau_1, 1000)
TIME_1 = np.arange(start, end + tau_1, tau_1)
u_0 = b
A_2_1 = []
A_3_1 = []
PRECISE_SOL = lambda t : b * np.exp(-a * t)
F = lambda t : 0
#A_2
def A_2(arr, const, times, tau):
u_n = const
for t in times:
arr.append(u_n)
u_n = (F(t) - a * u_n) * tau + u_n
return np.array(arr)
#A_3
def A_3(arr, const, times, tau):
u_n = const
for t in times:
arr.append(u_n)
if len(arr) <= 1:
u_n = (F(t) - a * const / 2 + const / tau) / (a / 2 + 1 / tau)
else:
u_n_1 = arr[-2] #u_(n-1)
u_n = (F(t) - a*u_n) * 2 * tau + u_n_1 #U_(n+1)
return np.array(arr)
plt.figure(figsize=(12, 40))
plt.plot(PRECISE_TIME, PRECISE_SOL(PRECISE_TIME), label='precise solution')
#plt.scatter(TIME_1, A_2_1, s=5, c='red', label='A_2 for step ' + str(tau_1))
#plt.scatter(TIME_1, A_3_1, s=8, c='green', label='A_3 for step ' + str(tau_1))
plt.plot(TIME_1, A_2([], b, TIME_1, tau_1), c='red', label='A_2 for step ' + str(tau_1))
plt.plot(TIME_1, A_3([], b, TIME_1, tau_1), c='green', label='A_3 for step ' + str(tau_1))
plt.legend()
plt.title("for a = " + str(a) + ", b = " + str(b) + ", f(t) = 0")
plt.show()
下面是带有滑块的一个(请注意,函数A_2和A_3与原始版本相同):
import numpy as np
from time import sleep
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button
a = 1
b = 1
start = 0
end = 1
#A_2
def A_2(arr, const, times, tau, a):
u_n = const
for t in times:
arr.append(u_n)
u_n = (F(t) - a * u_n) * tau + u_n
return np.array(arr)
#A_3
def A_3(arr, const, times, tau, a):
u_n = const
for t in times:
arr.append(u_n)
if len(arr) <= 1:
u_n = (F(t) - a * const / 2 + const / tau) / (a / 2 + 1 / tau)
else:
u_n_1 = arr[-2] #u_(n-1)
u_n = (F(t) - a*u_n) * 2 * tau + u_n_1 #U_(n+1)
return np.array(arr)
#define initial parameters
init_tau = 0.1
#different times
PRECISE_TIME = np.linspace(start, end + init_tau, 1000)
TIME = np.arange(start, end + init_tau, init_tau)
#lambda expressions
PRECISE_SOL = lambda t : b * np.exp(-a * t)
F = lambda t : 0
# Create the figure and the line that we will manipulate
fig, ax = plt.subplots()
linep, = ax.plot(PRECISE_TIME, PRECISE_SOL(PRECISE_TIME))
lineA_2, = ax.plot(TIME, A_2([], b, TIME, init_tau, a), c='red')
lineA_3, = ax.plot(TIME, A_3([], b, TIME, init_tau, a), c='green')
#lineA_2 = ax.scatter(TIME, A_2([], b, TIME, init_tau), s=5, c='red', label="A_2")
#lineA_3 = ax.scatter(TIME, A_3([], b, TIME, init_tau), s=8, c='green', label="A_3")
ax.set_ylabel('Solution')
ax.set_xlabel('Time')
# adjust the main plot to make room for the sliders
fig.subplots_adjust(left=0.25, bottom=0.25)
# Make a horizontal slider to control the tau.
axtau = fig.add_axes([0.25, 0.1, 0.65, 0.03])
tau_slider = Slider(
ax=axtau,
label='Tau',
valmin=0.01,
valmax=0.5,
valinit=init_tau,
)
# The function to be called anytime a slider's value changes
def update(val):
lineA_2.set_ydata(A_2([], b, TIME, tau_slider.val, a))
lineA_3.set_ydata(A_3([], b, TIME, tau_slider.val, a))
#sleep(1)
fig.canvas.draw_idle()
# register the update function with slider
tau_slider.on_changed(update)
# Create a `matplotlib.widgets.Button` to reset the sliders to initial values.
resetax = fig.add_axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', hovercolor='0.975')
def reset(event):
tau_slider.reset()
button.on_clicked(reset)
plt.show()
并且他们为相同的参数(τ)绘制了不同的图表
我认为我的计算机无法跟上重新计算结果的速度,所以我添加了time.sleep()
函数,但这没有帮助
1条答案
按热度按时间g2ieeal71#
你只是更新了你的线的y值,而
TIME
保持不变(用init_tau
初始化)。不清楚为什么你要使用tau
作为np.arange
中的步骤,但我认为你需要在每次tau
改变时计算TIME
,并完全重绘你的线:输出(tau=0.5,如第一个示例):