[ create a new paste ] login | about

Link: http://codepad.org/zcZg8Wa0    [ raw code | output | fork ]

bot42 - Python, pasted on Oct 21:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def computeRoot(poly, x_0, epsilon):
    ctr=-1
    y=0
    dy=42
    epsilon=abs(epsilon)
    while (y>epsilon or y<-epsilon) or ctr==-1:
        ctr=ctr+1
        print ctr
        x_0=x_0-(y/dy)
        print "x_0=",x_0
        y=0
        dy=0
        for thing in range(len(poly)):
            y+=poly[thing]*(x_0**thing)
            
            dy+=(thing)*poly[thing]*(x_0**(thing-1))
        print 'y=',y 
    return [x_0,ctr]  
print computeRoot([1, -1, 1, -1], 2, .001)


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
0
x_0= 2
y= -5
1
x_0= 1.44444444444
y= -1.37174211248
2
x_0= 1.13057124922
y= -0.297466290463
3
x_0= 1.01497995228
y= -0.0304120639884
4
x_0= 1.00022106302
y= -0.000442223788041
[1.0002210630197605, 4]


Create a new paste based on this one


Comments: