import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as ani
#放物線
def f(x,a):
y = a*x**2 + (1-4*a**2)/(4*a)
return y
#座標を準備
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.set_xticks([-2,-1,0,1,2])
ax.set_yticks([-2,-1,0,1,2])
ax.axhline(0,color='0.6')
ax.axvline(0,color='0.6')
ax.set_aspect("equal")
ax.grid()
ax.set_title("2015東大数学 理系第1問",pad=15)
#変数を準備
x = np.arange(-4,4.1,0.1)
log_a0 = np.log((-2+np.sqrt(5))/2)
A = np.exp(np.arange(log_a0,log_a0+6.75,0.01))
#動画の作成
imgs = []
C_past = ax.plot()
for a in A:
y = f(x,a)
C_past += ax.plot(x,y,color="dodgerblue",alpha=0.5)
C_now = ax.plot(x,y,color="navy")
text = ax.text(1.1,-1.8,"a\n= "+"{:>5.2f}".format(a),
color="navy",fontsize=16)
img = C_now + C_past + [text]
imgs.append(img)
#最後の余韻
for i in range(200):
x_left = np.arange(-1,-2.01,-0.01)
y_left = np.sqrt(x_left**2-1)
D_left = [ax.fill_between(x_left,y_left,2*np.ones(101),
color="navy",alpha=i/199,zorder=10)]
x_right = np.arange(1,2.01,0.01)
y_right = np.sqrt(x_right**2-1)
D_right = [ax.fill_between(x_right,y_right,2*np.ones(101),
color="navy",alpha=i/199,zorder=10)]
D_center = [ax.axvspan(-1,1,color="navy",alpha=i/199,zorder=10)]
img = C_past + D_left + D_right + D_center
imgs.append(img)
mov=ani.ArtistAnimation(fig, imgs, 20)
plt.show()