# 1lab

Oct 24th, 2019
81
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def function(x):
2.     return 0.93*x**5 - 0.71*x**4 - 0.53*x**3 + 2.1*x**2 - 10.5
3.
4.
5. def dichotomy(a, b, count):
6.     c = (a + b)/2
7.     if count == 0 or abs(function(c)) < 0.0001:
8.         return c
9.     else:
10.         if function(a)*function(c) < 0:
11.             return dichotomy(a, c, count - 1)
12.         if function(a)*function(c) > 0:
13.             return dichotomy(c, b, count - 1)
14.
15.
16. start = 0
17. end = 10
18. n = 20
19. key = 1
20. while key:                              #interval check
21.     sign = function(start) * function(end)
22.     if sign > 0:
23.         print("May be you haven't root. Please change 'start' and 'end'")
24.         start = int(input())
25.         end = int(input())
26.     sign = function(start) * function(end)
27.     if sign < 0:
28.             key = 0
29. X = dichotomy(start, end, n)
30. print('root = ', X)
31. print('function = ', function(X))
RAW Paste Data