-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab1.py
64 lines (55 loc) · 1.61 KB
/
lab1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def pol_val(pol, val):
res = 0
for i in range(len(pol)):
res += pol[i] * (val ** i)
return res
def bisection_meth(a, b, pol, eps):
i = 0
while abs(a - b) > eps:
c = (a + b)/2
if pol_val(pol, a) * pol_val(pol, c) <= 0:
b = c
elif pol_val(pol, b) * pol_val(pol, c) <= 0:
a = c
print(i,' ', c)
i += 1
print((a+b)/2)
def xord_meth(a, b, pol, eps):
c = (a * pol_val(pol, b) - b * pol_val(pol, a)) / (pol_val(pol, b) - pol_val(pol, a))
c_pr = 0
i = 0
while abs(c - c_pr) > eps or pol_val(pol, c) > eps:
if pol_val(pol, a) * pol_val(pol, c) <= 0:
b = c
elif pol_val(pol, b) * pol_val(pol, c) <= 0:
a = c
c_pr = c
print(i,' ', c)
i += 1
c = (a * pol_val(pol, b) - b * pol_val(pol, a)) / (pol_val(pol, b) - pol_val(pol, a))
print(c)
def derivative(pol):
pol1 = []
for i in range(len(pol)):
pol1.append(pol[i] * i)
pol1.pop(0)
return pol1
def newton_meth(x0, pol, eps):
i = 0
pol1 = derivative(pol)
x1 = x0 - pol_val(pol, x0) / pol_val(pol1, x0)
while abs(x1 - x0) > eps or abs(pol_val(pol, x1)) > eps:
i += 1
x0 = x1
print(i,' ', x1)
x1 = x0 - pol_val(pol, x0) / pol_val(pol1, x0)
print(x1)
if __name__ == "__main__":
polyn = [1, -3, 1, -2, -4]
polyn = polyn[::-1]
eps = 0.00001
#bisection_meth(-1, -0.25, polyn, eps)
bisection_meth(2.75, 3.5, polyn, eps)
#xord_meth(-1, -0.25, polyn, eps)
#xord_meth(2.75, 3.5, polyn, eps)
#newton_meth(2.75, polyn, eps)