[Tutor] Help with program
Courtney Skinner
courtneyelizabeth983 at gmail.com
Mon Feb 16 17:27:04 CET 2015
Hello,
I am trying to build a program that approximates the value of cosine - this is my program so far. It is not returning the right values. Could you tell me what I am doing wrong?
def main():
import math
print("This program approximates the cosine of x by summing")
print("the terms of this series: x^0 / 0!, -x^2 / 2!,")
print("x^4 / 4!, -x^6 / 6!...")
n = eval(input("How many terms should be used? "))
x = eval(input("What value should be used for x? "))
s = 1
d = 1
e = 1
value = 0
for i in range(n - 1):
value = value + s + (x**e / math.factorial(d))
s = s * 1
e = e + 2
d + d + 2
print("Approximation for cos(x) calculated by this program: ")
print(value)
print()
difference = math.cos(x) - value
print("Difference between this value and math.cos(x): ")
print(difference)
main()
Thank you!
C.Skinner
More information about the Tutor
mailing list