From b892d2386c3d49ff7c0252c29b69c450a39887b7 Mon Sep 17 00:00:00 2001 From: emil Date: Sat, 7 Dec 2024 03:04:25 +0300 Subject: [PATCH] Golden section method --- main.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/main.py b/main.py index ce767f5..5b87200 100644 --- a/main.py +++ b/main.py @@ -20,3 +20,28 @@ def bisection_method(a, b, eps): else: a = c fa = fc + +def f_golden(x): + return (x - 2)**2 + 3 + +def golden_section_search(a, b, eps): + phi = (1 + math.sqrt(5)) / 2.0 + resphi = 2 - phi + c = a + resphi * (b - a) + d = b - resphi * (b - a) + fc = f_golden(c) + fd = f_golden(d) + + while (b - a) > eps: + if fc < fd: + b = d + d = c + fd = fc + c = a + resphi * (b - a) + fc = f_golden(c) + else: + a = c + c = d + fc = fd + d = b - resphi * (b - a) + fd = f_golden(d)