Permalink
Browse files

Move Python regula falsi function into own file

  • Loading branch information...
1 parent 789ba04 commit ddf765358ad92cfba626afea248cecbd8121e159 @apauley committed Sep 2, 2010
Showing with 22 additions and 16 deletions.
  1. +0 −16 Chapter1/Python/numerical-methods.py
  2. +22 −0 Chapter1/Python/regula-falsi.py
@@ -45,22 +45,6 @@ def bisection(func, a, b, max_steps=MaxSteps):
b = midPoint
print_end()
-def regula_falsi(func, a, b, max_steps=MaxSteps, tolerance=Tolerance):
- print_header("regula falsi (false position)",func)
- p = 0.0
- for loopCount in range(max_steps):
- p = b - (evaluate(func,float(b)) * ((a-b)/(evaluate(func,float(a))-evaluate(func,float(b)))))
- print "Current approximation is %.9f" % p
- if (math.copysign(evaluate(func,float(a)),evaluate(func,float(b))) != evaluate(func,float(a))):
- b = p
- else:
- a = p
- if (abs(evaluate(func,float(p))) < tolerance):
- print "Root is %.9f (%d iterations)" % (p,int(loopCount))
- return
- print "Root find cancelled at %.9f" % p
- 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,0 +1,22 @@
+#!/usr/bin/env python
+
+from helpers import *
+
+def regula_falsi(func, a, b, max_steps=MaxSteps, tolerance=Tolerance):
+ print_header("regula falsi (false position)",func)
+ p = 0.0
+ for loopCount in range(max_steps):
+ p = b - (evaluate(func,float(b)) * ((a-b)/(evaluate(func,float(a))-evaluate(func,float(b)))))
+ print "Current approximation is %.9f" % p
+ if (math.copysign(evaluate(func,float(a)),evaluate(func,float(b))) != evaluate(func,float(a))):
+ b = p
+ else:
+ a = p
+ if (abs(evaluate(func,float(p))) < tolerance):
+ print "Root is %.9f (%d iterations)" % (p,int(loopCount))
+ return
+ print "Root find cancelled at %.9f" % p
+ print_end()
+
+if __name__ == '__main__':
+ regula_falsi(example_f, 0, 1)

0 comments on commit ddf7653

Please sign in to comment.