Skip to content

Commit a339549

Browse files
committed
a little cleaning
1 parent e0905ee commit a339549

2 files changed

Lines changed: 31 additions & 34 deletions

File tree

basic_numerics/roots/roots.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# use bisection, Newton's method, or secant method to find roots
2-
#
3-
# M. Zingale (2013-02-14)
1+
"""
2+
use bisection, Newton's method, or secant method to find roots
3+
4+
M. Zingale
5+
"""
46

57
from __future__ import print_function
68

@@ -29,7 +31,7 @@ def hprime(x):
2931
return 2.0*x
3032

3133

32-
class root:
34+
class Root(object):
3335
""" simple class to manage root finding. All method take in a
3436
function and desired tolerance """
3537

@@ -39,7 +41,6 @@ def __init__(self, fun, tol, fprime=None):
3941

4042
self.tol = tol
4143

42-
4344
def bisection(self, xl, xr):
4445
""" find the root using bisection. xl and xr should bracket
4546
the root """
@@ -50,7 +51,7 @@ def bisection(self, xl, xr):
5051
# initial evaluations
5152
fl = self.f(xl)
5253
fr = self.f(xr)
53-
54+
5455
xeval.append(xl)
5556
xeval.append(xr)
5657

@@ -60,10 +61,10 @@ def bisection(self, xl, xr):
6061
return None
6162

6263
err = abs(xr - xl)
63-
while (err > self.tol):
64+
while err > self.tol:
6465
xm = 0.5*(xl + xr)
6566
fm = self.f(xm)
66-
67+
6768
xeval.append(xm)
6869

6970
if fm*fl >= 0:
@@ -80,7 +81,6 @@ def bisection(self, xl, xr):
8081

8182
return 0.5*(xl + xm), xeval
8283

83-
8484
def newton(self, x0):
8585
""" find the root via Newton's method. x0 is the initial guess
8686
for the root """
@@ -91,15 +91,14 @@ def newton(self, x0):
9191
dx = -self.f(x0)/self.fprime(x0)
9292
xeval.append(x0)
9393
x = x0 + dx
94-
95-
while (abs(dx) > self.tol):
96-
dx = -self.f(x)/self.fprime(x)
94+
95+
while abs(dx) > self.tol:
96+
dx = -self.f(x)/self.fprime(x)
9797
xeval.append(x)
9898
x += dx
9999

100100
return x, xeval
101101

102-
103102
def secant(self, xm1, x0):
104103
""" find the root via Newton's method. xm1 and x0 are two
105104
initial guesses close to the root"""
@@ -111,29 +110,26 @@ def secant(self, xm1, x0):
111110

112111
# loop. xm1 will always carry the previous estimate for the
113112
# root
114-
while (abs(dx) > self.tol):
113+
while abs(dx) > self.tol:
115114
dx = -self.f(x)*(x - xm1)/(self.f(x) - self.f(xm1))
116115
xm1 = x
117116
x += dx
118117

119-
120118
return x
121119

122120

123121
def main():
124-
r = root(h, 1.e-6, fprime=hprime)
122+
"""a simple test driver"""
123+
124+
r = Root(h, 1.e-6, fprime=hprime)
125125
#rootb, xeval = r.bisection(0.0, 10.0)
126126
rootn, xeval = r.newton(10.0)
127127
#roots = r.secant(10.0, 9.0)
128-
128+
129129
#print "Bisection: ", rootb
130130
print("Newton: ", rootn, xeval)
131131
#print "Secant: ", roots
132132

133133

134134
if __name__ == "__main__":
135135
main()
136-
137-
138-
139-

basic_numerics/roots/roots_plot.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
# make plots using the root finding methods in roots.py
2-
#
3-
# M. Zingale (2013-02-14)
1+
"""
2+
make plots using the root finding methods in roots.py
3+
4+
M. Zingale (2013-02-14)
5+
"""
46

57
from __future__ import print_function
68

7-
import math
89
import numpy
910
import matplotlib as mpl
1011
import matplotlib.pyplot as plt
11-
from roots import *
12+
import roots
1213
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset
1314

1415
mpl.rcParams['xtick.labelsize'] = 16
1516
mpl.rcParams['ytick.labelsize'] = 16
1617
mpl.rcParams['mathtext.fontset'] = 'cm'
1718
mpl.rcParams['mathtext.rm'] = 'serif'
1819

19-
r = root(f, 1.e-5, fprime=fprime)
20+
r = roots.Root(roots.f, 1.e-5, fprime=roots.fprime)
2021

2122
xmin = 0.0
2223
xmax = 5.0
@@ -35,8 +36,8 @@
3536
plt.plot(xfine, r.f(xfine))
3637

3738
plt.scatter(numpy.array(xeval[0:n+1]),
38-
r.f(numpy.array(xeval[0:n+1])),
39-
marker="x", s=25, color="r", zorder=100)
39+
r.f(numpy.array(xeval[0:n+1])),
40+
marker="x", s=25, color="r", zorder=100)
4041

4142
plt.plot(xfine, r.fprime(x)*(xfine-x) + r.f(x), color="0.5")
4243

@@ -45,16 +46,16 @@
4546
plt.plot([xintercept, xintercept], [0, r.f(xintercept)], color="0.5", ls=":")
4647

4748
if n%2 == 0:
48-
plt.text(x, r.f(x)-0.3, "{}".format(n),
49+
plt.text(x, r.f(x)-0.3, "{}".format(n),
4950
color="r", fontsize="16",
5051
verticalalignment="top", horizontalalignment="center", zorder=1000)
5152
else:
52-
plt.text(x, r.f(x)+0.3, "{}".format(n),
53+
plt.text(x, r.f(x)+0.3, "{}".format(n),
5354
color="r", fontsize="16",
5455
verticalalignment="bottom", horizontalalignment="center", zorder=1000)
5556

5657
F = plt.gcf()
57-
plt.text(0.4, 0.02, "root approx = {}".format(x),
58+
plt.text(0.4, 0.02, "root approx = {}".format(x),
5859
transform = F.transFigure, color="k", fontsize="16")
5960

6061
# axes through origin
@@ -94,12 +95,12 @@
9495
axins.plot([xintercept, xintercept], [0, r.f(xintercept)], color="0.5", ls=":")
9596

9697
if n%2 == 0:
97-
axins.text(x, r.f(x)-0.05, "{}".format(n),
98+
axins.text(x, r.f(x)-0.05, "{}".format(n),
9899
color="r", fontsize="10",
99100
verticalalignment="top", horizontalalignment="center",
100101
clip_on=True, zorder=100)
101102
else:
102-
axins.text(x, r.f(x)+0.05, "{}".format(n),
103+
axins.text(x, r.f(x)+0.05, "{}".format(n),
103104
color="r", fontsize="10",
104105
verticalalignment="bottom", horizontalalignment="center",
105106
clip_on=True, zorder=100)

0 commit comments

Comments
 (0)