1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np
def love_line(x, a, w): y = np.power(np.abs(x), 2/3) + np.exp(1)/3 * np.sqrt(np.pi - np.square(x)) * np.sin(a*np.pi*x) return w*y
if __name__ == "__main__": x = np.arange(-2, 2, 0.01) num_start = 1 num_end = 20 loop = np.arange(num_start,num_end,1) loop = np.hstack((loop,[num_end],loop[::-1]))
fig = plt.figure() ims = []
plt.title(r"$f(x) = x^{2/3}+e/3*(\pi-x^2)^{1/2}*sin(a*\pi*x)$") plt.axis([-2, 2, -1.5, 2.5])
for i in loop: y = love_line(x, i, 1.0) im = plt.plot(x, y, color="r") ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=200) ani.save("love.gif", writer='pillow') plt.close()
x = np.arange(-2, 2, 0.01) y = love_line(x, 10, 1.0) plt.clf plt.plot(x, y, color="r") plt.axis([-2, 2, -1.5, 2.5]) plt.title(r"$f(x) = x^{2/3}+e/3*(\pi-x^2)^{1/2}*sin(a*\pi*x),\ a = 10$") plt.savefig('./love.jpg', dpi=300) plt.show() plt.close()
|