Skip to content

Commit 67ed924

Browse files
committed
formated output
1 parent bade6a5 commit 67ed924

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

bisection.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def f(x):
1212
return (-32.17/(2*math.pow(x,2)))*(((math.exp(x)-math.exp(-x))/2) - math.sin(x)) - 1.7
1313

14-
def bisect(func, low, high, tol):
14+
def bisect(func, low, high, tol, N):
1515
# switch low and high if low is larger than high
1616
if low > high:
1717
low = temp
@@ -22,7 +22,7 @@ def bisect(func, low, high, tol):
2222
print "Check input for low and high guess (f(low) and f(high) must have different signs)"
2323

2424
lastFuncVal = func(low)
25-
while(True):
25+
for i in range(0, N):
2626
mid = (high+low)/2.0
2727
if abs(func(mid) - lastFuncVal)/abs(func(mid)) <= tol:
2828
return mid
@@ -33,5 +33,6 @@ def bisect(func, low, high, tol):
3333
else:
3434
high = mid
3535
lastFuncVal = func(mid)
36+
return "Method failed after {} iterations".format(N)
3637

37-
print bisect(f, -1, -.001, math.pow(10,-5))
38+
print "Bisection method soln:, x =", bisect(f, -1, -.001, math.pow(10,-5), 100)

fixedPoint.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
def f(x):
1010
return (1/math.tan(x)) - (1/x) + x
1111

12-
def fixedPoint(func, approx, tol):
13-
while True:
12+
def fixedPoint(func, approx, tol, N):
13+
for i in range(0, N):
1414
p = func(approx)
1515
if abs(p-approx) < tol:
1616
return p
1717
approx = p
18+
return "Method failed after {} iterations".format(N)
1819

19-
print fixedPoint(f, 4.6, 10**-4)
20+
print "Fixed point solution: x =", fixedPoint(f, 4.6, 10**-4, 100)

0 commit comments

Comments
 (0)