added taylor series sine approximation
This commit is contained in:
parent
687ec9b6b1
commit
5d1c02080f
3
main.py
3
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()
|
||||
|
|
|
@ -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)))
|
Loading…
Reference in New Issue