So, I'm trying to find the value of cos(x)where x=1.2. I feel that the script I wrote should be fine, however the value I selected is incorrect. I.e; cos(1.2)=0.6988057880877979To 25terms when I have to go out: cos(1.2)=0.36235775.
I created a similar calculation program sin(1.2)that works great.
Calculation sin(1.2):
import math as m
x=1.2
k=1
N=25
s=x
sign=1.0
while k<N:
sign=-sign
k=k+2
term=sign*x**k/m.factorial(k)
s=s+term
print('sin(%g) = %g (approximation with %d terms)' % (x,s,N))
Now let's try to calculate cos(1.2):
import math as m
x=1.2
k=1
N=25
s=x
sign=1.0
while k<N:
sign=-sign
k=k+1
term=sign*x**k/m.factorial(k)
s=s+term
print(s)
source
share