-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrootApproximator.py
168 lines (127 loc) · 4.87 KB
/
rootApproximator.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from math import cos, sin
def separateRoots(func, a, b, n):
step_counter = 0
step = (b - a) / n
foundIntervals = list()
foundRoots = list()
start = a
end = start + step
while end <= b:
start_value = func(start)
end_value = func(end)
if start_value == 0:
foundRoots.append(start_value)
elif start_value * end_value < 0:
step_counter += 1
foundIntervals.append((start, end))
else:
pass
start = end
end = end + step
return foundIntervals, foundRoots
def printApproximationResults(name, initial_approx, step_counter, approx, last_segment_length, residual):
print(f'Method name: {name}\n'
f'Initial approximation: {initial_approx}\n'
f'Step count: {step_counter}\n'
f'Approximation by method: {approx}\n'
f'Last difference: {last_segment_length}\n'
f'|f(x)-0|: {residual}')
def getStartingValue(a, b, func, second_derivative):
step = (b - a) / 1000
x = a
while True:
if func(x) * second_derivative(x) > 0:
break
x += step
if x > b:
raise Exception('No convergence')
return x
def bisectionMethod(func, a, b, eps):
step_counter = 0
local_a = a
local_b = b
while (local_b - local_a) > 2 * eps:
step_counter += 1
c = (local_a + local_b) / 2
if func(c) * func(local_a) <= 0:
local_b = c
else:
local_a = c
approx = (local_a + local_b) / 2
initial_approx = (a + b) / 2
residual = abs(func(approx))
printApproximationResults("Bisection method", initial_approx, step_counter, approx, local_b - local_a, residual)
def newtonMethod(func, derivative, second_derivative, a, b, eps):
step_counter = 0
initial_approx = x_prev = getStartingValue(a, b, func, second_derivative)
while True:
step_counter += 1
x_curr = x_prev - func(x_prev) / derivative(x_prev)
if abs(x_curr - x_prev) <= eps:
break
x_prev = x_curr
last_segment_length = x_prev - x_curr
approx = x_curr
residual = abs(func(approx))
printApproximationResults('Newton method', initial_approx, step_counter, approx, last_segment_length, residual)
def modifiedNewtonMethod(func, derivative, second_derivative, a, b, eps):
step_counter = 0
initial_approx = x_prev = x0 = getStartingValue(a, b, func, second_derivative)
while True:
step_counter += 1
x_curr = x_prev - func(x_prev) / derivative(x0)
if abs(x_curr - x_prev) <= eps:
break
x_prev = x_curr
last_segment_length = x_prev - x_curr
approx = x_curr
residual = abs(func(approx))
printApproximationResults('Modified Newton method', initial_approx, step_counter, approx, last_segment_length,
residual)
def secantMethod(func, a, b, eps):
step_counter = 0
x_prev = a
x_curr = b
while True:
step_counter += 1
x_next = x_curr - func(x_curr) * (x_curr - x_prev) / (func(x_curr) - func(x_prev))
if abs(x_next - x_curr) <= eps:
break
x_prev = x_curr
x_curr = x_next
last_segment_length = x_next - x_curr
approx = x_next
initial_approx = (a + b) / 2
residual = abs(func(approx))
printApproximationResults('Secant method', initial_approx, step_counter, approx, last_segment_length, residual)
if __name__ == "__main__":
print('Численные методы решения нелинейных уравнений')
print('f(x) = 8cos(x) - x - 6')
print('[A ; B] = [-9 ; 1]')
print('eps = 10^(-7)\n')
function = lambda x: 8 * cos(x) - x - 6
first_deriv = lambda x: -8 * sin(x) - 1
second_deriv = lambda x: -8 * cos(x)
a = float(input("Input A: "))
b = float(input("Input B: "))
n = int(input("Input N: "))
eps = float(input("Input epsilon: "))
print('\n---Root separation---')
foundIntervals, foundRoots = separateRoots(function, a, b, n)
print(f"\nRoots found during root separation: {len(foundRoots)}\n"
f"List of roots: {foundRoots}\n")
print(f"Sign changing intervals found during root separation: {len(foundIntervals)}\n"
f"List of intervals:")
for interval in foundIntervals:
print(f" [{interval[0]}, {interval[1]}]")
print('\n---Clarification of roots---\n')
for interval in foundIntervals:
print(f"Current interval: [{interval[0]}, {interval[1]}]\n")
bisectionMethod(function, interval[0], interval[1], eps)
print('\n')
newtonMethod(function, first_deriv, second_deriv, interval[0], interval[1], eps)
print('\n')
modifiedNewtonMethod(function, first_deriv, second_deriv, interval[0], interval[1], eps)
print('\n')
secantMethod(function, interval[0], interval[1], eps)
print('\n')