Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math as m
- import random
- n = 1000
- # eto pobeda
- def func(x):
- return x / (6*x - x**2)**0.5
- def derivative(x):
- return (6*x - x**2)**0.5 / (6 - x)
- def antider(x):
- return -(6*x - x**2)**0.5 + 3*m.asin((x-3)/3)
- # из варианта
- def task1_2():
- a = 3.1
- b = 3.8
- d = (b - a) / n
- x = a
- sl = 0
- while x < b - d / 2:
- sl += func(x) * d
- x += d
- x = a + d
- sr = 0
- while x < b + d / 2:
- sr += func(x) * d
- x += d
- f = antider(b) - antider(a)
- print("Точное значение:\t\t", f)
- print("Левые прямоугольники:\t\t", sl, "Погрешность: ", abs(1 - sl / f))
- print("Правые прямоугольники:\t\t", sr, "Погрешность: ", abs(1 - sr / f))
- # task1_2()
- def task3():
- a = 3.1
- b = 3.8
- d = (b - a) / n
- x = a
- s = 0
- while x < b - d / 2:
- s += func((x + (x + d)) / 2) * d
- x += d
- f = antider(b) - antider(a)
- print("Точное значение:\t\t", f)
- print("Метод средних:\t\t", s, "\tПогрешность: ", abs(1 - s / f))
- # task3()
- def task4():
- a = 3.1
- b = 3.8
- d = (b - a) / n
- x = a
- s = 0
- while x < b - d / 2:
- s += d * (func(x) + func(x + d)) / 2
- x += d
- f = antider(b) - antider(a)
- print("Точное значение:\t", f)
- print("Метод трапеций:\t\t", s, "\tПогрешность: ", abs(1 - s / f))
- # task4()
- def task5():
- a = 3.1
- b = 3.8
- d = (b - a) / n
- x = a
- s = 0
- while x < b - d / 2:
- s += d * (func(x) + func(x + d)) / 2
- x += d
- s += (d ** 2) * (derivative(a) - derivative(b)) / 12
- f = antider(b) - antider(a)
- print("Точное значение:\t", f)
- print("Метод Эйлера:\t", s, "\tПогрешность: ", abs(1 - s / f))
- task5()
- def task6():
- a = 3.1
- b = 3.8
- d = (b - a) / n / 2
- x = a
- s = 0
- while x < b - d:
- s += (d / 3) * (func(x) + 4 * func(x + d) + func(x + 2 * d))
- x += 2 * d
- f = antider(b) - antider(a)
- print("Точное значение:\t", f)
- print("Метод Симпсона:\t", s, "\tПогрешность: ", abs(1 - s / f))
- # task6()
- def task7():
- a = 3.1
- b = 3.8
- h = (b - a) / n
- s = 0
- for i in range(n):
- s += func(a + (b - a) * random.random()) * h
- f = antider(b) - antider(a)
- print("Точное значение:\t", f)
- print("Метод Монте-Карло:\t", s, "\tПогрешность: ", abs(1 - s / f))
- # task7()
- def task8():
- a = 3.1
- b = 3.8
- d = (b - a) / n
- H = [[1 / 2, 1 / 2],
- [1 / 6, 2 / 3, 1 / 6],
- [1 / 8, 3 / 8, 3 / 8, 1 / 8],
- [7 / 90, 16 / 45, 2 / 15, 16 / 45, 7 / 90],
- [19 / 288, 25 / 96, 25 / 144, 25 / 144, 25 / 96, 19 / 288]]
- f = antider(b) - antider(a)
- print("Точное значение:\t", f)
- print("Метод Ньютона-Котеса")
- for k in range(2, 6):
- s = 0
- x = a
- while x < b - d / 2:
- for i in range(1, k + 2):
- s += func(x + (i - 1) * d / k) * d * H[k - 1][i - 1]
- x += d
- print(k, "порядка:\t", s, " Погрешность: ", abs(1 - s / f))
- # task8()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement