From 2de8ca64323af3577f10daee745ba763740e46d7 Mon Sep 17 00:00:00 2001 From: teridax Date: Tue, 25 Apr 2023 19:18:06 +0200 Subject: [PATCH] added zero point approximation --- main.py | 10 ++++++--- newton_polynom/__init__.py | 14 +++++++----- zero_point_approximation/__init__.py | 33 ++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 zero_point_approximation/__init__.py diff --git a/main.py b/main.py index fc9947e..e6d3e2a 100644 --- a/main.py +++ b/main.py @@ -17,7 +17,11 @@ import fixpoint_approximation import linear_approximation import newton_polynom +import zero_point_approximation -# linear_approximation.test() -# fixpoint_approximation.test() -newton_polynom.test() + +if __name__ == '__main__': + linear_approximation.test() + fixpoint_approximation.test() + newton_polynom.test() + zero_point_approximation.test() diff --git a/newton_polynom/__init__.py b/newton_polynom/__init__.py index 6620b2b..936fc20 100644 --- a/newton_polynom/__init__.py +++ b/newton_polynom/__init__.py @@ -28,18 +28,22 @@ def combine_n(points): # returns a polynom that will intersect all supplied points as long as # the number of points is bigger than 1 def newton_polynom(points): + polynom = "" for x in range(1, len(points)): if x == 1: - print(points[0][1], end=' + ') + polynom += '{r0:.2f} + '.format(r0=points[0][1]) else: - print(end=' + ') + polynom += ' + ' - print(combine_n(points[0:(x + 1)]), end='') + polynom += "{r:.2f}".format(r=combine_n(points[0:(x + 1)])) for y in range(x): - print(" * (x - {value:.2f})".format(value=points[y][0]), end='') + polynom += " * (x - {value:.2f})".format(value=points[y][0]) + + return polynom def test(): - newton_polynom([[-1, 5], [2, -1], [3, -1], [4, 5], [8, 9]]) + points = [[-1, 5], [2, -1], [3, -1], [4, 5], [8, 9]] + print("newton polynom: ", newton_polynom(points)) diff --git a/zero_point_approximation/__init__.py b/zero_point_approximation/__init__.py new file mode 100644 index 0000000..308c996 --- /dev/null +++ b/zero_point_approximation/__init__.py @@ -0,0 +1,33 @@ +# Python module for approximating a zero point of any given function +# _ _ _ _ +# __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ +# \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | +# \ V V /| | | | |_| || __/ | | | | |_) | |_| | +# \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | +# |___/ +# ____ __ __ _ +# / ___|_ _____ _ __ \ \ / /__ __ _ ___| | +# \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | +# ___) \ V / __/ | | | \ V / (_) | (_| | __/ | +# |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| +# |___/ +# Licensed under the GPLv2 License, Version 2.0 (the "License"); +# Copyright (c) Sven Vogel + +def zero_point_approximation(interval, function, iterations): + for _ in range(iterations): + median = interval[1] - (interval[1] - interval[0]) / 2 + if function(median) > 0: + interval[1] = median + else: + interval[0] = median + + return interval + + +def f(x): + return 4 * x * x * x - 7 * x * x + 5 * x - 12 + + +def test(): + print("Zero-point approximation: ", zero_point_approximation(interval=[-9, 9], function=f, iterations=15))