added zero point approximation

This commit is contained in:
Sven Vogel 2023-04-25 19:18:06 +02:00
parent 201931050a
commit 2de8ca6432
3 changed files with 49 additions and 8 deletions

10
main.py
View File

@ -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()

View File

@ -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))

View File

@ -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))