【发布时间】:2017-11-21 23:24:50
【问题描述】:
我刚开始使用 matplot lib 尝试绘制微分的 3d 表面。具体来说,我有一个任务是编写自己的 ArcTan 函数,在代码中显示为 ExtArcTan,并将其值与 python 自己的值进行比较,在代码中显示为 atan。我对 X 和 N 的不同值执行此操作。N 指的是我的函数中的级数项数,因为它实际上是泰勒近似。
我从创建 x 和 N 数组开始:
N=np.arange(50)
X=np.arange(-5,6)
N,X=np.meshgrid(N,X)
然后我需要为这两个变量中的每一个计算给定的差异,下面的代码就是引发错误的原因。
Diff=abs(ExtArcTan(X,N)-atan(X))
没有最终的数组,我无法完成将数组放入绘图以生成表面的琐碎任务。
我在 Diff 行收到此错误:
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
不知道我做错了什么:
供参考:
def ArcTanComponent(x,i): #calculates the ith component of arctan(x) taylor series
return (((-1)**i)/(2*i+1))*(x**(2*i+1))
def BaseArcTan(x,N): #calculates all components for given N and sums to give approximation of arctan(x)
Components=[]
for i in range(N+1):
Components.append(ArcTanComponent(x,i))
return np.sum(Components)
def ExtArcTan(x,N): #extends baseArcTan function to work over entire range
if (x>0):
return PI/2 - BaseArcTan(1/x,N)
elif(x<0):
return -PI/2 - BaseArcTan(1/x,N)
elif(x==0):
return 0
【问题讨论】:
标签: python arrays matplotlib