尝试绘制总和时出现 python 中的类型错误

发布于 2024-11-09 02:16:39 字数 1326 浏览 2 评论 0原文

因为我得到了在这里提出另一个问题的建议,所以......我想绘制总和,并且我有一个代码:

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:
image

) 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:
image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画尸师 2024-11-16 02:16:39

您不能将 numpy.ndarray 除以 numpy.float64。这是有问题的代码:

return 0.5*(1 + sum( (a**(2*n)*cos(2*sqrt(1 + n)*t))/(e**a**2*factorial(n))

You can't divide a numpy.ndarray by a numpy.float64. This is the problematic code:

return 0.5*(1 + sum( (a**(2*n)*cos(2*sqrt(1 + n)*t))/(e**a**2*factorial(n))
一桥轻雨一伞开 2024-11-16 02:16:39

我不明白该生成器表达式在 f1 中的 return 语句末尾正在做什么,但是 this:

a=4
t = linspace(0, 35, 1000)
y1 = numpy.array([f1(t_i) for t_i in t])

应该可以帮助您。它的作用是通过循环 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:

a=4
t = linspace(0, 35, 1000)
y1 = numpy.array([f1(t_i) for t_i in t])

should get you somewhere. What it does is create a new numpy.array by looping through t, calling f1 on each value and building a list from the results. This way the t inside f1 is a single number rather than the whole array at once, which means that your arithmetic can be properly applied to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文