-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.py
More file actions
90 lines (73 loc) · 2.15 KB
/
task1.py
File metadata and controls
90 lines (73 loc) · 2.15 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import math
def ctg(x):
return 1/math.tan(x)
def f(x):
return ctg(1.05*x) - x*x
def df(x):
return -2*x - 1.05 / math.sin(1.05 * x) ** 2
def secant(f, a, b):
return (a * f(b) - b * f(a)) / (f(b) - f(a))
def newton(f, dumb, x):
return x - f(x)/df(x)
def combined(f, a, b):
x = newton(f, a, b)
return secant(f, x, b)
def solve(func, epsilon, a = 0, b = 0, iteration = 0, method = newton):
x = method(func, a, b)
while abs(x-b) > epsilon:
b = x
iteration += 1
x = method(func, a, b)
return x, iteration
print("secant")
print(solve(func = f, epsilon = 0.0000000001, a = -2.95, b = -2.8, method = secant))
print("newton")
print(solve(func = f, epsilon = 0.0000000001, b = -2.8, method = newton))
print("combined")
print(solve(func = f, epsilon = 0.0000000001, a = -2.95, b = -2.8, method = combined))
def f1(x):
return math.sin(x + 0.5) - 1
def f2(y):
return -math.cos(y - 2)
def solveSystem(f1, f2, x0, y0, epsilon, iteration = 0):
yi = f1(x0)
xi = f2(y0)
while abs(xi - x0) > epsilon and abs(yi - y0) > epsilon:
iteration += 1
x0 = xi
y0 = yi
yi = f1(x0)
xi = f2(y0)
return xi, yi, iteration
print("system")
print(solveSystem(f1, f2, 0, 0, 0.00000000001 ))
def f1(x,y):
return math.sin(x + 0.5) - y - 1
def df1x(x,y):
return math.cos(x + 0.5)
def df1y(x,y):
return -1
def f2(x,y):
return math.cos(y-2) + x
def df2x(x,y):
return 1
def df2y(x,y):
return -math.sin(y-2)
def delta(x,y):
return (df1x(x,y) * df2y(x,y) - df1y(x,y) * df2x(x,y))
def delta_x(x,y):
return -(f1(x,y)*df2y(x,y) - df1y(x,y)*f2(x,y))
def delta_y(x,y):
return (f1(x,y)*df2x(x,y) - df1x(x,y)*f2(x,y))
def newton_solve(f1, f2, x0, y0, epsilon, iteration = 0):
x = x0 + delta_x(x0, y0) / delta(x0,y0)
y = y0 + delta_y(x0, y0) / delta(x0, y0)
while abs(x-x0) > epsilon and abs(y-y0) > epsilon:
iteration += 1
x0 = x
y0 = y
x = x0 + delta_x(x0,y0) / delta(x0,y0)
y = y0 + delta_y(x0, y0) / delta(x0, y0)
return x, y, iteration
print("newton system")
print(newton_solve(f1, f2, 0.5, 0.1, 0.00000001))