Python-Math-Algorithms/linear_approximation/__init__.py

29 lines
1.1 KiB
Python

# Python module for linearly approximating the derivative of any function
# _ _ _ _
# __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
# \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
# \ V V /| | | | |_| || __/ | | | | |_) | |_| |
# \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
# |___/
# ____ __ __ _
# / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
# \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
# ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
# |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
# |___/
# Licensed under the GPLv2 License, Version 2.0 (the "License");
# Copyright (c) Sven Vogel
# linearly approximate a functions derivative in an interval
def linear_approximate(interval, function):
return (function(interval[1]) - function(interval[0])) / (interval[1] - interval[0])
# function to linearly approximate
def f(x):
return 3 + x * x - 5 * x
def test():
print("linear approximation: ", linear_approximate([2.0, 3.0], f))