5
\$\begingroup\$

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 
 Output: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:
 continue 
 square = Square(x[row], y[row], x[col], y[col])
 add = add + square.estimate(a)
 print(add)
if __name__ == "__main__":
 main() 
 
``
asked Nov 13, 2020 at 18:45
\$\endgroup\$
2
  • \$\begingroup\$ Note: a is a constant argument of the function to compute. \$\endgroup\$ Commented Nov 13, 2020 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\$ Commented Nov 15, 2020 at 18:37

1 Answer 1

1
\$\begingroup\$

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.

answered Nov 14, 2020 at 22:41
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.