#Kevin Arvai

#Bisection_Method.py

#provided #f(x) = (-2.0*xr**6.0) – (1.6*xr**4.0) + 12.0*xr + 1.0

#xl is lower; xu is upper
xl = 0.0
xu = 1.0
#error
es = .05
#start at zero, should finish before 50
iter = 0.0
itermax = 50.0

fl = (-12.0*xl**5.0) – (6.4*xl**3.0) + 12.0
xr = xl

while iter >= 0:

xrold = xr
xr = (xl + xu) / 2
#fr = (-2.0*xr**6.0) – (1.6*xr**4.0) + 12.0*xr + 1.0
fr = (-12.0*xr**5.0) – (6.4*xr**3.0) + 12.0
iter = iter +1
print “iteration: ” + str(iter)
if (xr != 0):
ea = 100*abs((xr-xrold)/xr)
print ‘error: ‘ + str(ea)
test = fl*fr
print “test: ” + str(test)

if (test < 0):
xu = xr

if(test > 0):
xl = xr
fl = fr

if test == 0:
ea = 0

if (ea < es):
break

if (iter >= itermax):
break
print “xr: ” + str(xr)
print “\n”
print “final xr: ” + str(xr)

print “max: ” + str((-12.0*xr**5.0) – (6.4*xr**3.0) + 12.0)

Advertisements

One thought on “Bisection_Method.py

Comment Here:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s