added taylor series sine approximation

This commit is contained in:
Sven Vogel 2023-05-22 17:56:31 +02:00
parent 687ec9b6b1
commit 5d1c02080f
2 changed files with 26 additions and 1 deletions

View File

@ -19,7 +19,7 @@ import linear_approximation
import matrix import matrix
import newton_polynom import newton_polynom
import zero_point_approximation import zero_point_approximation
import taylor_sine_approx
if __name__ == '__main__': if __name__ == '__main__':
linear_approximation.test() linear_approximation.test()
@ -27,3 +27,4 @@ if __name__ == '__main__':
newton_polynom.test() newton_polynom.test()
zero_point_approximation.test() zero_point_approximation.test()
matrix.test() matrix.test()
taylor_sine_approx.test()

View File

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