import matplotlib.pyplot as plt
import math
# https://[Log in to view URL]
a = 0.7 # 変化率
x = 3
y = 1
dt = 0.01
t = 0
t_end = 20.0
x1 = x
x2 = x
x3 = x
x4 = x
result_x1 = []
result_x2 = []
result_x3 = []
result_x4 = []
elapsedTime = []
while t < t_end:
result_x1.append(x1)
result_x2.append(x2)
result_x3.append(x3)
result_x4.append(x4)
elapsedTime.append(t)
t += dt
x1 += dt * a * (y - x1) # dt によっては x_1 を超えることがある
x2 *= pow(a, dt) # 0 への減衰, ほぼ exponential decay, a は 1 秒後における元の値の割合
x3 += (1.0 - pow(a, dt)) * (y - x3)
x4 += (1.0 - math.exp(-a * dt)) * (y - x4)
plt.plot(elapsedTime, result_x1, label='wrong damping')
plt.plot(elapsedTime, result_x2, label='damping to 0')
plt.plot(elapsedTime, result_x3, label='exact damping')
plt.plot(elapsedTime, result_x4, label='exponential decay')
plt.xlabel('x - time')
plt.title('damping')
plt.legend()
plt.show()
To embed this project on your website, copy the following code and paste it into your website's HTML: