我想为给定的函数执行一个动画。动画给零分区
我现在的问题是,它给了我一个零除,但我不明白这是如何发生的。
我已经把一些打印语句,他们都没事。
我不确定FuncAnimation是如何工作的,因为如果我忽略这个调用,而只是运行“animate(n)”,那么我没有错误。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
R = 1
b = 0.02
m0 = 0.02
n = np.linspace(1,0.01 ,10)
r = np.linspace(0,R ,10)
fig, axes = plt.subplots()
line, = axes.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(n):
u_r = ((b/2 * m0) ** (1/n)) * (n/(n+1)) * (R**(1+1/n) - r**(1+1/n))
print()
print("1/n = {0} , n/n+1= {1} ,1+1/n = {2}".format(1/n, n/(n+1),1+(1/n)))
print()
print(u_r)
line.set_data(r, u_r[0]*u_r)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=len(n),blit=False)
plt.show()
的错误是在该行:
u_r = ((b/2 * m0) ** (1/n)) * (n/(n+1)) * (R**(1+1/n) - r**(1+1/n))
顺便说一句你使用'animate'作为'FuncAnimation'的一个参数并且有一个同名的函数。 – Psytho
你有一个模块级变量'n',但该变量隐藏在'animate'函数的范围内,因为* animate'的*参数也被称为'n'。该参数将是一个整数,范围从0到'len(n)-1'。看到我的答案。 –
你的动画应该做什么?你想为每个n的值计算u_r吗?或者是其他东西? –