Skip to content
Advertisement

Integrate Class

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()

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement