From 5d1c02080f7f5f91ed7c5df6869de610e3a15fe6 Mon Sep 17 00:00:00 2001 From: teridax Date: Mon, 22 May 2023 17:56:31 +0200 Subject: [PATCH] added taylor series sine approximation --- main.py | 3 ++- taylor_sine_approx/__init__.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 taylor_sine_approx/__init__.py diff --git a/main.py b/main.py index 0a457cb..ac7bbba 100644 --- a/main.py +++ b/main.py @@ -19,7 +19,7 @@ import linear_approximation import matrix import newton_polynom import zero_point_approximation - +import taylor_sine_approx if __name__ == '__main__': linear_approximation.test() @@ -27,3 +27,4 @@ if __name__ == '__main__': newton_polynom.test() zero_point_approximation.test() matrix.test() + taylor_sine_approx.test() diff --git a/taylor_sine_approx/__init__.py b/taylor_sine_approx/__init__.py new file mode 100644 index 0000000..e0746fa --- /dev/null +++ b/taylor_sine_approx/__init__.py @@ -0,0 +1,24 @@ +import math + + +# iterative factorial function +def fac(x): + factor = 1.0 + for i in range(1, x + 1): + factor *= i + return factor + + +# approximation of the sine function using taylor series +def taylor_sine(n, x): + k = 0.0 + sign = -1.0 + for i in range(0, n): + sign *= -1.0 + k += sign / fac(2 * i + 1) * math.pow(x, 2 * i + 1) + + return k + + +def test(): + print("taylor: %s, sine: %s" % (taylor_sine(16, math.pi), math.sin(math.pi)))