Skip to content

Commit 0263306

Browse files
committed
Split into 4 scripts; increased usability of the scripts allowing for easier control over the variables involved (tolerance, Max iterations, function,etc.)
1 parent 3212543 commit 0263306

File tree

4 files changed

+72
-38
lines changed

4 files changed

+72
-38
lines changed

bisection.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
#!/usr/bin/env python2.7
22

33
# 9/28/2017
4-
# Finds the root, or solution, of the form f(x) = 0. It uses:
4+
# Finds the root, or solution, of the form f(x) = 0. f(x) is a continuous
5+
# function defined on an interval [a,b] and where f(a) and f(b) have
6+
# opposite signs [f(a)*f(b) < 0]
7+
# Also, uses:
58
# abs((w(n)-w(n-q))/w(n)) <= 10^-5 as the tolerance
69

710
import math
811

12+
# globals
13+
TOL = 10**-5
14+
A = 1
15+
B = 2
16+
N = 100
17+
918
# give python eqn of the formula using python math syntax
1019
def f(x):
11-
return (-32.17/(2*math.pow(x,2)))*(((math.exp(x)-math.exp(-x))/2) - math.sin(x)) - 1.7
20+
return (math.pow(x,3) - x - 2)
1221

22+
# INPUT: function (func), low guess (a), high guess (b), tolerance (tol),
23+
# MAX iterations (N)
24+
# CONDITIONS: a < b, f(a)*f(b) < 0
25+
# OUTPUT: value which differs from a root of f(x)=0 by less than 'tol'
1326
def bisect(func, low, high, tol, N):
1427
# switch low and high if low is larger than high
1528
if low > high:
@@ -23,7 +36,7 @@ def bisect(func, low, high, tol, N):
2336
lastFuncVal = func(low)
2437
for i in range(0, N):
2538
mid = (high+low)/2.0
26-
if abs(func(mid) - lastFuncVal)/abs(func(mid)) <= tol:
39+
if func(mid) == 0 or (high-low)/2.0 < tol:
2740
return mid
2841
# same sigh
2942
if func(mid)*func(low) > 0:
@@ -34,4 +47,4 @@ def bisect(func, low, high, tol, N):
3447
lastFuncVal = func(mid)
3548
return "Method failed after {} iterations".format(N)
3649

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

fixedPoint.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
#!/usr/bin/env python2.7
22

3-
# Will Fritz
43
# 9/28/2017
54
# Finds the root of equation in the form of x = f(x) or g(x) = x - f(x) by using the fixed point method
65
import math
76

7+
# globals
8+
APPROX = 4.6
9+
TOL = 10**-4
10+
N = 100
11+
812
# function to test using python syntax in form g(x) = x - f(x)
913
def f(x):
1014
return (1/math.tan(x)) - (1/x) + x
1115

12-
def fixedPoint(func, approx, tol, N):
13-
for i in range(0, N):
16+
def fixedPoint(func, approx, tol, n):
17+
for i in range(0, n):
1418
p = func(approx)
1519
if abs(p-approx) < tol:
1620
return p
1721
approx = p
18-
return "Method failed after {} iterations".format(N)
22+
return "Method failed after {} iterations".format(n)
1923

20-
print "Fixed point solution: x =", fixedPoint(f, 4.6, 10**-4, 100)
24+
print "Fixed point soln: x = ", fixedPoint(f, APPROX, TOL, N)

newtons.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
11
#!/usr/bin/env python2.7
22

3-
# Will Fritz
43
# 9/29/2017
5-
# program to find solution to equation, namely f(x), using newton's method
6-
# and secant method
4+
# program to find solution to equation, namely f(x) = 0, using newton's
5+
# method
6+
77
import math
88
import time
99

10+
# globals
11+
APPROX = 0.1
12+
TOL = 10**-5
13+
N = 100
14+
15+
# functions we are using, namely f and f'
1016
def f(x):
1117
return math.pow((1+x),204)-440*x-1
1218

1319
def fprime(x):
14-
return 204*pow((x+1),203) - 500
20+
return 204*math.pow((x+1),203) - 440
1521

16-
def newtons(approx, tol, N):
22+
# Actual Newton's Method
23+
def newtons(approx, tol, n):
1724
p0 = approx
18-
for i in range(0, N):
19-
p = p0 - (f(p0)/fprime(p0))
25+
for i in range(0, n):
26+
p = p0 - (f(p0)/fprime(p0)) # this is the calculation of the guess
2027
if abs(p - p0) < tol:
2128
return p
2229
p0 = p
23-
return "The method failed after {} iterations".format(N)
24-
25-
def secant(p0, p1, tol, N):
26-
for i in range(0, N):
27-
p = p1 - ((f(p1)*(p1-p0))/(f(p1) - f(p0)))
28-
if abs(p-p1) < tol:
29-
return p
30-
p0 = p1
31-
p1 = p
32-
return "Them method failed after {} iterations">format(N)
33-
34-
startTime = time.time()
35-
output = newtons(.1, pow(10, -5), 100)
36-
elapsedN = time.time() - startTime
37-
startTime2 = time.time()
38-
output1 = secant(.1,.09,pow(10,-5),100)
39-
elapsedS = time.time() - startTime2
40-
41-
print "Newtons method soln: x =", output
42-
print "Newtons time in seconds: {}".format(elapsedN)
30+
return "The method failed after {} iterations".format(n)
4331

44-
print "Secant method soln: x =", output1
45-
print "Secant time in seconds: {}".format(elapsedS)
32+
# Call the method and print its answer
33+
output = newtons(APPROX, TOL, N)
34+
print "Newton's method soln: x = ", output

secant.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python2.7
2+
3+
# 9/12/2018
4+
# finds the solution to f(x) = 0 using secant method
5+
6+
import math
7+
8+
# globals
9+
APPROX = 0.1
10+
TOL = 10**-5
11+
N = 100
12+
13+
# function we are using
14+
def f(x):
15+
return math.pow((1+x),204)-440*x-1
16+
17+
# Actual Secant Method
18+
def secant(p0, p1, tol, N):
19+
for i in range(0, N):
20+
p = p1 - ((f(p1)*(p1-p0))/(f(p1) - f(p0)))
21+
if abs(p-p1) < tol:
22+
return p
23+
p0 = p1
24+
p1 = p
25+
return "Them method failed after {} iterations">format(N)
26+
27+
28+
print "Secant method soln: x = ", secant(.1,.09,pow(10,-5),100)

0 commit comments

Comments
 (0)