Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move Python Muller function into own file

  • Loading branch information...
commit e66bc108741a9392333b7f49ad84e3dd06b9bdaa 1 parent e9d6e29
@apauley authored
Showing with 31 additions and 32 deletions.
  1. +31 −0 Chapter1/Python/muller.py
  2. +0 −32 Chapter1/Python/numerical-methods.py
View
31 Chapter1/Python/muller.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+
+from helpers import *
+
+def mullers_method(func, a, b, r, max_steps=MaxSteps):
+ print_header("Muller's method", func)
+ x = [a,b,r]
+ for loopCount in range(max_steps):
+ x = swap_points(x)
+ y = evaluate(func,float(x[0])),evaluate(func,float(x[1])),evaluate(func,float(x[2]))
+ h1 = x[1]-x[0]
+ h2 = x[0]-x[2]
+ lam = h2/h1
+ c = y[0]
+ a = (lam*y[1] - y[0]*((1.0+lam))+y[2])/(lam*h1**2.0*(1+lam))
+ b = (y[1] - y[0] - a*((h1)**2.0))/(h1)
+ if b > 0:
+ root = x[0] - ((2.0*c)/(b+ (b**2 - 4.0*a*c)**0.5))
+ else:
+ root = x[0] - ((2.0*c)/(b- (b**2 - 4.0*a*c)**0.5))
+ print "a = %.5f b = %.5f c = %.5f root = %.5f " % (a,b,c,root)
+ print "Current approximation is %.9f" % root
+ if abs(evaluate(func,float(root))) > x[0]:
+ x = [x[1],x[0],root]
+ else:
+ x = [x[2],x[0],root]
+ x = swap_points(x)
+ print_end()
+
+if __name__ == '__main__':
+ mullers_method(example_f, 0.0, 0.5, 1.0)
View
32 Chapter1/Python/numerical-methods.py
@@ -3,31 +3,6 @@
import math
from helpers import *
-def mullers_method(func, a, b, r, max_steps=MaxSteps):
- print_header("Muller's method", func)
- x = [a,b,r]
- for loopCount in range(max_steps):
- x = swap_points(x)
- y = evaluate(func,float(x[0])),evaluate(func,float(x[1])),evaluate(func,float(x[2]))
- h1 = x[1]-x[0]
- h2 = x[0]-x[2]
- lam = h2/h1
- c = y[0]
- a = (lam*y[1] - y[0]*((1.0+lam))+y[2])/(lam*h1**2.0*(1+lam))
- b = (y[1] - y[0] - a*((h1)**2.0))/(h1)
- if b > 0:
- root = x[0] - ((2.0*c)/(b+ (b**2 - 4.0*a*c)**0.5))
- else:
- root = x[0] - ((2.0*c)/(b- (b**2 - 4.0*a*c)**0.5))
- print "a = %.5f b = %.5f c = %.5f root = %.5f " % (a,b,c,root)
- print "Current approximation is %.9f" % root
- if abs(evaluate(func,float(root))) > x[0]:
- x = [x[1],x[0],root]
- else:
- x = [x[2],x[0],root]
- x = swap_points(x)
- 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):
@@ -38,10 +13,3 @@ def newton_raphson(func, derFunc, initialApproximation, max_steps=MaxSteps, tole
print "Current approximation %.9f" % initialApproximation
initialApproximation = p
print_end()
-
-if __name__ == '__main__':
- try:
- print "in __main__"
- mullers_method(example_f, 0.0, 0.5, 1.0)
- finally:
- print "Fully done."
Please sign in to comment.
Something went wrong with that request. Please try again.