Permalink
Browse files

Move Python bisection function into own file

  • Loading branch information...
Andreas Pauley
Andreas Pauley committed Sep 2, 2010
1 parent ddf7653 commit ae6c47ef6d7a24d923fb35c876975cc5b76a5d6e
Showing with 20 additions and 14 deletions.
  1. +20 −0 Chapter1/Python/bisection.py
  2. +0 −14 Chapter1/Python/numerical-methods.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+
+from helpers import *
+
+def bisection(func, a, b, max_steps=MaxSteps):
+ print_header("Bisection Method", func)
+ initial = evaluate(func,float(a))
+ for loopCounter in range(max_steps):
+ midPoint = a + (b-a)/2.0
+ result = evaluate(func,float(midPoint))
+ print "Accuracy is within %.9f " % (abs(b-a)/2.0)
+ if (result == 0 or ( (abs(b-a)/2.0) > 0)):
+ a = midPoint
+ initial = result
+ else:
+ b = midPoint
+ print_end()
+
+if __name__ == '__main__':
+ bisection(example_f, 0, 1)
@@ -31,20 +31,6 @@ def mullers_method(func, a, b, r, max_steps=MaxSteps):
x = swap_points(x)
print_end()
-def bisection(func, a, b, max_steps=MaxSteps):
- print_header("Bisection Method", func)
- initial = evaluate(func,float(a))
- for loopCounter in range(max_steps):
- midPoint = a + (b-a)/2.0
- result = evaluate(func,float(midPoint))
- print "Accuracy is within %.9f " % (abs(b-a)/2.0)
- if (result == 0 or ( (abs(b-a)/2.0) > 0)):
- a = midPoint
- initial = result
- else:
- b = midPoint
- print_end()
-
def newton_raphson(func, derFunc, initialApproximation, max_steps=MaxSteps, tolerance=Tolerance):
print_header("newton/raphson",func + " with " + defFunc + " as derivative")
for loopCounter in range(max_steps):

0 comments on commit ae6c47e

Please sign in to comment.