Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- def f(x):
- if x == 0:
- return 0
- else:
- a = (math.pow(x, 2) + 1) * math.pow(math.sin(x), 3) / (x * math.log(1 + x))
- b = (1 + math.pow(x, 2)) * math.pow(math.sin(1/x), 3) / (math.pow(x, 3) * math.log(1 + 1/x))
- return a * math.exp(-math.pow(x, 4)) + b * math.exp(-math.pow(x, -4))
- def f1(x):
- if x == 0:
- return 0
- elif x == 1:
- return 0
- else:
- a = math.sin(3 * x) * math.atan(x) * math.pow(1 - x, 4) / (math.log(x) * (1 + math.pow(x, 4)))
- b = math.sin(3 / x) * math.atan(1 / x) * math.pow(x - 1, 4) / \
- (math.log(x) * (math.pow(x, 6) + math.pow(x, 2)))
- return a * math.exp(-math.pow(x, 2)) - b * math.exp(-math.pow(x, -2))
- def f2(x):
- if x == 0:
- return 9
- else:
- a = (math.pow(x, 2) + 1) * math.pow(math.sin(3 * x), 2) / (math.log(1 + x) * x)
- b = (math.pow(x, 2) + 1) * math.pow(math.sin(3 / x), 2) / (math.log(1 + 1 / x) * math.pow(x, 2))
- return a * math.exp(-math.pow(x, 2)) + b * math.exp(-math.pow(x, -2))
- def integrate(e, f):
- def kv_formula(a, b):
- if a > b:
- print("Wrong parameters")
- else:
- return (b - a) * (f(b) + f(a)) / 2
- res = [0, 0, 0]
- a, b = 0, 1
- h = 0.5
- # h = 0.0001
- while a + h <= b:
- while True:
- I1 = kv_formula(a, a + h)
- I11 = kv_formula(a, a + h/2)
- I21 = kv_formula(a + h/2, a + h)
- delta = (I11 + I21 - I1) / 3.0
- res[1] = res[1] + delta
- res[2] = res[2] + math.fabs(delta)
- if math.fabs(delta) > e:
- h = h / 2
- # print("h = ", h)
- else:
- break
- res[0] = res[0] + I1
- a = a + h
- # h = max(h, 0.00001)
- # if i % 1000 == 0:
- # print("next step = ", a)
- h = b - a
- res[0] = res[0] + kv_formula(a, a + h)
- return res
- if __name__ == '__main__':
- f_ = f1
- e = math.pow(10, -3)
- I = integrate(e, f_)[0]
- print("Integral = ", I)
- e = math.pow(10, -7)
- res1 = integrate(e, f_)
- print("Integral-7 = ", res1[0])
- e = math.pow(10, -9)
- res2 = integrate(e, f_)
- print("Integral-9 = ", res2[0])
- e = math.pow(10, -11)
- res3 = integrate(e, f_)
- print("Integral-11 = ", res3[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement