# Chapter 7 : Roots of polynomials¶

## Ex7.1: Pg: 179¶

In [1]:
from numpy import arange
def f(x):
y=(x-4)*(x+6)
return y
n=2
a=[0,-24,2,1]
t=4
r=a[(3)]
a[(3)]=0
for i in arange(n,0,-1):
s=a[(i)]
a[(i)]=r
r=s+r*t

print "The quptient is a(1)+a(2)*x where :"
print "a(1)=",a[1]
print "a(2)=",a[2]
print "remainder=",r

The quptient is a(1)+a(2)*x where :
a(1)= 6
a(2)= 1
remainder= 0


## Ex7.2: Pg: 183¶

In [2]:
def f(x):
y=x**3 - 13*x - 12
return y

x1t=-3
x2t=-1
x3t=4
x0=4.5
x1=5.5
x2=5
print "iteration:",0
print "xr:",x2
print "---------------------------------------------"
for i in range(0,4):

h0=x1-x0
h1=x2-x1
d0=(f(x1)-f(x0))/(x1-x0)
d1=(f(x2)-f(x1))/(x2-x1)
a=(d1-d0)/(h1+h0)
b=a*h1+d1
c=f(x2)
d=(b**2 - 4*a*c)**0.5
if abs(b+d)>abs(b-d):
x3=x2+((-2*c)/(b+d))
else:
x3=x2+((-2*c)/(b-d))
ea=abs(x3-x2)*100/x3
x0=x1
x1=x2
x2=x3
print "iteration:",i
print "xr:",x2
print "ea(%):",ea,"%"
print "---------------------------------------------"

iteration: 0
xr: 5
---------------------------------------------
iteration: 0
xr: 3.97648704224
ea(%): 25.7391246818 %
---------------------------------------------
iteration: 1
xr: 4.00105049882
ea(%): 0.613925182444 %
---------------------------------------------
iteration: 2
xr: 4.00000070527
ea(%): 0.026244833989 %
---------------------------------------------
iteration: 3
xr: 4.0
ea(%): 1.76317506373e-05 %
---------------------------------------------


## Ex7.3: Pg: 187¶

In [3]:
from numpy import arange
def f(x):
y=x**5-3.5*x**4+2.75*x**3+2.125*x**2-3.875*x+1.25
return y
r=-1
s=-1
es=1##%
n=6
count=1
ear=100
eas=100
a=[0,1.25, -3.875, 2.125, 2.75, -3.5, 1]
b=a
c=a
while (ear>es) and (eas>es):

b[(n)]=a[(n)]
b[(n-1)]=a[(n-1)]+r*b[(n)]
for i in arange(n-2,0,-1):
b[(i)]=a[(i)]+r*b[(i+1)]+s*b[(i+2)]

c[(n)]=b[(n)]
c[(n-1)]=b[(n-1)]+r*c[(n)]
for i in arange((n-2),1,-1):
c[(i)]=b[(i)]+r*c[(i+1)]+s*c[(i+2)]

#c(3)*dr+c(4)*ds=-b(2)
#c(2)*dr+c(3)*ds=-b(1)
ds=((-b[(1)])+(b[(2)]*c[(2)]/c[(3)]))/(c[(3)]-(c[(4)]*c[(2)]/c[(3)]))
dr=(-b[(2)]-c[(4)]*ds)/c[(3)]
r=r+dr
s=s+ds
ear=abs(dr/r)*100
eas=abs(ds/s)*100
print "Iteration:",count
print "delata r:",dr
print "delata s:",ds
print "r:",r
print "s:",s
print "Error in r:",ear
print "Error in s:",eas
print "-----------------------------------------------------"
count=count+1

x1=(r+(r**2 + 4*s)**0.5)/2
x2=(r-(r**2 + 4*s)**0.5)/2
bracket=[x1, x2]
print bracket,"The roots are:"
print "x**3 - 4*x**2 + 5.25*x - 2.5","The quotient is:"
print "-----------------------------------------------------"

Iteration: 1
delata r: 0.212481426449
delata s: 1.61961367013
r: -0.787518573551
s: 0.619613670134
Error in r: 26.9811320755
Error in s: 261.39088729
-----------------------------------------------------
Iteration: 2
delata r: 4.04595826496
delata s: 5.2325468461
r: 3.25843969141
s: 5.85216051623
Error in r: 124.168579079
Error in s: 89.4122235982
-----------------------------------------------------
Iteration: 3
delata r: 2.4536017174
delata s: -24.7953826358
r: 5.71204140882
s: -18.9432221196
Error in r: 42.9549007403
Error in s: 130.893163155
-----------------------------------------------------
Iteration: 4
delata r: -7.89009085
delata s: 22.0401977474
r: -2.17804944119
s: 3.09697562788
Error in r: 362.254901143
Error in s: 711.668427386
-----------------------------------------------------
Iteration: 5
delata r: -58.1023530819
delata s: 185.887882155
r: -60.2804025231
s: 188.984857783
Error in r: 96.386803422
Error in s: 98.3612572646
-----------------------------------------------------
Iteration: 6
delata r: 1160.70485616
delata s: 90001.7445985
r: 1100.42445363
s: 90190.7294562
Error in r: 105.477922844
Error in s: 99.7904608834
-----------------------------------------------------
Iteration: 7
delata r: -21882.7802726
delata s: 31520060.138
r: -20782.355819
s: 31610250.8675
Error in r: 105.294993807
Error in s: 99.7146788558
-----------------------------------------------------
Iteration: 8
delata r: 411986.000256
delata s: 11197461422.1
r: 391203.644438
s: 11229071673.0
Error in r: 105.312413653
Error in s: 99.7184963122
-----------------------------------------------------
Iteration: 9
delata r: -7758767.67604
delata s: 3.9700158833e+12
r: -7367564.0316
s: 3.98124495498e+12
Error in r: 105.309809901
Error in s: 99.7179507466
-----------------------------------------------------
Iteration: 10
delata r: 146111056.094
delata s: 1.4079763212e+15
r: 138743492.063
s: 1.41195756615e+15
Error in r: 105.310205129
Error in s: 99.7180336683
-----------------------------------------------------
Iteration: 11
delata r: -2751543853.16
delata s: 4.99319657786e+17
r: -2612800361.1
s: 5.00731615352e+17
Error in r: 105.310145166
Error in s: 99.718021087
-----------------------------------------------------
Iteration: 12
delata r: 51816650788.5
delata s: 1.77078151694e+20
r: 49203850427.4
s: 1.7757888331e+20
Error in r: 105.310154263
Error in s: 99.7180229957
-----------------------------------------------------
Iteration: 13
delata r: -9.75803357309e+11
delata s: 6.27987270746e+22
r: -9.26599506881e+11
s: 6.29763059579e+22
Error in r: 105.310152883
Error in s: 99.7180227061
-----------------------------------------------------
Iteration: 14
delata r: 1.83761812944e+13
delata s: 2.22708488435e+25
r: 1.74495817875e+13
s: 2.23338251495e+25
Error in r: 105.310153092
Error in s: 99.7180227501
-----------------------------------------------------
Iteration: 15
delata r: -3.46057469143e+14
delata s: 7.89810111349e+27
r: -3.28607887356e+14
s: 7.92043493864e+27
Error in r: 105.310153061
Error in s: 99.7180227434
-----------------------------------------------------
Iteration: 16
delata r: 6.51690195935e+15
delata s: 2.80097098525e+30
r: 6.18829407199e+15
s: 2.80889142018e+30
Error in r: 105.310153065
Error in s: 99.7180227444
-----------------------------------------------------
Iteration: 17
delata r: -1.22725312811e+17
delata s: 9.93332238103e+32
r: -1.16537018739e+17
s: 9.96141129523e+32
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 18
delata r: 2.31114454359e+18
delata s: 3.5227388664e+35
r: 2.19460752485e+18
s: 3.53270027769e+35
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 19
delata r: -4.35231247656e+19
delata s: 1.24929893994e+38
r: -4.13285172407e+19
s: 1.25283164021e+38
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 20
delata r: 8.19620908009e+20
delata s: 4.43049541996e+40
r: 7.78292390768e+20
s: 4.44302373636e+40
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 21
delata r: -1.54349770717e+22
delata s: 1.57122439144e+43
r: -1.46566846809e+22
s: 1.57566741518e+43
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 22
delata r: 2.90669155552e+23
delata s: 5.57216711507e+45
r: 2.76012470871e+23
s: 5.58792378922e+45
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 23
delata r: -5.47383760902e+24
delata s: 1.97610516534e+48
r: -5.19782513815e+24
s: 1.98169308913e+48
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 24
delata r: 1.03082482601e+26
delata s: 7.00803034767e+50
r: 9.78846574632e+25
s: 7.02784727856e+50
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 25
delata r: -1.94123373367e+27
delata s: 2.48531759419e+53
r: -1.8433490762e+27
s: 2.49234544147e+53
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 26
delata r: 3.6557020297e+28
delata s: 8.81389382974e+55
r: 3.47136712208e+28
s: 8.83881728416e+55
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 27
delata r: -6.88436281433e+29
delata s: 3.12574636833e+58
r: -6.53722610212e+29
s: 3.13458518562e+58
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 28
delata r: 1.29645280097e+31
delata s: 1.10851010324e+61
r: 1.23108053995e+31
s: 1.11164468843e+61
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 29
delata r: -2.44146032172e+32
delata s: 3.93120395638e+63
r: -2.31835226773e+32
s: 3.94232040327e+63
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 30
delata r: 4.59772118049e+33
delata s: 1.39415639979e+66
r: 4.36588595372e+33
s: 1.3980987202e+66
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 31
delata r: -8.65835904252e+34
delata s: 4.94421578898e+68
r: -8.22177044714e+34
s: 4.95819677619e+68
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 32
delata r: 1.63052908966e+36
delata s: 1.75340942893e+71
r: 1.54831138518e+36
s: 1.75836762571e+71
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 33
delata r: -3.07058773973e+37
delata s: 6.21826545742e+73
r: -2.91575660121e+37
s: 6.23584913368e+73
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 34
delata r: 5.78248442618e+38
delata s: 2.20523653295e+76
r: 5.49090876606e+38
s: 2.21147238208e+76
Error in r: 105.310153065
Error in s: 99.7180227443
-----------------------------------------------------
Iteration: 35
delata r: -inf
delata s: inf
r: -inf
s: inf
Error in r: nan
Error in s: nan
-----------------------------------------------------
[nan, -inf] The roots are:
x**3 - 4*x**2 + 5.25*x - 2.5 The quotient is:
-----------------------------------------------------


## Ex7.4: Pg: 191¶

In [4]:
from math import cos
def f(x):
y=x-cos(x)
return y
x1=0
if f(x1)<0:
x2=x1+0.001
while f(x2)<0:
x2=x2+0.001

elif x20==x1+0.001:
while f(x2)>0:
x2=x2+0.001

else:
print "The root is=",x1

x=x2-(x2-x1)*f(x2)/(f(x2)-f(x1))
print "The root is=",x

The root is= 0.738868466337


## Ex7.5: Pg: 191¶

In [5]:
def u(x,y):
z=x**2+x*y-10
return z
def v(x,y):
z=y+3*x*y**2-57
return z
x=1
y=3.5
while u(x,y)!=v(x,y):
x=x+1
y=y-0.5

print "x=",x
print "y=",y

x= 2
y= 3.0


## Ex7.6: Pg: 194¶

In [6]:
from sympy import symbols,solve
x=symbols('s')
p=x**10 -1
print "The roots of the polynomial are:"
for r in solve(p):
print r

The roots of the polynomial are:
-1
1
-1/4 + sqrt(5)/4 - I*sqrt(sqrt(5)/8 + 5/8)
-1/4 + sqrt(5)/4 + I*sqrt(sqrt(5)/8 + 5/8)
1/4 + sqrt(5)/4 - I*sqrt(-sqrt(5)/8 + 5/8)
1/4 + sqrt(5)/4 + I*sqrt(-sqrt(5)/8 + 5/8)
-sqrt(5)/4 - 1/4 - I*sqrt(-sqrt(5)/8 + 5/8)
-sqrt(5)/4 - 1/4 + I*sqrt(-sqrt(5)/8 + 5/8)
-sqrt(5)/4 + 1/4 - I*sqrt(sqrt(5)/8 + 5/8)
-sqrt(5)/4 + 1/4 + I*sqrt(sqrt(5)/8 + 5/8)


## Ex7.7: Pg: 195¶

In [7]:
from sympy import symbols,solve
x=symbols('s')
p=x**5 - 3.5*x**4 +2.75*x**3 +2.125*x**2 - 3.875*x + 1.25
print "The roots of the polynomial are:"
for r in solve(p):
print r

The roots of the polynomial are:
-1.00000000000000
0.500000000000000
2.00000000000000
1.0 - 0.5*I
1.0 + 0.5*I


## Ex7.8: Pg: 196¶

In [8]:
from math import cos
def f(x):
y=x-cos(x)
return y
x1=0
if f(x1)<0:
x2=x1+0.00001
while f(x2)<0:
x2=x2+0.00001

elif x2==x1+0.00001:
while f(x2)>0:
x2=x2+0.00001

else:
print x1,"The root is="

x=x2-(x2-x1)*f(x2)/(f(x2)-f(x1))
print "The root is=",x

The root is= 0.739083980074