尝试绘制总和时出现 python 中的类型错误
因为我得到了在这里提出另一个问题的建议,所以......我想绘制总和,并且我有一个代码:
from scitools.std import *
from math import factorial, cos, e, sqrt
from scipy import *
import numpy as np
def f1(t):
return 0.5*(1 + sum( (a**(2*n)*cos(2*sqrt(1 + n)*t))/(e**a**2*factorial(n)) for n in range(0,100)))
a=4
t = linspace(0, 35, 1000)
y1 = f1(t)
plot(t, y1)
xlabel(r'$\tau$')
ylabel(r'P($\tau$)')
legend(r'P($\tau$)')
axis([0.0, 35.0, 0.0, 1.0])
grid(True)
show()
但是我收到错误
Traceback (most recent call last):
File "D:\faxstuff\3.godina\kvantna\vježbe\qm2\v8\plot.py", line 12, in <module>
y1 = f1(t)
File "D:\faxstuff\3.godina\kvantna\vježbe\qm2\v8\plot.py", line 8, in f1
return 0.5*(1 + sum( (a**(2*n)*cos(2*sqrt(1 + n)*t))/(e**a**2*factorial(n)) for n in range(0,100)))
File "C:\Python26\lib\site-packages\numpy\core\fromnumeric.py", line 1415, in sum
res = _sum_(a)
File "D:\faxstuff\3.godina\kvantna\vježbe\qm2\v8\plot.py", line 8, in <genexpr>
return 0.5*(1 + sum( (a**(2*n)*cos(2*sqrt(1 + n)*t))/(e**a**2*factorial(n)) for n in range(0,100)))
TypeError: unsupported operand type(s) for /: 'numpy.ndarray' and 'numpy.float64'
那么问题似乎是什么?它必须对数组做一些事情,但我不知道是什么:\
编辑:Mathematica 中的图片如下所示:
Since I got the advice to make another question here it goes... I want to plot the sum, and I have a code:
from scitools.std import *
from math import factorial, cos, e, sqrt
from scipy import *
import numpy as np
def f1(t):
return 0.5*(1 + sum( (a**(2*n)*cos(2*sqrt(1 + n)*t))/(e**a**2*factorial(n)) for n in range(0,100)))
a=4
t = linspace(0, 35, 1000)
y1 = f1(t)
plot(t, y1)
xlabel(r'$\tau
But I get the error
Traceback (most recent call last):
File "D:\faxstuff\3.godina\kvantna\vježbe\qm2\v8\plot.py", line 12, in <module>
y1 = f1(t)
File "D:\faxstuff\3.godina\kvantna\vježbe\qm2\v8\plot.py", line 8, in f1
return 0.5*(1 + sum( (a**(2*n)*cos(2*sqrt(1 + n)*t))/(e**a**2*factorial(n)) for n in range(0,100)))
File "C:\Python26\lib\site-packages\numpy\core\fromnumeric.py", line 1415, in sum
res = _sum_(a)
File "D:\faxstuff\3.godina\kvantna\vježbe\qm2\v8\plot.py", line 8, in <genexpr>
return 0.5*(1 + sum( (a**(2*n)*cos(2*sqrt(1 + n)*t))/(e**a**2*factorial(n)) for n in range(0,100)))
TypeError: unsupported operand type(s) for /: 'numpy.ndarray' and 'numpy.float64'
So what seems to be the problem? It has got to do something with array, but I don't know what :\
EDIT: The picture, in Mathematica looks like this:
)
ylabel(r'P($\tau$)')
legend(r'P($\tau$)')
axis([0.0, 35.0, 0.0, 1.0])
grid(True)
show()
But I get the error
So what seems to be the problem? It has got to do something with array, but I don't know what :\
EDIT: The picture, in Mathematica looks like this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能将
numpy.ndarray
除以numpy.float64
。这是有问题的代码:You can't divide a
numpy.ndarray
by anumpy.float64
. This is the problematic code:我不明白该生成器表达式在
f1
中的 return 语句末尾正在做什么,但是 this:应该可以帮助您。它的作用是通过循环
t
创建一个新的numpy.array
,对每个值调用f1
并根据结果构建一个列表。这样,f1
中的t
是单个数字,而不是整个数组,这意味着您的算术可以正确地应用于它。I don't understand what that generator expression is doing at the end of the return statement in
f1
, but this:should get you somewhere. What it does is create a new
numpy.array
by looping throught
, callingf1
on each value and building a list from the results. This way thet
insidef1
is a single number rather than the whole array at once, which means that your arithmetic can be properly applied to it.