34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
# 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))
|