Skip to content

Commit ed335f9

Browse files
committed
Added 'Muller' method
1 parent 076e93d commit ed335f9

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ python3 main.py
4343

4444
- Bisection method
4545
- Secant method
46-
- Regula Falsi (False Position)
47-
- Pegasus
46+
- Regula Falsi method (False Position)
47+
- Pegasus method
48+
- Muller method
4849
- Newton method
4950

5051
### Interpolation

main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,32 @@ def f(x):
154154
print(f"\tconverged = {converged}")
155155

156156

157+
@print_docstring
158+
def run_example_muller():
159+
"""Run an example 'Solutions: Muller'."""
160+
161+
def f(x):
162+
return 2 * x ** 3 - math.cos(x + 1) - 3
163+
164+
a = -1.0
165+
b = 2.0
166+
toler = 0.01
167+
iter_max = 100
168+
169+
print("Input:")
170+
print(f"\ta = {a}")
171+
print(f"\tb = {b}")
172+
print(f"\ttoler = {toler}")
173+
print(f"\titer_max = {iter_max}")
174+
175+
[root, i, converged] = solutions.muller(f, a, b, toler, iter_max)
176+
177+
print("Output:")
178+
print(f"\troot = {root:.5f}")
179+
print(f"\ti = {i}")
180+
print(f"\tconverged = {converged}")
181+
182+
157183
@print_docstring
158184
def run_example_newton():
159185
"""Run an example 'Solutions: Newton'."""
@@ -626,6 +652,7 @@ def main():
626652
run_example_secant()
627653
run_example_regula_falsi()
628654
run_example_pegasus()
655+
run_example_muller()
629656
run_example_newton()
630657
run_example_lagrange()
631658
run_example_neville()

solutions.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,69 @@ def pegasus(f, a, b, toler, iter_max):
202202
return [root, i, converged]
203203

204204

205+
def muller(f, a, c, toler, iter_max):
206+
"""Calculate the root of an equation by the Muller method.
207+
208+
Args:
209+
f: function f(x).
210+
a: lower limit.
211+
c: upper limit.
212+
toler: tolerance (stopping criterion).
213+
iter_max: maximum number of iterations (stopping criterion).
214+
215+
Returns:
216+
root: root value.
217+
iter: number of iterations used by the method.
218+
converged: flag to indicate if the root was found.
219+
"""
220+
b = (a + c) / 2
221+
fa = f(a)
222+
fb = f(b)
223+
fc = f(c)
224+
x = b
225+
fx = fb
226+
delta_x = c - a
227+
228+
converged = False
229+
for i in range(0, iter_max + 1):
230+
h1 = c - b
231+
h2 = b - a
232+
r = h1 / h2
233+
t = x
234+
235+
aa = (fc - (r + 1) * fb + r * fa) / (h1 * (h1 + h2))
236+
bb = (fc - fb) / h1 - aa * h1
237+
cc = fb
238+
239+
signal_bb = int(math.copysign(1, bb))
240+
241+
z = (-bb + signal_bb * math.sqrt(bb ** 2 - 4 * aa * cc)) / (2 * aa)
242+
x = b + z
243+
244+
delta_x = x - t
245+
fx = f(x)
246+
247+
print(f"i = {i:03d},\tx = {x:+.4f},\t", end="")
248+
print(f"fx = {fx:+.4f},\tdx = {delta_x:+.4f}")
249+
250+
if math.fabs(delta_x) <= toler and math.fabs(fx) <= toler:
251+
converged = True
252+
break
253+
254+
if x > b:
255+
a = b
256+
fa = fb
257+
else:
258+
c = b
259+
fc = fb
260+
261+
b = x
262+
fb = fx
263+
264+
root = x
265+
return [root, i, converged]
266+
267+
205268
def newton(f, df, x0, toler, iter_max):
206269
"""Calculate the root of an equation by the Newton method.
207270

0 commit comments

Comments
 (0)