day1/exercise/roots.py
changeset 64 333092b68926
equal deleted inserted replaced
63:f5eac04a00fe 64:333092b68926
       
     1 import math
       
     2 
       
     3 def linspace(a, b, N):
       
     4     lns = []
       
     5     step = (float(b) - float(a)) / float(N - 1)
       
     6     print step
       
     7     for i in range(N):
       
     8         lns.append(a + i*step)
       
     9 
       
    10     return lns
       
    11 
       
    12 def sinsin_func():
       
    13     x = linspace(0, 5, 11)
       
    14     sin_list = []
       
    15     for i in x:
       
    16         sin_list.append(math.sin(i) + math.sin(10*i))
       
    17 
       
    18     return sin_list
       
    19 
       
    20 def find_root_range():
       
    21     sin_list = sinsin_func()
       
    22     for i, sins in enumerate(sin_list):
       
    23         if (sin_list[i] > 0 and sin_list[i+1] < 0) or (sin_list[i] > 0 and sin_list[i+1] < 0):
       
    24             print "Roots lie between: %f and %f" % (sin_list[i], sin_list[i+1])
       
    25         if sin_list[i] == 0:
       
    26             print "%f is the root" % sin_list[i]
       
    27 
       
    28 find_root_range()