0

This is using the Taylor series to find the value of the sine of an angle between -1 and +1, but what I'm trying to do is get the value of every degree, print it, go to the next degree, then print, etc...

def Fac(n):
    r=1
    for c in range(n,1,-1):
        r*=c #r=r*c
    return r

def Pow(x,n):
    r=1
    for c in range(0,n):
        r*=x
    return r

def Sign(i): #i will be the number of the term we are on for Taylor
    r=1
    for c in range(0,i):
        r*=-1
    return r

def Rad(a):
    return a*3.141592654/180

def SinTerm(x,n,i):
    y=Sign(i)*Pow(x,n)/Fac(n)
    return y


def main():
    for c in range(0,361):
        a=c
    i=3
    n=1
    sum=0
    for c in range(0,i):
        sum+=SinTerm(Rad(a),n,c)
        n+=2
    print "Taylor COSINE sum = ", sum

while True:
    main()

I thought making range(0,361) would show all the values separately, but it adds up all the values of 0-360 degrees instead. How would I make it so the sin value for every degree is shown separately? I would prefer answers that guide me in the right direction, not give the answer away out right.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    Your `main` does an aweful lot just so it can assign `a` to be 360... – StoryTeller - Unslander Monica Sep 14 '12 at 21:28
  • 1
    If you have a loop that runs N times, and you want to print out N values, the print statement has to be inside the loop. (Unless the loop builds up a collection of values, but you're not doing that anywhere.) Your have one loop that runs 361 times, one that runs 3 times, but the print statement isn't in either, so it only runs once. – abarnert Sep 14 '12 at 21:30
  • My goal is to create a sine graph with all the values being represented on the graph with asteriks. My prof only wants us to use the python IDLS for the graph so from column 0 in the IDLE would be -1 and column 80 would be 1 while column 40 would be 0. Im basically trying to show a sideways sine graph on the IDLE which is why I don't know what the importance of the print statement is for my goal. – Brian Ho Sep 14 '12 at 21:48
  • 1
    Brian: You question mentions printing in the title, and you said "what im trying to do is get the value of every degree, print it, go to the next degree, then print," but you don't think the print statement is relevant? In that case, you should change the question. – David Robinson Sep 14 '12 at 22:01

1 Answers1

5

Your issue is the indentation in the for loop. Only the line a=c is included. You want to indent the whole thing (and also, it looks like, loop over a rather than c in the outer loop):

for a in range(0,361):
    i=3
    n=1
    sum=0
    for c in range(0,i):
        sum+=SinTerm(Rad(a),n,c)
        n+=2
    print "Taylor COSINE sum = ", sum
David Robinson
  • 77,383
  • 16
  • 167
  • 187