I am looking for help for two things:

1.how precise this approximation is (such as Big O or percent error) 2. How efficient the algorithm for time / space usage.

I created an algorithm to estimate an integral. The algorithm creates squares and adds the estimate of the remain sum times the area together. Since this function is 2d, the area is multiplied by f(x, y) at a point. The function in this program is f(x, y) = x + (a * y).

The documented code is below.

def f(x:float, y:float, a:float)->float:
    param x:float, x within the function domain 
    param y:float, y within the function domain 
    param a:float, a paramter of the curve 
    Output: Number of function at the point 
    return x + (a * y)
class Square:
    def __init__(self, xi:float, yi:float, xf:float, yf:float)->None:
        param xi:float, smaller coordinate of x 
        param yi:float, smaller coordinate of y 
        param xf:float, larger cooordinate of x 
        self.xi = xi 
        self.yi = yi 
        self.xf = xf 
        self.yf = yf 
    def estimate(self, a:float)->float:
        param a:float, a from the curve paramter 
        Output: a term to add to estimate the intergal 
        xd = self.xf - self.xi 
        yd = self.yf - self.yi 
        if xd < 0:
            xd = xd * -1 
        if yd < 0:
            yd = yd * -1 
        area_delta = xd * yd 
        est = f(self.xi, self.yi, a) 
        return area_delta * est  

def main()->None:
    Input: None 
    Note: driver program 
    x = [1.0, 2.0, 3.0]
    y = [2.0, 3.0, 4.0]
    a = 1.0 
    add = 0.0 
    square = None  
    for row in range(len(x)):
        for col in range(len(x)):
            if row <= col:
            square = Square(x[row], y[row], x[col], y[col])
            add = add + square.estimate(a)

if __name__ == "__main__":
  • \$\begingroup\$ Note: a is a constant argument of the function to compute. \$\endgroup\$ – Akash Patel Nov 13 '20 at 20:42
  • \$\begingroup\$ It is suggested to wait a few days before accepting an answer to choose the best answer from a broader pool \$\endgroup\$ – Caridorc Nov 15 '20 at 18:37

You are just re-implementing the abs built-in here. (If a number is negative make it positive):

    xd = self.xf - self.xi 
    yd = self.yf - self.yi 
    if xd < 0:
        xd = xd * -1 
    if yd < 0:
        yd = yd * -1 

In general I do not understand why you have a whole class defined. I think this should be written more compactly because the actual calculations performed are very simple.

To be more user friendly I would have a function having as input the domain of integration and the function to integrate like:

def weighted_area(xrange, yrange, function, precision):

An argument should be the precision in case the user wants to change the integration step.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.