Advertisement
EWTD

Untitled

Mar 21st, 2023
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. import math
  2. import json
  3.  
  4. class Integral:
  5.     EPS = 0.1
  6.  
  7.     def __init__(self, function):
  8.         self.integral_function = function
  9.  
  10.     def definite_integral(self, a: float, b: float) -> float:
  11.         result = self.definite_integral_extended(a, b)
  12.         return min([(value["RichardsonError"], value["Value"]) for key, value in result["Result"].items()])[1]
  13.  
  14.     def definite_integral_extended(self, a: float, b: float):
  15.         result = {"From": a, "To": b, "Result": {}}
  16.         def convergence(richardsonCharacteristic: int, countMethod):
  17.             n = 1
  18.             integral_n = 0
  19.             integral_2n = 0
  20.             richardson_extrapolation_error = 1
  21.             idx = 0
  22.             while richardson_extrapolation_error > Integral.EPS:
  23.                 n *= 2
  24.                 integral_n = countMethod(a, b, n) if integral_2n == 0 else integral_2n
  25.                 integral_2n = countMethod(a, b, 2*n)
  26.                 richardson_extrapolation_error = abs(integral_2n - integral_n) / (2**richardsonCharacteristic - 1)
  27.                 idx += 1
  28.             return {"RichardsonCharacteristic": richardsonCharacteristic,
  29.                     "RichardsonError": richardson_extrapolation_error,
  30.                     "Iterations": idx + 1,
  31.                     "Value": integral_2n}
  32.  
  33.         result["Result"]["RectangleMethod"] = convergence(2, self.__rectangleMethod)
  34.         result["Result"]["TrapezoidMethod"] = convergence(2, self.__trapezoidMethod)
  35.         result["Result"]["SimpsonMethod"] = convergence(4, self.__simpsonMethod)
  36.         return result
  37.  
  38.     def __rectangleMethod(self, a: float, b: float, n: int):
  39.         h = Integral.__height(a, b, n)
  40.         summ = 0
  41.         for i in range(1, n + 1): summ += self.integral_function(a + (i - 0.5) * h)
  42.         return summ * h
  43.  
  44.     def __trapezoidMethod(self, a: float, b: float, n: int):
  45.         h = Integral.__height(a, b, n)
  46.         summ = 0
  47.         for i in range(1, n): summ += self.integral_function(a + i * h)
  48.         return h * ((self.integral_function(a) + self.integral_function(b)) / 2 + summ)
  49.  
  50.     def __simpsonMethod(self, a: float, b: float, n: int):
  51.         h = Integral.__height(a, b, n)
  52.         summ = 0
  53.         for i in range(1, n + 1):
  54.             summ += self.integral_function(a + (i - 1) * h) + 4 * self.integral_function(a + (i - 0.5) * h) + \
  55.                     self.integral_function(a + i * h)
  56.         return summ * h / 6
  57.  
  58.     @staticmethod
  59.     def __height(a: float, b: float, n: int): return (b-a)/n
  60.  
  61.  
  62. if __name__ == '__main__':
  63.     integral = Integral(lambda x: 0.25*x*(math.e**(x**2/2)))
  64.     print(json.dumps(integral.definite_integral_extended(0, 2), indent=4))
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement