Skip to content

Commit ca117ab

Browse files
committed
Different method to find roots of a function
0 parents  commit ca117ab

7 files changed

+178
-0
lines changed

aitkens_method.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import math
2+
e = math.e
3+
pi = math.pi
4+
5+
accelerate = lambda x0, x1, x2: (x0*x2 - x1*x1)/(x0 + x2 - 2*x1)
6+
7+
def aitkens(f, p, n):
8+
initial_values = [0] * (n+2)
9+
for i in range(n+2):
10+
initial_values[i] = f(p)
11+
p = initial_values[i]
12+
print(initial_values)
13+
for i in range(n):
14+
result = accelerate(initial_values[i], initial_values[i+1], initial_values[i+2])
15+
print(f'P{i+1}:\t{result:.7f}')
16+
17+
if __name__ == '__main__':
18+
f = lambda x: math.cos(x)
19+
aitkens(f, 0.5, 5)

bisection_method.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import math
2+
3+
def bisect(f,a,b, TOL):
4+
if (f(a)*f(b) >= 0):
5+
print("You have not assumed the right interval.")
6+
return
7+
mid = a
8+
iterations = 0
9+
while ((b-a) >= TOL):
10+
mid = (a+b)/2
11+
result = f(mid)
12+
# Check if middle point is root
13+
if (result == 0):
14+
break
15+
# Decide the side to repeat the steps
16+
if (result*f(a) < 0):
17+
b = mid
18+
else:
19+
a = mid
20+
iterations += 1
21+
print(f'P{iterations}: {mid:.5f}\tResult: {result}')
22+
print(f'Root: {mid:.7f}\nIterations: {iterations}')
23+
24+
if __name__ == '__main__':
25+
f = lambda x: 0.331 - math.asin(x) - (x*(1-x*x)**0.5)
26+
bisect(f, 0, 1, 0.01)

fixed_point.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import math
2+
3+
def fixed_point_iteration(f, p, TOL):
4+
error = 1
5+
iterations = 0
6+
while error > TOL:
7+
p_new = f(p)
8+
error = abs(p_new - p)
9+
p = p_new
10+
iterations += 1
11+
print(f'p{iterations} = {p: 0.5f}')
12+
print(f'Root: {p}\nIterations: {iterations}')
13+
14+
if __name__ == '__main__':
15+
f = lambda x: (math.sin(x) + math.cos(x))/2
16+
fixed_point_iteration(f, 0, 1e-5)

muellers_method.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
MAX_ITERATIONS = 100
2+
3+
def muellers(f, p0, p1, p2, TOL):
4+
iterations = 3
5+
error = 1
6+
print(f'Iteration\t\tPi')
7+
while error > TOL and iterations < MAX_ITERATIONS:
8+
9+
h1 = p1-p0
10+
h2 = p2-p1
11+
del1 = (f(p1)-f(p0))/h1
12+
del2 = (f(p2)-f(p1))/h2
13+
d = (del2-del1)/(h2+h1)
14+
c = f(p2)
15+
16+
b = del2 + h2*d
17+
discriminant = (b*b - 4*c*d)**0.5
18+
19+
E = b+discriminant if abs(b-discriminant) < abs(b+discriminant) else b-discriminant
20+
21+
proximity = -2*c/E
22+
p = p2+proximity
23+
24+
print(f'{iterations:^5}\t\t{p:.5f}')
25+
26+
error = abs(proximity)
27+
28+
p0 = p1
29+
p1 = p2
30+
p2 = p
31+
iterations += 1
32+
33+
print("Incorrect initial points. Try again." if iterations > MAX_ITERATIONS else "Exiting...")
34+
35+
if __name__ == '__main__':
36+
#f = lambda x: x**4 + 2*x**2 - x - 3
37+
f = lambda x: x**5 - x**4 + 2*x**3 - 3*x**2 + x - 4
38+
muellers(f, 1, 0, -1, 1e-5)

newton_raphson.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import math
2+
e = math.e
3+
pi = math.pi
4+
5+
def newton_raphson(f, df, x, TOL):
6+
error = 1
7+
iterations = 0
8+
while error > TOL:
9+
new_x = x - f(x)/df(x)
10+
error = abs(new_x - x)
11+
x = new_x
12+
iterations += 1
13+
print(f'x{iterations}: {x}')
14+
print(f"Newton's Estimate = {x:.15f}\nIterations: {iterations}")
15+
16+
def modified_newton(f, df, ddf, x, TOL):
17+
error = 1
18+
iterations = 0
19+
while error > TOL:
20+
f_x = f(x)
21+
d_x = df(x)
22+
new_x = x - (f_x*d_x)/(d_x*d_x - f_x*ddf(x))
23+
error = abs(new_x - x)
24+
x = new_x
25+
iterations += 1
26+
print(f'x{iterations}: {x}')
27+
print(f"Modified Newton Estimate = {x:.15f}\nIterations: {iterations}")
28+
29+
def fibonacci_estimate():
30+
values = [1, 22, 7, 42, 33, 4, 40]
31+
total = 0
32+
for i in range(len(values)):
33+
total += values[i]/(60**i)
34+
print(f"Fibonacci's Estimate = {total:.15f}")
35+
36+
if __name__ == '__main__':
37+
f = lambda x: x**4 - 2*x**3 - 12*x*x + 16*x - 40
38+
df = lambda x: 4*x**3 - 6*x*x - 24*x + 16
39+
ddf = lambda x: 12*x*x - 12*x - 24
40+
newton_raphson(f, df, 1, 1e-5)
41+
modified_newton(f, df, ddf, 1, 1e-5)

secant_method.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import math
2+
3+
def secant(f, x1, x2, TOL):
4+
error = 1
5+
iterations = 0
6+
print(f'x0: {x1}\nx1: {x2}')
7+
while error > TOL:
8+
x_new = x2 - (f(x2)*(x2-x1))/(f(x2)-f(x1))
9+
error = abs(x_new - x2)
10+
x1 = x2
11+
x2 = x_new
12+
iterations += 1
13+
print(f'x{iterations+1}: {x2}')
14+
print(f'Result: {x2}\nIterations: {iterations}')
15+
16+
if __name__ == '__main__':
17+
f = lambda x: x - math.cos(x)
18+
secant(f, 0, math.pi/2, 1e-4)

steffensens_method.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import math
2+
3+
accelerate = lambda x0, x1, x2: (x0*x2 - x1*x1)/(x0 + x2 - 2*x1)
4+
5+
def steffensens(f, p, TOL):
6+
error = 1
7+
iterations = 0
8+
while error > TOL:
9+
p1 = f(p)
10+
p2 = f(p1)
11+
p_new = accelerate(p, p1, p2)
12+
error = abs(p_new-p)
13+
iterations += 1
14+
print(f'P0: {p:.7f}\nP1: {p1:.7f}\nP2: {p2:.7f}\nP-hat_{iterations}: {p_new:.7f}\n')
15+
p = p_new
16+
print(f'Final value: {p}\nIterations: {iterations}')
17+
18+
if __name__ == '__main__':
19+
f = lambda x: 5**-x
20+
steffensens(f, 0, 5e-2)

0 commit comments

Comments
 (0)