集成类
因此,我一直在尝试为分配创建一个集成类,尽管我为该类中的功能获得了简单的骨骼结构,但我一直在获得none
结果,这确实使我感到困扰。我写下来的代码在下面写下。 我该怎么做才能使该代码起作用?
import math
class Integrator():
def __init__(self, xMin, xMax, N):
x = []
self.xMin = min(x)
self.xMax = max(x)
self.N = N
def I(self, x):
(x**2)*np.exp(-x)*np.sin(x)
def integrate(self):
y = list(np.arange(self.xMin, self.xMax, self.N))
tot = 0
i = 0
while i < self.xMax:
tot += y [i]
i += self.N
np.sum(tot)*(self.xMax-self.xMin)/self.N
examp = Integrator(1,3,20000)
examp.integrate()
So I have been trying to create an integrate class for an assignment and while I have gotten a simple skeletal structure for the functions within said class, I keep on getting a None
result, which really bugs me. The code that I have written down though is written below.
What do I do to make this code work?
import math
class Integrator():
def __init__(self, xMin, xMax, N):
x = []
self.xMin = min(x)
self.xMax = max(x)
self.N = N
def I(self, x):
(x**2)*np.exp(-x)*np.sin(x)
def integrate(self):
y = list(np.arange(self.xMin, self.xMax, self.N))
tot = 0
i = 0
while i < self.xMax:
tot += y [i]
i += self.N
np.sum(tot)*(self.xMax-self.xMin)/self.N
examp = Integrator(1,3,20000)
examp.integrate()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少
返回
在集成
和i
方法上的语句。这就是为什么他们都只返回无
。除此之外,还有其他一些问题。例如,
min
和Max
语句在这里无法使用。这些运算符不适合空序列(这是x
是)。也许您的意思是self.xmin = xmin
?除此之外,
Integrate
方法还有一些好奇心。例如,<代码> 循环只会进行一次迭代,因为i&lt; self.xmax
(在您的示例中为3),但是每个迭代i
都会通过self.n
递增(在您的示例中为20000)。np.sum(tot)
也是不合逻辑的,因为仅当tot
是“类似数组的”时,tot只是一个float(或int)。无需总结其中之一。然后,
列表(np.arange(self.xmin,self.xmax,self.n))
可能不会做您期望的事情。np.arange
被给出了开始,停止和 spep 参数。这意味着它以1(self.xmin
)开始,然后设置20000的一步(self.n
),然后因为它大于3(self.xmax
),它不会包括一个。因此,y = [1]
。也许您想要y = list(np.arange(self.xmin,self.xmax,1/self.n))
,以便步骤使得y
有40000的长度。也就是说,我不知道您希望从这种方法中返回什么。也许是按照这一行:
返回
7.999900000008443
。You're missing a
return
statement on theintegrate
andI
methods. That is why they both exclusively returnNone
.Beyond that, there are some other issues. For example, the
min
andmax
statements here will not work. These operators do not work on an empty sequence (which is whatx
is). Perhaps you meantself.xMin = xMin
?Beyond that, there are some curiosities with the
integrate
method. For example, thewhile
loop will only do one iteration, becausei < self.xMax
(which is 3 in your example), but every iterationi
gets incremented byself.N
(which is 20000 in your example).np.sum(tot)
is also illogical, as that only works whentot
is "array-like", but tot is just a float (or int). No need to sum one of those.Then,
list(np.arange(self.xMin, self.xMax, self.N))
likely does not do what you're expecting.np.arange
is given a start, stop and step parameter. That means that it starts at 1 (self.xMin
), then sets a step of 20000 (self.N
), and then because that is larger than stop of 3 (self.xMax
), it will not include that one. So,y = [1]
. Maybe you'd wanty = list(np.arange(self.xMin, self.xMax, 1 / self.N))
, so that the step is such thaty
has a length of 40000.That said, I have no idea what you're expecting to get returned from this method. Perhaps it's along the lines of this though:
Which returns
7.999900000008443
.