Skip to content

Commit 076e93d

Browse files
committed
Added 'Pegasus' method
1 parent c5547c9 commit 076e93d

3 files changed

Lines changed: 157 additions & 83 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ python3 main.py
4242
### Solutions of equations
4343

4444
- Bisection method
45-
- Newton method
46-
- Regula Falsi (False Position)
4745
- Secant method
46+
- Regula Falsi (False Position)
47+
- Pegasus
48+
- Newton method
4849

4950
### Interpolation
5051

main.py

Lines changed: 79 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ def run_example_bisection():
4545
# The interval reported must have a signal exchange, f (a) * f (b)<0.
4646

4747
def f(x):
48-
return 4 * x ** 3 + x + math.cos(x) - 10
48+
return 2 * x ** 3 - math.cos(x + 1) - 3
4949

50-
a = 1.0
50+
a = -1.0
5151
b = 2.0
52-
toler = 10 ** -5
52+
toler = 0.01
5353
iter_max = 100
5454

5555
print("Input:")
@@ -61,42 +61,43 @@ def f(x):
6161
[root, i, converged] = solutions.bisection(f, a, b, toler, iter_max)
6262

6363
print("Output:")
64-
print(f"\troot = {root:.4f}")
64+
print(f"\troot = {root:.5f}")
6565
print(f"\ti = {i}")
6666
print(f"\tconverged = {converged}")
6767

6868

6969
@print_docstring
70-
def run_example_newton():
71-
"""Run an example 'Solutions: Newton'."""
72-
# Newton method (find roots of an equation)
70+
def run_example_secant():
71+
"""Run an example 'Solutions: Secant'."""
72+
# Secant method (find roots of an equation)
7373
# Pros:
74-
# It is a fast method.
75-
# Cons:
76-
# It may diverge;
77-
# It is necessary to calculate the derivative of the function;
78-
# It is necessary to give an initial x0 value where
79-
# f'(x0) must be nonzero.
74+
# It is a fast method (slower than Newton's method);
75+
# It is based on the Newton method but does not need the derivative
76+
# of the function.
77+
# Cons:
78+
# It may diverge if the function is not approximately linear in the
79+
# range containing the root;
80+
# It is necessary to give two points, 'a' and 'b' where
81+
# f(a)-f(b) must be nonzero.
8082

8183
def f(x):
82-
return 4 * x ** 3 + x + math.cos(x) - 10
84+
return 2 * x ** 3 - math.cos(x + 1) - 3
8385

84-
def df(x):
85-
return 12 * x ** 2 + 1 - math.sin(x)
86-
87-
x0 = 1.0
88-
toler = 10 ** -5
86+
a = -1.0
87+
b = 2.0
88+
toler = 0.01
8989
iter_max = 100
9090

9191
print("Input:")
92-
print(f"\tx0 = {x0}")
92+
print(f"\ta = {a}")
93+
print(f"\tb = {b}")
9394
print(f"\ttoler = {toler}")
9495
print(f"\titer_max = {iter_max}")
9596

96-
[root, i, converged] = solutions.newton(f, df, x0, toler, iter_max)
97+
[root, i, converged] = solutions.secant(f, a, b, toler, iter_max)
9798

9899
print("Output:")
99-
print(f"\troot = {root:.4f}")
100+
print(f"\troot = {root:.5f}")
100101
print(f"\ti = {i}")
101102
print(f"\tconverged = {converged}")
102103

@@ -106,11 +107,11 @@ def run_example_regula_falsi():
106107
"""Run an example 'Solutions: Regula Falsi'."""
107108

108109
def f(x):
109-
return 4 * x ** 3 + x + math.cos(x) - 10
110+
return 2 * x ** 3 - math.cos(x + 1) - 3
110111

111-
a = 1.0
112+
a = -1.0
112113
b = 2.0
113-
toler = 10 ** -5
114+
toler = 0.01
114115
iter_max = 100
115116

116117
print("Input:")
@@ -122,31 +123,21 @@ def f(x):
122123
[root, i, converged] = solutions.regula_falsi(f, a, b, toler, iter_max)
123124

124125
print("Output:")
125-
print(f"\troot = {root:.4f}")
126+
print(f"\troot = {root:.5f}")
126127
print(f"\ti = {i}")
127128
print(f"\tconverged = {converged}")
128129

129130

130131
@print_docstring
131-
def run_example_secant():
132-
"""Run an example 'Solutions: Secant'."""
133-
# Secant method (find roots of an equation)
134-
# Pros:
135-
# It is a fast method (slower than Newton's method);
136-
# It is based on the Newton method but does not need the derivative
137-
# of the function.
138-
# Cons:
139-
# It may diverge if the function is not approximately linear in the
140-
# range containing the root;
141-
# It is necessary to give two points, 'a' and 'b' where
142-
# f(a)-f(b) must be nonzero.
132+
def run_example_pegasus():
133+
"""Run an example 'Solutions: Pegasus'."""
143134

144135
def f(x):
145-
return 4 * x ** 3 + x + math.cos(x) - 10
136+
return 2 * x ** 3 - math.cos(x + 1) - 3
146137

147-
a = 1.0
138+
a = -1.0
148139
b = 2.0
149-
toler = 10 ** -5
140+
toler = 0.01
150141
iter_max = 100
151142

152143
print("Input:")
@@ -155,10 +146,45 @@ def f(x):
155146
print(f"\ttoler = {toler}")
156147
print(f"\titer_max = {iter_max}")
157148

158-
[root, i, converged] = solutions.secant(f, a, b, toler, iter_max)
149+
[root, i, converged] = solutions.pegasus(f, a, b, toler, iter_max)
150+
151+
print("Output:")
152+
print(f"\troot = {root:.5f}")
153+
print(f"\ti = {i}")
154+
print(f"\tconverged = {converged}")
155+
156+
157+
@print_docstring
158+
def run_example_newton():
159+
"""Run an example 'Solutions: Newton'."""
160+
# Newton method (find roots of an equation)
161+
# Pros:
162+
# It is a fast method.
163+
# Cons:
164+
# It may diverge;
165+
# It is necessary to calculate the derivative of the function;
166+
# It is necessary to give an initial x0 value where
167+
# f'(x0) must be nonzero.
168+
169+
def f(x):
170+
return 2 * x ** 3 - math.cos(x + 1) - 3
171+
172+
def df(x):
173+
return 12 * x ** 2 + 1 - math.sin(x)
174+
175+
x0 = 1.0
176+
toler = 10 ** -5
177+
iter_max = 100
178+
179+
print("Input:")
180+
print(f"\tx0 = {x0}")
181+
print(f"\ttoler = {toler}")
182+
print(f"\titer_max = {iter_max}")
183+
184+
[root, i, converged] = solutions.newton(f, df, x0, toler, iter_max)
159185

160186
print("Output:")
161-
print(f"\troot = {root:.4f}")
187+
print(f"\troot = {root:.5f}")
162188
print(f"\ti = {i}")
163189
print(f"\tconverged = {converged}")
164190

@@ -178,7 +204,7 @@ def run_example_lagrange():
178204
[y_int] = interpolation.lagrange(x, y, x_int)
179205

180206
print("Output:")
181-
print(f"\ty_int = {y_int:.4f}")
207+
print(f"\ty_int = {y_int:.5f}")
182208

183209

184210
@print_docstring
@@ -196,7 +222,7 @@ def run_example_neville():
196222
[y_int, q] = interpolation.neville(x, y, x_int)
197223

198224
print("Output:")
199-
print(f"\ty_int = {y_int:.4f}")
225+
print(f"\ty_int = {y_int:.5f}")
200226
print(f"\tq =\n{q}")
201227

202228

@@ -208,7 +234,7 @@ def run_example_briot_ruffini():
208234

209235
print("Input:")
210236
print(f"\ta = {a}")
211-
print(f"\troot = {root:.4f}")
237+
print(f"\troot = {root:.5f}")
212238

213239
[b, rest] = polynomials.briot_ruffini(a, root)
214240

@@ -296,7 +322,7 @@ def run_example_composite_trapezoidal_array():
296322
[xi] = integration.composite_trapezoidal_array(x, y)
297323

298324
print("Output:")
299-
print(f"\txi = {xi:.4f}")
325+
print(f"\txi = {xi:.5f}")
300326

301327

302328
@print_docstring
@@ -318,7 +344,7 @@ def f(x):
318344
[xi] = integration.composite_trapezoidal(f, b, a, n)
319345

320346
print("Output:")
321-
print(f"\txi = {xi:.4f}")
347+
print(f"\txi = {xi:.5f}")
322348

323349

324350
@print_docstring
@@ -335,7 +361,7 @@ def run_example_composite_simpson_array():
335361
[xi] = integration.composite_simpson_array(x, y)
336362

337363
print("Output:")
338-
print(f"\txi = {xi:.4f}")
364+
print(f"\txi = {xi:.5f}")
339365

340366

341367
@print_docstring
@@ -357,7 +383,7 @@ def f(x):
357383
[xi] = integration.composite_simpson(f, b, a, n)
358384

359385
print("Output:")
360-
print(f"\txi = {xi:.4f}")
386+
print(f"\txi = {xi:.5f}")
361387

362388

363389
@print_docstring
@@ -597,9 +623,10 @@ def run_example_gauss_seidel():
597623
def main():
598624
"""Run all examples."""
599625
run_example_bisection()
600-
run_example_newton()
601-
run_example_regula_falsi()
602626
run_example_secant()
627+
run_example_regula_falsi()
628+
run_example_pegasus()
629+
run_example_newton()
603630
run_example_lagrange()
604631
run_example_neville()
605632
run_example_briot_ruffini()

0 commit comments

Comments
 (0)