Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. import math
  2.  
  3. def f(x):
  4.     if x == 0:
  5.         return 0
  6.     else:
  7.         a = (math.pow(x, 2) + 1) * math.pow(math.sin(x), 3) / (x * math.log(1 + x))
  8.         b = (1 + math.pow(x, 2)) * math.pow(math.sin(1/x), 3) / (math.pow(x, 3) * math.log(1 + 1/x))
  9.         return a * math.exp(-math.pow(x, 4)) + b * math.exp(-math.pow(x, -4))
  10.  
  11. def f1(x):
  12.     if x == 0:
  13.         return 0
  14.     elif x == 1:
  15.         return 0
  16.     else:
  17.         a = math.sin(3 * x) * math.atan(x) * math.pow(1 - x, 4) / (math.log(x) * (1 + math.pow(x, 4)))
  18.         b = math.sin(3 / x) * math.atan(1 / x) * math.pow(x - 1, 4) / \
  19.             (math.log(x) * (math.pow(x, 6) + math.pow(x, 2)))
  20.         return a * math.exp(-math.pow(x, 2)) - b * math.exp(-math.pow(x, -2))
  21.  
  22. def f2(x):
  23.     if x == 0:
  24.         return 9
  25.     else:
  26.         a = (math.pow(x, 2) + 1) * math.pow(math.sin(3 * x), 2) / (math.log(1 + x) * x)
  27.         b = (math.pow(x, 2) + 1) * math.pow(math.sin(3 / x), 2) / (math.log(1 + 1 / x) * math.pow(x, 2))
  28.         return a * math.exp(-math.pow(x, 2)) + b * math.exp(-math.pow(x, -2))
  29.  
  30.  
  31. def integrate(e, f):
  32.     def kv_formula(a, b):
  33.         if a > b:
  34.             print("Wrong parameters")
  35.         else:
  36.             return (b - a) * (f(b) + f(a)) / 2
  37.     res = [0, 0, 0]
  38.     a, b = 0, 1
  39.     h = 0.5
  40. #    h = 0.0001
  41.     while a + h <= b:
  42.         while True:
  43.             I1 = kv_formula(a, a + h)
  44.             I11 = kv_formula(a, a + h/2)
  45.             I21 = kv_formula(a + h/2, a + h)
  46.             delta = (I11 + I21 - I1) / 3.0
  47.             res[1] = res[1] + delta
  48.             res[2] = res[2] + math.fabs(delta)
  49.             if math.fabs(delta) > e:
  50.                 h = h / 2
  51. #                print("h = ", h)
  52.             else:
  53.                 break
  54.  
  55.         res[0] = res[0] + I1
  56.         a = a + h
  57. #       h = max(h, 0.00001)
  58. #        if i % 1000 == 0:
  59. #            print("next step = ", a)
  60.     h = b - a
  61.     res[0] = res[0] + kv_formula(a, a + h)
  62.     return res
  63.  
  64.  
  65.  
  66. if __name__ == '__main__':
  67.     f_ = f1
  68.     e = math.pow(10, -3)
  69.     I = integrate(e, f_)[0]
  70.     print("Integral = ", I)
  71.  
  72.     e = math.pow(10, -7)
  73.     res1 = integrate(e, f_)
  74.     print("Integral-7 = ", res1[0])
  75.     e = math.pow(10, -9)
  76.     res2 = integrate(e, f_)
  77.     print("Integral-9 = ", res2[0])
  78.     e = math.pow(10, -11)
  79.     res3 = integrate(e, f_)
  80.     print("Integral-11 = ", res3[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement