I am calculating the approximation of the function cos(x) + 1 using a Taylor Series Expansion up to order 7 using Tensorflow.
I wrote the following code:
x=tf.constant(3.14159,dtype=tf.float32)
result = tf.constant(2,dtype=tf.float32)
if i % 2 == 1:
num=tf.math.pow(x, i*2)
den=math.factorial(i*2)
result=tf.math.subtract(result,tf.math.divide(num,den))
else:
num=tf.math.pow(x, i*2)
den=math.factorial(i*2)
result=tf.math.add(result,tf.math.divide(num,den))
The Taylor Series of the function is
However, when x=3.14, I get conflicting results:
MY CODE ---> -0.21135163
Actual answer ---> 3.52077e-12
Can anyone help me point out where am I going wrong?
