Chapter 5 : Bracketing Methods

Ex5.1: Pg:120

In [6]:
from numpy import arange
from math import exp
%matplotlib inline
from matplotlib.pyplot import plot, title, xlabel, ylabel, show
m=68.1##kg
v=40##m/s
t=10##s
g=9.8##m/s**2
def f(c):
    y=g*m*(1-exp(-c*t/m))/c - v#
    return y
print "For various values of c and f(c) is found as:"
i=0#
Fc=[]
for c in arange(4,21,4):
    i=i+1#
    bracket=[c, f(c)]
    print bracket
    Fc.append(f(c))

c=arange(4,21,4)
plot(c,Fc)
title('f(c) vs c')
xlabel('c')
ylabel('f(c) (m/s)')
show()
For various values of c and f(c) is found as:
[4, 34.114844174677984]
[8, 17.653427509399428]
[12, 6.066935998372109]
[16, -2.2687619693477643]
[20, -8.400628721768179]

Ex5.2: Pg:123

In [3]:
from numpy import arange
from math import sin,cos
%matplotlib inline
from matplotlib.pyplot import plot, title, xlabel, ylabel, show

def f(x):
    y=sin(10*x)+cos(3*x)#
    return y
count=1#
func=[]
val=[]
for i in arange(0.55,1,0.05):
    val.append(i)
    func.append(f(i))
    count=count+1#

plot(val,func)
title("x vs f(x)")
xlabel('x')
ylabel('f(x)')
show()
for v in val:
    print '%0.2f'%v,'\t',
0.55 	0.60 	0.65 	0.70 	0.75 	0.80 	0.85 	0.90 	0.95 	

Ex5.3: Pg:125

In [ ]:
from math import exp
m=68.1##kg
v=40##m/s
t=10##s
g=9.8##m/s**2
def f(c):
    y=g*m*(1-exp(-c*t/m))/c - v#
    return y
x1=12#
x2=16#
xt=14.7802##true value
e=input("enter the tolerable true percent error=")
xr=(x1+x2)/2#
etemp=abs(xr-xt)/xt*100##error
while etemp>e:
    if f(x1)*f(xr)>0:
        x1=xr#
        xr=(x1+x2)/2#
        etemp=abs(xr-xt)/xt*100#
    
    if f(x1)*f(xr)<0:
        x2=xr#
        xr=(x1+x2)/2#
        etemp=abs(xr-xt)/xt*100#
    
    if f(x1)*f(xr)==0:
        break
    
print "The result is =",xr
enter the tolerable true percent error=1.24

Ex5.4: Pg:126

In [2]:
from math import exp
m=68.1##kg
v=40##m/s
t=10##s
g=9.8##m/s**2
def f(c):
    y=g*m*(1-exp(-c*t/m))/c - v#
    return y
x1=12#
x2=16#
xt=14.7802##true value
e=input("enter the tolerable approximate error=")
xr=(x1+x2)/2#
i=1#
et=abs(xr-xt)/xt*100##error
print "Iteration:",i
print "xl:",x1
print "xu:",x2
print "xr:",xr
print "et:",et,"%"
print "----------------------------------------"
etemp=100#
while etemp>e:
    if f(x1)*f(xr)>0:
        x1=xr
        xr=(x1+x2)/2
        etemp=abs(xr-x1)/xr*100
        et=abs(xr-xt)/xt*100
    
    if f(x1)*f(xr)<0:
            x2=xr
            xr=(x1+x2)/2
            etemp=abs(xr-x2)/xr*100
            et=abs(xr-xt)/xt*100
    
    if f(x1)*f(xr)==0:
        break#
    
    i=i+1#
    print "Iteration:",i
    print "xl:",x1
    print "xu:",x2
    print "xr:",xr
    print "et(%):",et,"%"
    print "ea",etemp,"%"
    print "----------------------------------------"


print "The result is=",xr
enter the tolerable approximate error=21.03
Iteration: 1
xl: 12
xu: 16
xr: 14
et: 5.27868364433 %
----------------------------------------
Iteration: 2
xl: 14
xu: 15
xr: 14
et(%): 5.27868364433 %
ea 0 %
----------------------------------------
The result is= 14

Ex5.5: Pg:133

In [3]:
from math import exp
m=68.1##kg
v=40##m/s
t=10##s
g=9.8##m/s**2
def f(c):
    y=g*m*(1-exp(-c*t/m))/c - v#
    return y
x1=12#
x2=16#
xt=14.7802##true value
e=input("enter the tolerable true percent error=")
xr=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1))#
etemp=abs(xr-xt)/xt*100##error
while etemp>e:
    if f(x1)*f(xr)>0:
        x1=xr
        xr=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1))
        etemp=abs(xr-xt)/xt*100
    
    if f(x1)*f(xr)<0:
            x2=xr
            xr=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1))
            etemp=abs(xr-xt)/xt*100
    
    if f(x1)*f(xr)==0:
        break
    

print "The result is=",xr
enter the tolerable true percent error=24.36
The result is= 14.9113031791

Ex5.6: Pg:135

In [1]:
def f(x):
    y=x**10 - 1#
    return y
x1=0#
x2=1.3#
xt=1#

#using bisection method
print "BISECTION METHOD:"
xr=(x1+x2)/2#
et=abs(xr-xt)/xt*100##error
print "Iteration:",1
print "xl:",x1
print "xu:",x2
print "xr:",xr
print "et(%):",et,"%"
print "----------------------------------------"
for i in range(2,6):
    if f(x1)*f(xr)>0:
        x1=xr
        xr=(x1+x2)/2
        ea=abs(xr-x1)/xr*100#
        et=abs(xr-xt)/xt*100#
    else:
        if f(x1)*f(xr)<0:
            x2=xr#
            xr=(x1+x2)/2#
            ea=abs(xr-x2)/xr*100#
            et=abs(xr-xt)/xt*100#
        
    
    if f(x1)*f(xr)==0:
        break
    
    print "Iteration:",i
    print "xl:",x1
    print "xu:",x2
    print "xr:",xr
    print "et(%):",et,"%"
    print "ea(%)",ea,"%"
    print "----------------------------------------"


#using false position method
print "FALSE POSITION METHOD:"
x1=0#
x2=1.3#
xt=1#
xr=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1))##
et=abs(xr-xt)/xt*100##error
print "Iteration:",1
print "xl:",x1
print "xu:",x2
print "xr:",xr
print "et(%):",et,"%"
print "----------------------------------------"
for i in range(2,6):
    if f(x1)*f(xr)>0:
        x1=xr#
        xr=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1))#
        ea=abs(xr-x1)/xr*100#
        et=abs(xr-xt)/xt*100#
    
    elif f(x1)*f(xr)<0:
            x2=xr#
            xr=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1))#
            ea=abs(xr-x2)/xr*100#
            et=abs(xr-xt)/xt*100#
        
    
    elif f(x1)*f(xr)==0:
        break#
    
    print "Iteration:",i
    print "xl:",x1
    print "xu:",x2
    print "xr:",xr
    print "et(%):",et,'%'
    print "ea(%)",ea,"%"
    print "----------------------------------------"
BISECTION METHOD:
Iteration: 1
xl: 0
xu: 1.3
xr: 0.65
et(%): 35.0 %
----------------------------------------
Iteration: 2
xl: 0.65
xu: 1.3
xr: 0.975
et(%): 2.5 %
ea(%) 33.3333333333 %
----------------------------------------
Iteration: 3
xl: 0.975
xu: 1.3
xr: 1.1375
et(%): 13.75 %
ea(%) 14.2857142857 %
----------------------------------------
Iteration: 4
xl: 0.975
xu: 1.1375
xr: 1.05625
et(%): 5.625 %
ea(%) 7.69230769231 %
----------------------------------------
Iteration: 5
xl: 0.975
xu: 1.05625
xr: 1.015625
et(%): 1.5625 %
ea(%) 4.0 %
----------------------------------------
FALSE POSITION METHOD:
Iteration: 1
xl: 0
xu: 1.3
xr: 0.0942995953723
et(%): 90.5700404628 %
----------------------------------------
Iteration: 2
xl: 0.0942995953723
xu: 1.3
xr: 0.181758872519
et(%): 81.8241127481 %
ea(%) 48.1182986748 %
----------------------------------------
Iteration: 3
xl: 0.181758872519
xu: 1.3
xr: 0.26287401252
et(%): 73.712598748 %
ea(%) 30.8570403075 %
----------------------------------------
Iteration: 4
xl: 0.26287401252
xu: 1.3
xr: 0.338105103322
et(%): 66.1894896678 %
ea(%) 22.2508001396 %
----------------------------------------
Iteration: 5
xl: 0.338105103322
xu: 1.3
xr: 0.407877916593
et(%): 59.2122083407 %
ea(%) 17.1062983388 %
----------------------------------------