Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Move Python regula falsi function into own file

  • Loading branch information...
commit ddf765358ad92cfba626afea248cecbd8121e159 1 parent 789ba04
Andreas Pauley authored
16 Chapter1/Python/numerical-methods.py
@@ -45,22 +45,6 @@ def bisection(func, a, b, max_steps=MaxSteps):
45 45 b = midPoint
46 46 print_end()
47 47
48   -def regula_falsi(func, a, b, max_steps=MaxSteps, tolerance=Tolerance):
49   - print_header("regula falsi (false position)",func)
50   - p = 0.0
51   - for loopCount in range(max_steps):
52   - p = b - (evaluate(func,float(b)) * ((a-b)/(evaluate(func,float(a))-evaluate(func,float(b)))))
53   - print "Current approximation is %.9f" % p
54   - if (math.copysign(evaluate(func,float(a)),evaluate(func,float(b))) != evaluate(func,float(a))):
55   - b = p
56   - else:
57   - a = p
58   - if (abs(evaluate(func,float(p))) < tolerance):
59   - print "Root is %.9f (%d iterations)" % (p,int(loopCount))
60   - return
61   - print "Root find cancelled at %.9f" % p
62   - print_end()
63   -
64 48 def newton_raphson(func, derFunc, initialApproximation, max_steps=MaxSteps, tolerance=Tolerance):
65 49 print_header("newton/raphson",func + " with " + defFunc + " as derivative")
66 50 for loopCounter in range(max_steps):
22 Chapter1/Python/regula-falsi.py
... ... @@ -0,0 +1,22 @@
  1 +#!/usr/bin/env python
  2 +
  3 +from helpers import *
  4 +
  5 +def regula_falsi(func, a, b, max_steps=MaxSteps, tolerance=Tolerance):
  6 + print_header("regula falsi (false position)",func)
  7 + p = 0.0
  8 + for loopCount in range(max_steps):
  9 + p = b - (evaluate(func,float(b)) * ((a-b)/(evaluate(func,float(a))-evaluate(func,float(b)))))
  10 + print "Current approximation is %.9f" % p
  11 + if (math.copysign(evaluate(func,float(a)),evaluate(func,float(b))) != evaluate(func,float(a))):
  12 + b = p
  13 + else:
  14 + a = p
  15 + if (abs(evaluate(func,float(p))) < tolerance):
  16 + print "Root is %.9f (%d iterations)" % (p,int(loopCount))
  17 + return
  18 + print "Root find cancelled at %.9f" % p
  19 + print_end()
  20 +
  21 +if __name__ == '__main__':
  22 + regula_falsi(example_f, 0, 1)

0 comments on commit ddf7653

Please sign in to comment.
Something went wrong with that request. Please try again.