# Chapter02:Solution of Algebric and Transcendental Equations¶

## Ex2.1:pg-24¶

In [4]:
#example 2.1
#bisection method
#page 24
import math
def f(x):
return math.pow(x,3)-x-1
x1=1
x2=2      #f(1) is negative and f(2) is positive
d=0.0001       #for accuracy of root
c=1
print "Succesive approximations \t   x1\t   \tx2\t   \tm\t   \tf(m)\n"
while abs(x1-x2)>d:

m=(x1+x2)/2.0
print "                          \t%f\t%f\t%f\t%f\n" %(x1,x2,m,f(m))
if f(m)*f(x1)>0.0:
x1=m
else:
x2=m
c=c+1 # to count number of iterations
print "the solution of equation after %i iteration is %g" %(c,m)

Succesive approximations 	   x1	   	x2	   	m	   	f(m)

1.000000	2.000000	1.500000	0.875000

1.000000	1.500000	1.250000	-0.296875

1.250000	1.500000	1.375000	0.224609

1.250000	1.375000	1.312500	-0.051514

1.312500	1.375000	1.343750	0.082611

1.312500	1.343750	1.328125	0.014576

1.312500	1.328125	1.320312	-0.018711

1.320312	1.328125	1.324219	-0.002128

1.324219	1.328125	1.326172	0.006209

1.324219	1.326172	1.325195	0.002037

1.324219	1.325195	1.324707	-0.000047

1.324707	1.325195	1.324951	0.000995

1.324707	1.324951	1.324829	0.000474

1.324707	1.324829	1.324768	0.000214

the solution of equation after 15 iteration is 1.32477'


## Ex2.2:pg-25¶

In [5]:
#example 2.2
#bisection method
#page 25
import math
def f(x):
return math.pow(x,3)-2*x-5
x1=2
x2=3   #f(2) is negative and f(3) is positive
d=0.0001   #for accuracy of root
c=1
print "Succesive approximations \t   x1\t   \tx2\t   \tm\t   \tf(m)\n"
while abs(x1-x2)>d:
m=(x1+x2)/2.0
print "                          \t%f\t%f\t%f\t%f\n" %(x1,x2,m,f(m))
if f(m)*f(x1)>0:
x1=m
else:
x2=m
c=c+1;# to count number of iterations
print "the solution of equation after %i iteration is %0.4g" %(c,m)

Succesive approximations 	   x1	   	x2	   	m	   	f(m)

2.000000	3.000000	2.500000	5.625000

2.000000	2.500000	2.250000	1.890625

2.000000	2.250000	2.125000	0.345703

2.000000	2.125000	2.062500	-0.351318

2.062500	2.125000	2.093750	-0.008942

2.093750	2.125000	2.109375	0.166836

2.093750	2.109375	2.101562	0.078562

2.093750	2.101562	2.097656	0.034714

2.093750	2.097656	2.095703	0.012862

2.093750	2.095703	2.094727	0.001954

2.093750	2.094727	2.094238	-0.003495

2.094238	2.094727	2.094482	-0.000771

2.094482	2.094727	2.094604	0.000592

2.094482	2.094604	2.094543	-0.000090

the solution of equation after 15 iteration is 2.095


## Ex2.3:pg-26¶

In [6]:
#example 2.3
#bisection method
#page 26
import math
def f(x):
return math.pow(x,3)+math.pow(x,2)+x+7
x1=-3
x2=-2      #f(-3) is negative and f(-2) is positive
d=0.0001   #for accuracy of root
c=1
print "Succesive approximations \t   x1\t   \tx2\t   \tm\t   \tf(m)\n"
while abs(x1-x2)>d:
m=(x1+x2)/2.0
print "                          \t%f\t%f\t%f\t%f\n" %(x1,x2,m,f(m))
if f(m)*f(x1)>0:
x1=m
else:
x2=m
c=c+1  # to count number of iterations
print "the solution of equation after %i iteration is %0.4g" %(c,m)

Succesive approximations 	   x1	   	x2	   	m	   	f(m)

-3.000000	-2.000000	-2.500000	-4.875000

-2.500000	-2.000000	-2.250000	-1.578125

-2.250000	-2.000000	-2.125000	-0.205078

-2.125000	-2.000000	-2.062500	0.417725

-2.125000	-2.062500	-2.093750	0.111481

-2.125000	-2.093750	-2.109375	-0.045498

-2.109375	-2.093750	-2.101562	0.033315

-2.109375	-2.101562	-2.105469	-0.006010

-2.105469	-2.101562	-2.103516	0.013673

-2.105469	-2.103516	-2.104492	0.003836

-2.105469	-2.104492	-2.104980	-0.001086

-2.104980	-2.104492	-2.104736	0.001376

-2.104980	-2.104736	-2.104858	0.000145

-2.104980	-2.104858	-2.104919	-0.000470

the solution of equation after 15 iteration is -2.105


## Ex2.4:pg-26¶

In [10]:
#example 2.4
#bisection method
#page 26
import math
def f(x):
return x*math.exp(x)-1
x1=0
x2=1  #f(0) is negative and f(1) is positive
d=0.0005   #maximun tolerance value
c=1
print "Succesive approximations \t   x1\t   \tx2\t   \tm\t   \ttol\t    \tf(m)\n"
while abs((x2-x1)/x2)>d:
m=(x1+x2)/2.0  #tolerance value for each iteration
tol=((x2-x1)/x2)*100
print "                          \t%f\t%f\t%f\t%f\t%f\n" %(x1,x2,m,tol,f(m))
if f(m)*f(x1)>0:
x1=m
else:
x2=m
c=c+1  # to count number of iterations
print "the solution of equation after %i iteration is %0.4g" %(c,m)

Succesive approximations 	   x1	   	x2	   	m	   	tol	    	f(m)

0.000000	1.000000	0.500000	100.000000	-0.175639

0.500000	1.000000	0.750000	50.000000	0.587750

0.500000	0.750000	0.625000	33.333333	0.167654

0.500000	0.625000	0.562500	20.000000	-0.012782

0.562500	0.625000	0.593750	10.000000	0.075142

0.562500	0.593750	0.578125	5.263158	0.030619

0.562500	0.578125	0.570312	2.702703	0.008780

0.562500	0.570312	0.566406	1.369863	-0.002035

0.566406	0.570312	0.568359	0.684932	0.003364

0.566406	0.568359	0.567383	0.343643	0.000662

0.566406	0.567383	0.566895	0.172117	-0.000687

0.566895	0.567383	0.567139	0.086059	-0.000013

the solution of equation after 13 iteration is 0.5671


## Ex2.5:pg-27¶

In [11]:
#example 2.5
#bisection method
#page 27
import math
def f(x):
return 4*math.exp(-x)*math.sin(x)-1
x1=0
x2=0.5  #f(0) is negative and f(1) is positive
d=0.0001    #for accuracy of root
c=1
print "Succesive approximations \t   x1\t   \tx2\t   \tm\t   \t    \tf(m)\n"
while abs(x2-x1)>d:
m=(x1+x2)/2.0
print "                          \t%f\t%f\t%f\t%f\n" %(x1,x2,m,f(m))
if f(m)*f(x1)>0:
x1=m
else:
x2=m
c=c+1 # to count number of iterations
print "the solution of equation after %i iteration is %0.3g" %(c,m)

Succesive approximations 	   x1	   	x2	   	m	   	    	f(m)

0.000000	0.500000	0.250000	-0.229286

0.250000	0.500000	0.375000	0.006941

0.250000	0.375000	0.312500	-0.100293

0.312500	0.375000	0.343750	-0.044068

0.343750	0.375000	0.359375	-0.017925

0.359375	0.375000	0.367188	-0.005334

0.367188	0.375000	0.371094	0.000842

0.367188	0.371094	0.369141	-0.002236

0.369141	0.371094	0.370117	-0.000694

0.370117	0.371094	0.370605	0.000075

0.370117	0.370605	0.370361	-0.000310

0.370361	0.370605	0.370483	-0.000118

0.370483	0.370605	0.370544	-0.000022

the solution of equation after 14 iteration is 0.371


## Ex2.6:pg-28¶

In [15]:
#example 2.6
#false position method
#page 28
import math
def f(x):
return x**3-2*x-5
a=2.0
b=3.0      #f(2) is negative and f(3)is positive
d=0.00001
print "succesive iterations    \ta\t    b\t    f(a)\t    f(b)\t\  x1\n"
for i in range(1,25):
x1=b*f(a)/(f(a)-f(b))+a*f(b)/(f(b)-f(a))
if(f(a)*f(x1))>0:
b=x1
else:
a=x1
if abs(f(x1))<d:
break
print "                        \t%f  %f  %f  %f  %f\n" %(a,b,f(a),f(b),x1)
print "the root of the equation is  %f" %(x1)

succesive iterations    	a	    b	    f(a)	    f(b)	\  x1

2.000000  2.058824  -1.000000  -0.390800  2.058824

2.096559  2.058824  0.022428  -0.390800  2.096559

2.094511  2.058824  -0.000457  -0.390800  2.094511

the root of the equation is  2.094552


## Ex2.7:pg-29¶

In [17]:
#example 2.7
#false position method
#page 29
def f(x):
return x**2.2-69
a=5.0
b=6.0      #f(5) is negative and f(6)is positive
d=0.00001
print "succesive iterations    \ta\t    b\t    f(a)\t    f(b)\t\  x1\n"
for i in range(1,25):
x1=b*f(a)/(f(a)-f(b))+a*f(b)/(f(b)-f(a));
if(f(a)*f(x1))>0:
b=x1
else:
a=x1
if abs(f(x1))<d:
break
print "                         \t%f  %f  %f  %f  %f\n" %(a,b,f(a),f(b),x1)
print "the root of the equation is  %f" %(x1)

succesive iterations    	a	    b	    f(a)	    f(b)	\  x1

7.027228  6.000000  3.933141  -17.485113  7.027228

6.838593  6.000000  -0.304723  -17.485113  6.838593

6.853467  6.000000  0.024411  -17.485113  6.853467

6.852277  6.000000  -0.001950  -17.485113  6.852277

6.852372  6.000000  0.000156  -17.485113  6.852372

6.852365  6.000000  -0.000012  -17.485113  6.852365

the root of the equation is  6.852365


## Ex2.8:pg-29¶

In [11]:
#example 2.8
#false position method
#page 29
import math
def f(x):
return 2*x-log10(x)-7
a=3.0
b=4.0         #f(3) is negative and f(4)is positive
d=0.00001
print "succesive iterations    \ta    \t    b\t      f(a)\t    f(b)\t      x1\n"
for i in range(1,25):
x1=b*f(a)/(f(a)-f(b))+a*f(b)/(f(b)-f(a))
if(f(a)*f(x1))>0:
b=x1
else:
a=x1
if abs(f(x1))<d:
break
print "                    \t%f  %f  %f  %f  %f\n" %(a,b,f(a),f(b),x1)
print "the root of the equation is  %0.4g" %(x1)

succesive iterations    	a    	    b	      f(a)	    f(b)	      x1

3.000000  3.787772  -1.477121  -0.002839  3.787772

3.789289  3.787772  0.000021  -0.002839  3.789289

the root of the equation is  3.789


## Ex2.9:pg-30¶

In [20]:
#example 2.9
#false position method
#page 30
import math
def f(x):
return 4*math.exp(-x)*math.sin(x)-1
a=0.0
b=0.5   #f(0) is negative and f(0.5)is positive
d=0.00001
print "succesive iterations    \ta\t    b\t    f(a)\t    f(b)\t\  x1\n"
for i in range(1,25):
x1=b*f(a)/(f(a)-f(b))+a*f(b)/(f(b)-f(a))
if(f(a)*f(x1))>0:
b=x1
else:
a=x1
if abs(f(x1))<d:
break
print "                          \t%f  %f  %f  %f  %f\n" %(a,b,f(a),f(b),x1)
print "the root of the equation is  %f" %(x1)

succesive iterations    	a	    b	    f(a)	    f(b)	\  x1

0.429869  0.500000  0.084545  0.163145  0.429869

0.354433  0.500000  -0.026054  0.163145  0.354433

0.374479  0.500000  0.006132  0.163145  0.374479

0.369577  0.500000  -0.001547  0.163145  0.369577

0.370802  0.500000  0.000384  0.163145  0.370802

0.370497  0.500000  -0.000096  0.163145  0.370497

0.370573  0.500000  0.000024  0.163145  0.370573

the root of the equation is  0.370554


## Ex2.10:pg-33¶

In [23]:
#example 2.10
#iteration method
#page 33
import math
def f(x):
return 1/(math.sqrt(x+1))
x1=0.75
x2=0.0
n=1
d=0.0001 #accuracy opto 10^-4
c=0 #to count no of iterations
print "successive iterations \t\x01\tf(x1)\n"
while abs(x1-x2)>d:
print "                       \t%f    %f\n" %(x1,f(x1))
x2=x1
x1=f(x1)
c=c+1
print " the root of the eqaution after %i iteration is %0.4g" %(c,x1)

successive iterations 		f(x1)

0.750000    0.755929

0.755929    0.754652

0.754652    0.754926

0.754926    0.754867

the root of the eqaution after 4 iteration is 0.7549


## Ex2.11:pg-34¶

In [28]:
#example 2.11
#iteration method
#page34
import math
def f(x):
return cos(x)/2.0+3.0/2.0
x1=1.5    # as roots lies between 3/2 and pi/2
x2=0
d=0.0001  # accuracy opto 10^-4
c=0 # to count no of iterations
print "successive iterations \t\x01\tf(x1)\n"
while abs(x2-x1)>d:

print "                      \t%f    %f\n" %(x1,f(x1))
x2=x1
x1=f(x1)
c=c+1
print " the root of the eqaution after %i iteration is %0.4g" %(c,x1)

successive iterations 		f(x1)

1.500000    1.535369

1.535369    1.517710

1.517710    1.526531

1.526531    1.522126

1.522126    1.524326

1.524326    1.523227

1.523227    1.523776

1.523776    1.523502

1.523502    1.523639

1.523639    1.523570

the root of the eqaution after 10 iteration is 1.524


## Ex2.12:pg-35¶

In [34]:
#example 2.12
#iteration method
#page 35
import math
def f(x):
return math.exp(-x)
x1=1.5 # as roots lies between 0 and 1
x2=0
d=0.0001   # accuracy opto 10^-4
c=0    # to count no of iterations
print "successive iterations \t   x1 \t         f(x1)\n"
while abs(x2-x1)>d:

print "                       \t%f    %f\n" %(x1,f(x1))
x2=x1
x1=f(x1)
c=c+1
print "  the root of the eqaution after %i iteration is %0.4g" %(c,x1)

successive iterations 	   x1 	         f(x1)

1.500000    0.223130

0.223130    0.800011

0.800011    0.449324

0.449324    0.638059

0.638059    0.528317

0.528317    0.589597

0.589597    0.554551

0.554551    0.574330

0.574330    0.563082

0.563082    0.569451

0.569451    0.565836

0.565836    0.567885

0.567885    0.566723

0.566723    0.567382

0.567382    0.567008

0.567008    0.567220

0.567220    0.567100

0.567100    0.567168

the root of the eqaution after 18 iteration is 0.5672


## Ex2.13:pg-35¶

In [36]:
#example 2.13
#iteration method
#page 35
import math
def f(x):
return 1+math.sin(x)/10
x1=1.0      # as roots lies between 1 and pi evident from graph
x2=0
d=0.0001     # accuracy opto 10^-4
c=0    # to count no of iterations
print "successive iterations \t   x1 \t    f(x1)\n"
while abs(x2-x1)>d:
print "                       \t%f  %f\n" %(x1,f(x1))
x2=x1
x1=f(x1)
c=c+1
print " the root of the eqaution after %i iteration is %0.4g" %(c,x1)

successive iterations 	   x1 	    f(x1)

1.000000  1.084147

1.084147  1.088390

1.088390  1.088588

1.088588  1.088597

the root of the eqaution after 4 iteration is 1.089


## Ex2.14:pg-36¶

In [42]:
#example 2.14
#aitken's process
#page 36
import math
def f(x):
return 1.5+math.cos(x)/2.0
x0=1.5
y=0
e=0.0001
c=0
print "successive iterations     \t  x0 \t      x1 \t       x2 \t        x3 \t      y\n"
for i in range(1,10):
x1=f(x0)
x2=f(x1)
x3=f(x2)
y=x3-((x3-x2)**2)/(x3-2*x2+x1)
d=y-x0
x0=y
if abs(f(x0))<e:
break
c=c+1
print "                          \t%f   %f   %f   %f   %f\n" %(x0,x1,x2,x3,y)
print "the root of the equation after %i iteration is %f" %(c,y)

successive iterations     	  x0 	      x1 	       x2 	        x3 	      y

1.523592   1.535369   1.517710   1.526531   1.523592

1.523593   1.523593   1.523593   1.523593   1.523593

1.523593   1.523593   1.523593   1.523593   1.523593

1.523593   1.523593   1.523593   1.523593   1.523593

1.523593   1.523593   1.523593   1.523593   1.523593

1.523593   1.523593   1.523593   1.523593   1.523593

1.523593   1.523593   1.523593   1.523593   1.523593

1.523593   1.523593   1.523593   1.523593   1.523593

1.523593   1.523593   1.523593   1.523593   1.523593

the root of the equation after 9 iteration is 1.523593


## Ex2.15:pg-39¶

In [44]:
#example 2.15
#newton-raphson method
#page 39
import math
def f(x):
return x**3-2*x-5
def f1(x):
return 3*x**2-2    # first derivative of the function
x0=2.0       # initial value
d=0.0001
c=0
n=1
print "successive iterations \t x0 \t        f(x0) \t        f1(x0)\n"
while n==1:
x2=x0
x1=x0-(f(x0)/f1(x0))
x0=x1
print "                      \t%f   \t%f   \t%f   \n" %(x2,f(x1),f1(x1))
c=c+1
if abs(f(x0))<d:
break
print "the root of %i iteration is:%f" %(c,x0)

successive iterations 	 x0 	        f(x0) 	        f1(x0)

2.000000   	0.061000   	11.230000

2.100000   	0.000186   	11.161647

2.094568   	0.000000   	11.161438

the root of 3 iteration is:2.094551


## Ex2.16:pg-40¶

In [46]:
#example 2.16
#newton-raphson method
#page 40
import math
def f(x):
return x*math.sin(x)+math.cos(x)
def f1(x):
return x*math.cos(x)   #first derivation of the function
x0=math.pi   # initial value
d=0.0001
c=0
n=1
print "successive iterations \tx0\t        f(x0)\t        f1(x0)\n"
while n==1:
x2=x0
x1=x0-(f(x0)/f1(x0))
x0=x1
print "                      \t%f  \t%f  \t%f\n" %(x2,f(x1),f1(x1))
c=c+1
if abs(f(x0))<d:
break
print "the root of %i iteration is:%f" %(c,x0)

successive iterations 	x0	        f(x0)	        f1(x0)

3.141593  	-0.066186  	-2.681457

2.823283  	-0.000564  	-2.635588

2.798600  	-0.000000  	-2.635185

the root of 3 iteration is:2.798386


## Ex2.17:pg-40¶

In [48]:
#example 2.17
#newton-raphson method
#page 40
import math
def f(x):
return x*math.exp(x)-1
def f1(x):
return math.exp(x)+x*math.exp(x)   #first derivative of the function
x0=0       # initial value
d=0.0001
c=0
n=1
print "successive iterations \tx0\t        f(x0)\t        f1(x0)\n"
while n==1:
x2=x0
x1=x0-(f(x0)/f1(x0))
x0=x1
print "                      \t%f   \t%f   \t%f\n" %(x2,f(x1),f1(x1))
c=c+1
if abs(f(x0))<d:
break
print "the root of %i iteration is:%f" %(c,x0)

successive iterations 	x0	        f(x0)	        f1(x0)

0.000000   	1.718282   	5.436564

1.000000   	0.355343   	3.337012

0.683940   	0.028734   	2.810232

0.577454   	0.000239   	2.763614

0.567230   	0.000000   	2.763223

the root of 5 iteration is:0.567143


## Ex2.18:pg-41¶

In [51]:
#example 2.18
#newton-raphson method
#page 41
import math
def f(x):
return math.sin(x)-x/2.0
def f1(x):
return math.cos(x)-0.5
x0=math.pi/2.0 # initial value
d=0.0001
c=0
n=1
print "successive iterations \t x0 \t        f(x0)\t        f1(x0)\n"
while n==1:
x2=x0
x1=x0-(f(x0)/f1(x0))
x0=x1
print "                      \t%f\t%f\t%f\n" %(x2,f(x1),f1(x1))
c=c+1
if abs(f(x0))<d:
break
print "the root of %i iteration is: %0.4g" %(c,x0)

successive iterations 	 x0 	        f(x0)	        f1(x0)

1.570796	-0.090703	-0.916147

2.000000	-0.004520	-0.824232

1.900996	-0.000014	-0.819039

the root of 3 iteration is: 1.896


## Ex2.19:pg-41¶

In [54]:
#example 2.19
#newton-raphson method
#page 41
import math
def f(x):
return 4*math.exp(-x)*math.sin(x)-1
def f1(x):
return math.cos(x)*4*math.exp(-x)-4*math.exp(-x)*math.sin(x)
x0=0.2  # initial value
d=0.0001
c=0
n=1
print "successive iterations \t x0 \t        f(x0)\t        f1(x0)\n"
while n==1:
x2=x0
x1=x0-(f(x0)/f1(x0))
x0=x1
print "                      \t%f  \t%f    \t%f\n" %(x2,f(x1),f1(x1))
c=c+1
if abs(f(x0))<d:
break
print "the root of %i iteration is: %0.3g" %(c,x0)

successive iterations 	 x0 	        f(x0)	        f1(x0)

0.200000  	-0.056593    	1.753325

0.336526  	-0.002769    	1.583008

0.368804  	-0.000008    	1.573993

the root of 3 iteration is: 0.371


## Ex2.20:pg-42¶

In [57]:
#example 2.20
#generalized newton-raphson method
#page 42
import math
def f(x):
return x**3-x**2-x+1
def f1(x):
return 3*x**2-2*x-1
def f2(x):
return 6*x-2
x0=0.8   # initial value to finf double root
n=1
print "successive iterations   \t x0   \t      x1\t       x2\n"
while n==1:
x1=x0-(f(x0)/f1(x0));
x2=x0-(f1(x0)/f2(x0));
if abs(x1-x2)<0.000000001:
x0=(x1+x2)/2.0
break
else:
x0=(x1+x2)/2;
print "                          %f\t  %f\t  %f\n" %(x0,x1,x2)
print "\n \nthe double root is at: %f" %(x0)

successive iterations   	 x0   	      x1	       x2

0.974370	  0.905882	  1.042857

0.993890	  0.987269	  1.000512

0.998489	  0.996950	  1.000028

0.999623	  0.999245	  1.000002

0.999906	  0.999812	  1.000000

0.999976	  0.999953	  1.000000

0.999994	  0.999988	  1.000000

0.999999	  0.999997	  1.000000

1.000000	  0.999999	  1.000000

1.000000	  1.000000	  1.000000

1.000000	  1.000000	  1.000000

1.000000	  1.000000	  1.000000

1.000000	  1.000000	  1.000000

the double root is at: 1.000000


## Ex2.21:pg-45¶

In [3]:
#ramanujan's method
#example 2.21
#page 45
def f(x):
return 1-(13.0/12.0)*x-(3.0/8.0)*x**2+(1.0/24.0)*x**3
a1=13.0/12.0
a2=-3.0/8.0
a3=1.0/24.0
b1=1
b2=a1
b3=a1*b2+a2*b1
b4=a1*b3+a2*b2+a3*b1
b5=a1*b4+a2*b3+a3*b2
b6=a1*b5+a2*b4+a3*b3
b7=a1*b6+a2*b5+a3*b4
b8=a1*b7+a2*b6+a3*b5
b9=a1*b8+a2*b7+a3*b6
print "\n\n%f" %(b1/b2)
print "\n%f" %(b2/b3)
print "\n%f" %(b3/b4)
print "\n%f" %(b4/b5)
print "\n%f" %(b5/b6)
print "\n%f" %(b6/b7)
print "\n%f" %(b7/b8)
print "\n%f" %(b8/b9)
print "\n it appears as if the roots are converging at 2"


0.923077

1.356522

1.595376

1.738402

1.828184

1.886130

1.924153

1.949345

it appears as if the roots are converging at 2


## Ex2.22:pg-46¶

In [8]:
#ramanujan's method
#example 2.22
#page 46
def f(x):
return x+x**2+(x**3)/2.0+(x**4)/6.0+(x**5)/24.0
a1=1.0
a2=1.0
a3=1.0/2.0
a4=1.0/6.0
a5=1.0/24.0
b1=1
b2=a2
b3=a1*b2+a2*b1
b4=a1*b3+a2*b2+a3*b1
b5=a1*b4+a2*b3+a3*b2
b6=a1*b5+a2*b4+a3*b3
print "\n%f" %(b1/b2)
print "\n%f" %(b2/b3)
print "\n%f" %(b3/b4)
print "\n%f" %(b4/b5)
print "\n%f" %(b5/b6)
print "\n it appears as if the roots are converging at around %f" %(b5/b6)

1.000000

0.500000

0.571429

0.583333

0.571429

it appears as if the roots are converging at around 0.571429


## Ex2.23:pg-47¶

In [2]:
#ramanujan's method
#example 2.23
#page 47
from __future__ import division
def f(x):
return 1-2*((3*x/2.0+(x**2)/4.0-(x**4)/48.0+(x**6)/1440.0)-(x**8)*2/80640.0)
a1=3/2
a2=1/4
a3=0
a4=1/48
a5=0
a6=1/1440
a7=0
a8=-1/80640
b1=1
b2=a1
b3=a1*b2+a2*b1
b4=a1*b3+a2*b2+a3*b1
b5=a1*b4+a2*b3+a3*b2
b6=a1*b5+a2*b4+a3*b3
b7=a1*b6+a2*b5+a3*b4
b8=a1*b7+a2*b6+a3*b5
b9=a1*b8+a2*b7+a3*b6
print "\n%f" %(b1/b2)
print "\n%f" %(b2/b3)
print "\n%f" %(b3/b4)
print "\n%f" %(b4/b5)
print "\n%f" %(b5/b6)
print "\n%f" %(b6/b7)
print "\n%f" %(b7/b8)
print "\n it appears as if the roots are converging at around %f" %(b7/b8)

0.666667

0.600000

0.606061

0.605505

0.605556

0.605551

0.605551

it appears as if the roots are converging at around 0.605551


## Ex2.24:pg-47¶

In [3]:
#ramanujan's method
#example 2.24
#page 47
import math
def f(x):
return 1-(x-x**2.0/math.factorial(2.0)**2.0+x**3.0/math.factorial(3.0)**2.0-x**4.0/math.factorial(4.0)**2.0)
a1=1
a2=-1/math.factorial(2.0)**2.0
a3=1/math.factorial(3.0)**2.0
a4=-1/math.factorial(4.0)**2.0
a5=-1/math.factorial(5.0)**2.0
a6=1/math.factorial(6.0)**2.0
b1=1
b2=a1
b3=a1*b2+a2*b1
b4=a1*b3+a2*b2+a3*b1
b5=a1*b4+a2*b3+a3*b2
print "\n\n%f" %(b1/b2)
print "\n\n%f" %(b2/b3)
print "\n%f" %(b3/b4)
print "\n%f" %(b4/b5)
print "\n it appears as if the roots are converging at around %f" %(b4/b5)


1.000000

1.333333

1.421053

1.433962

it appears as if the roots are converging at around 1.433962


## Ex2.25:pg-49¶

In [29]:
#example 2.25
#secant method
#page 49
import math
from __future__ import division
def f(x):
return x**3-2*x-5
x1=2
x2=3   # initial values
n=1
c=0
print "successive iterations    \t x1          \t x2 \t       x3 \t        f(x3)\n"
while n==1:
x3=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1))
print "                         \t%f   \t%f  \t%f  \t%f\n" %(x1,x2,x3,f(x3))
if f(x3)*f(x1)>0:
x2=x3;
else:
x1=x3
if abs(f(x3))<0.000001:
break
c=c+1
print "the root of the equation after %i iteration is: %f" %(c,x3)

successive iterations    	 x1          	 x2 	       x3 	        f(x3)

2.000000   	3.000000  	2.058824  	-0.390800

2.000000   	2.058824  	2.096559  	0.022428

2.096559   	2.058824  	2.094511  	-0.000457

2.094511   	2.058824  	2.094552  	0.000009

2.094552   	2.058824  	2.094551  	-0.000000

the root of the equation after 4 iteration is: 2.094551


## Ex2.26:pg-50¶

In [31]:
#example 2.26
#secant method
#page 50
import math
from __future__ import division
def f(x):
return x*math.exp(x)-1
x1=0
x2=1 # initial values
n=1
c=0
print "successive iterations    \t x1          \t x2 \t       x3 \t        f(x3)\n"
while n==1:
x3=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1))
print "                          \t%f   \t%f  \t%f  \t%f\n" %(x1,x2,x3,f(x3))
if f(x3)*f(x1)>0:
x2=x3
else:
x1=x3
if abs(f(x3))<0.0001:
break
c=c+1
print "the root of the equation after %i iteration is: %0.4g" %(c,x3)

successive iterations    	 x1          	 x2 	       x3 	        f(x3)

0.000000   	1.000000  	0.367879  	-0.468536

0.000000   	0.367879  	0.692201  	0.383091

0.692201   	0.367879  	0.546310  	-0.056595

0.546310   	0.367879  	0.570823  	0.010200

0.570823   	0.367879  	0.566500  	-0.001778

0.566500   	0.367879  	0.567256  	0.000312

0.567256   	0.367879  	0.567124  	-0.000055

the root of the equation after 6 iteration is: 0.5671


## Ex2.27:pg-52¶

In [35]:
# example 2.27
#mulller's method
#page 52
from __future__ import division
import math
def f(x):
return x**3-x-1
x0=0
x1=1
x2=2        # initial values
n=1
c=0
print "successive iterations     \t x0     \t      x1 \t       x2 \t      f(x0)\t       f(x1)\t    f(x2)\n"
while n==1:
c=c+1
y0=f(x0)
y1=f(x1)
y2=f(x2)
h2=x2-x1
h1=x1-x0
d2=f(x2)-f(x1)
d1=f(x1)-f(x0)
print "                            \t%f\t  %f\t  %f\t  %f\t  %f\t  %f\n" %(x0,x1,x2,f(x0),f(x1),f(x2))
A=(d2/h2-d1/h1)/(h1+h2)
B=d2/h2+A*h2
S=math.sqrt(B**2-4*A*f(x2))
x3=x2-(2*f(x2))/(B+S)
E=abs((x3-x2)/x2)*100
if E<0.003:
break
else:
if c==1:
x2=x3
if c==2:
x1=x2
x2=x3
if c==3:
x0=x1
x1=x2
x2=x3
if c==3:
c=0
print "the required root is : %0.4f" %(x3)

successive iterations     	 x0     	      x1 	       x2 	      f(x0)	       f(x1)	    f(x2)

0.000000	  1.000000	  2.000000	  -1.000000	  -1.000000	  5.000000

0.000000	  1.000000	  1.263763	  -1.000000	  -1.000000	  -0.245412

0.000000	  1.263763	  1.331711	  -1.000000	  -0.245412	  0.030015

1.263763	  1.331711	  1.324583	  -0.245412	  0.030015	  -0.000574

1.263763	  1.331711	  1.324718	  -0.245412	  0.030015	  -0.000000

the required root is : 1.3247


## Ex2.28:pg-55¶

In [3]:
#graeffe's method
#example 2.28
#page 55
import math
from __future__ import division
def f(x):
return x**3-6*(x**2)+11*x-6
print "the equation is:\n"
A=[1, 14, 49, 36]   #coefficients of the above equation
print "%0.4g\n" %(math.sqrt(A[3]/A[2]))
print "%0.4g\n" %(math.sqrt(A[2]/A[1]))
print "%0.4g\n" %(math.sqrt(A[1]/A[0]))
print "the equation is:\n"
B=[1, 98, 1393, 1296]
print "%0.4g\n" %((B[3]/B[2])**(1/4))
print "%0.4g\n" %((B[2]/B[1])**(1/4))
print "%0.4g\n" %((B[1]/B[0])**(1/4))
print "It is apparent from the outputs that the roots converge at 1 2 3"

the equation is:

0.8571

1.871

3.742

the equation is:

0.9821

1.942

3.146

It is apparent from the outputs that the roots converge at 1 2 3


## Ex2.29:pg-57¶

In [7]:
#quadratic factor by lin's--bairsttow method
#example 2.29
#page 57
from numpy import matrix
from __future__ import division
def f(x):
return x**3-x-1
a=[-1, -1, 0, 1]
r1=1
s1=1
b4=a[3]
def f3(r):
return a[2]-r*a[3]
def f2(r,s):
return a[1]-r*a[2]+r**2*a[3]-s*a[3]
def f1(r,s):
return a[0]-s*a[2]+s*r*a[3]
A=matrix([[1,1],[2,-1]])
C=matrix([[0],[1]])
X=A.I*C
X1=[[ 0.33333333],[-0.33333333]]
dr=X1[0][0]
ds=X1[1][0]
r2=r1+dr
s2=s1+ds
#second pproximation
r1=r2
s1=s2
b11=f1(r2,s2)
b22=f2(r2,s2)
h=0.001
dr_b1=(f1(r1+h,s1)-f1(r1,s1))/h
ds_b1=(f1(r1,s1+h)-f1(r1,s1))/h
dr_b2=(f2(r1+h,s1)-f2(r1,s1))/h
ds_b2=(f2(r1,s1+h)-f2(r1,s1))/h
A=matrix([[dr_b1,ds_b1],[dr_b2,ds_b2]])
C=matrix([[-f1(r1,s1)],[-f2(r1,s2)]])
X=A.I*C
r2=r1+X[0][0]
s2=s1+X[1][0]
print "roots correct to 3 decimal places are : %0.3f       %0.3f" %(r2,s2)

roots correct to 3 decimal places are : 1.325       0.754


## Ex2.31:pg-62¶

In [9]:
#method of iteration
#example 2.31
#page 62
from __future__ import division
def f(x,y):
return (3*y*x**2+7)/10
def g(x,y):
return (y**2+4)/5
h=0.0001
x0=0.5
y0=0.5
f1_dx=(f(x0+h,y0)-f(x0,y0))/h
f1_dy=(f(x0,y0+h)-f(x0,y0))/h
g1_dx=(g(x0+h,y0)-g(x0,y0))/h
g1_dy=(g(x0+h,y0)-g(x0,y0))/h
if (f1_dx+f1_dy<1) and (g1_dx+g1_dy<1):
print "coditions for convergence is satisfied\n\n"
print "X \t              Y\t\n\n"
for i in range(0,10):
X=(3*y0*x0**2+7)/10
Y=(y0**2+4)/5
print "%f\t       %f\t\n" %(X,Y)
x0=X
y0=Y
print "\n\n CONVERGENCE AT (1 1) IS OBVIOUS"

coditions for convergence is satisfied

X 	              Y

0.737500	       0.850000

0.838696	       0.944500

0.899312	       0.978416

0.937391	       0.991460

0.961360	       0.996598

0.976320	       0.998642

0.985572	       0.999457

0.991247	       0.999783

0.994707	       0.999913

0.996807	       0.999965

CONVERGENCE AT (1 1) IS OBVIOUS


## Ex2.32:pg-65¶

In [6]:
#newton raphson metho
#example 2.32
#page 65
import numpy
def f(x,y):
return 3*y*x**2-10*x+7
def g(y):
return y**2-5*y+4
hh=0.0001
x0=0.5
y0=0.5  #initial values
f0=f(x0,y0)
g0=g(y0)
df_dx=(f(x0+hh,y0)-f(x0,y0))/hh
df_dy=(f(x0,y0+hh)-f(x0,y0))/hh
dg_dx=(g(y0)-g(y0))/hh
dg_dy=(g(y0+hh)-g(y0))/hh
d=[[df_dx,df_dy],[dg_dx,dg_dy]]
D1=numpy.linalg.det(d)
dd=[[-f0,df_dy],[-g0,dg_dy]]
h=numpy.linalg.det(dd)/D1
ddd=[[df_dx,-f0],[dg_dx,-g0]]
k=numpy.linalg.det(ddd)/D1;
x1=x0+h
y1=y0+k
f0=f(x1,y1)
g0=g(y1)
df_dx=(f(x1+hh,y1)-f(x1,y1))/hh
df_dy=(f(x1,y1+hh)-f(x1,y1))/hh
dg_dx=(g(y1)-g(y1))/hh
dg_dy=(g(y1+hh)-g(y1))/hh
dddd=[[df_dx,df_dy],[dg_dx,dg_dy]]
D2=numpy.linalg.det(dddd)
ddddd=[[-f0,df_dy],[-g0,dg_dy]]
h=numpy.linalg.det(ddddd)/D2
d6=[[df_dx,-f0],[dg_dx,-g0]]
k=numpy.linalg.det(d6)/D2
x2=x1+h
y2=y1+k
print " the roots of the equation are x2=%f and y2=%f" %(x2,y2)

 the roots of the equation are x2=0.970803 and y2=0.998752


## Ex2.33:pg-66¶

In [7]:
#newton raphson method
#example 2.33
#page 66
import math
import numpy
def f(x,y):
return x**2+y**2-1
def g(x,y):
return y-x**2
hh=0.0001
x0=0.7071
y0=0.7071     #initial values
f0=f(x0,y0)
g0=g(x0,y0)
df_dx=(f(x0+hh,y0)-f(x0,y0))/hh
df_dy=(f(x0,y0+hh)-f(x0,y0))/hh
dg_dx=(g(x0+hh,y0)-g(x0,y0))/hh
dg_dy=(g(x0,y0+hh)-g(x0,y0))/hh
D1=numpy.linalg.det([[df_dx,df_dy],[dg_dx,dg_dy]])
h=numpy.linalg.det([[-f0,df_dy],[-g0,dg_dy]])/D1
k=numpy.linalg.det([[df_dx,-f0],[dg_dx,-g0]])/D1
x1=x0+h
y1=y0+k
f0=f(x1,y1)
g0=g(x1,y1)
df_dx=(f(x1+hh,y1)-f(x1,y1))/hh
df_dy=(f(x1,y1+hh)-f(x1,y1))/hh
dg_dx=(g(x1+hh,y1)-g(x1,y1))/hh
dg_dy=(g(x1,y1+hh)-g(x1,y1))/hh
D2=numpy.linalg.det([[df_dx,df_dy],[dg_dx,dg_dy]])
h=numpy.linalg.det([[-f0,df_dy],[-g0,dg_dy]])/D2
k=numpy.linalg.det([[df_dx,-f0],[dg_dx,-g0]])/D2
x2=x1+h
y2=y1+k
print "the roots of the equation are x2=%f and y2=%f " %(x2,y2)

the roots of the equation are x2=0.786184 and y2=0.618039


## Ex2.34:pg-67¶

In [8]:
#newton raphson method
#example 2.34
#page 67
import math
import numpy
def f(x,y):
return math.sin(x)-y+0.9793
def g(x,y):
return math.cos(y)-x+0.6703
hh=0.0001
x0=0.5
y0=1.5   #initial values
f0=f(x0,y0)
g0=g(x0,y0)
df_dx=(f(x0+hh,y0)-f(x0,y0))/hh
df_dy=(f(x0,y0+hh)-f(x0,y0))/hh
dg_dx=(g(x0+hh,y0)-g(x0,y0))/hh
dg_dy=(g(x0,y0+hh)-g(x0,y0))/hh
d1=[[df_dx,df_dy],[dg_dx,dg_dy]]
D1=numpy.linalg.det(d1)
d2=[[-f0,df_dy],[-g0,dg_dy]]
h=numpy.linalg.det(d2)/D1
d3=[[df_dx,-f0],[dg_dx,-g0]]
k=numpy.linalg.det(d3)/D1
x1=x0+h
y1=y0+k
f0=f(x1,y1)
g0=g(x1,y1)
df_dx=(f(x1+hh,y1)-f(x1,y1))/hh
df_dy=(f(x1,y1+hh)-f(x1,y1))/hh
dg_dx=(g(x1+hh,y1)-g(x1,y1))/hh
dg_dy=(g(x1,y1+hh)-g(x1,y1))/hh
d4=[[df_dx,df_dy],[dg_dx,dg_dy]]
D2=numpy.linalg.det(d4)
h=numpy.linalg.det([[-f0,df_dy],[-g0,dg_dy]])/D2
k=numpy.linalg.det([[df_dx,-f0],[dg_dx,-g0]])/D2
x2=x1+h
y2=y1+k
print "the roots of the equation are x2=%0.4f and y2=%0.4f" %(x2,y2)

the roots of the equation are x2=0.6537 and y2=1.5874