集成类

发布于 2025-02-10 11:24:03 字数 663 浏览 3 评论 0原文

因此,我一直在尝试为分配创建一个集成类,尽管我为该类中的功能获得了简单的骨骼结构,但我一直在获得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 技术交流群。

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

发布评论

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

评论(1

酷到爆炸 2025-02-17 11:24:03

您缺少返回集成i方法上的语句。这就是为什么他们都只返回

除此之外,还有其他一些问题。例如,minMax语句在这里无法使用。这些运算符不适合空序列(这是x是)。也许您的意思是self.xmin = xmin

class Integrator():
    def __init__(self, xMin, xMax, N):
        x = []
        self.xMin = min(x)
        self.xMax = max(x)

除此之外,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的长度。

也就是说,我不知道您希望从这种方法中返回什么。也许是按照这一行:

import math
import numpy as np

class Integrator():
    def __init__(self, xMin, xMax, N):
        x = []
        self.xMin = xMin
        self.xMax = xMax
        self.N = N
    
    def I(self, x):
        return (x**2)*np.exp(-x)*np.sin(x)
        
    def integrate(self):
        y = list(np.arange(self.xMin, self.xMax, 1 / self.N))
        tot = 0
        i = 0
        while i < len(y):
            tot += y[i]
            i += 1
        return tot*(self.xMax-self.xMin)/self.N

examp = Integrator(1,3,20000)
print(examp.integrate())

返回7.999900000008443

You're missing a return statement on the integrate and I methods. That is why they both exclusively return None.

Beyond that, there are some other issues. For example, the min and max statements here will not work. These operators do not work on an empty sequence (which is what x is). Perhaps you meant self.xMin = xMin?

class Integrator():
    def __init__(self, xMin, xMax, N):
        x = []
        self.xMin = min(x)
        self.xMax = max(x)

Beyond that, there are some curiosities with the integrate method. For example, the while loop will only do one iteration, because i < self.xMax (which is 3 in your example), but every iteration i gets incremented by self.N (which is 20000 in your example).

np.sum(tot) is also illogical, as that only works when tot 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 want y = list(np.arange(self.xMin, self.xMax, 1 / self.N)), so that the step is such that y 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:

import math
import numpy as np

class Integrator():
    def __init__(self, xMin, xMax, N):
        x = []
        self.xMin = xMin
        self.xMax = xMax
        self.N = N
    
    def I(self, x):
        return (x**2)*np.exp(-x)*np.sin(x)
        
    def integrate(self):
        y = list(np.arange(self.xMin, self.xMax, 1 / self.N))
        tot = 0
        i = 0
        while i < len(y):
            tot += y[i]
            i += 1
        return tot*(self.xMax-self.xMin)/self.N

examp = Integrator(1,3,20000)
print(examp.integrate())

Which returns 7.999900000008443.

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