-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBisection.py
More file actions
84 lines (58 loc) · 1.68 KB
/
Bisection.py
File metadata and controls
84 lines (58 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import numpy as np
import matplotlib.pyplot as plt
E = np.linspace(0.0, 20.0, 100)
w = 1e-9 # width of quantum well in nm
V = 20 # volts in ev
Tolerance = 1e-3
#Constants
MassOfElectron=9.1094e-31
ElectronVolt=1.602e-19
h_bar=1.0545e-34
#numerator/Denom
Top = ((w ** 2)*((MassOfElectron)))/ElectronVolt
Bottom=(2*((h_bar/ElectronVolt)**2))
Values=Top/Bottom
#Functions
def Tan(Energy):
return np.tan(np.sqrt(Values * Energy))
def Even(Energy):
return np.sqrt((V - Energy) / Energy)
def Odd(Energy):
return (-np.sqrt(Energy / (V - Energy)))
#plotting functions
y1 = Tan(E)
y2 = Even(E)
y3 = Odd(E)
plt.plot(E,y1, label='Tan')
plt.plot( E,y2, label='Even')
plt.plot(E,y3,label='Odd')
plt.legend()
plt.title("Functions")
plt.show()
#Bisection
def TanOfEven(x):
return Tan(x) - Even(x)
def TanOfOdd(x):
return Tan(x) - Odd(x)
def crit(f, x1, x2, Tolerance):
def midpoint(x, y):
return (x + y) / 2
def sign(x, y):
if (x < 0 and y < 0) or (x > 0 and y > 0):
return True
else:
return False
while abs(x1 - x2) > Tolerance:
x = midpoint(x1, x2)
if sign(f(x1), f(x)):
x1 = x
elif sign(f(x), f(x2)):
x2 = x
elif abs(x) < Tolerance:
return x
return midpoint(x1, x2)
print('Test between points 1 & 5= ', crit(TanOfEven, 1, 5, Tolerance))
print('3 to 5', crit(TanOfEven, 3, 5, Tolerance))
print('9 to 12', crit(TanOfEven, 9, 12, Tolerance))
print('13 to 17', crit(TanOfEven, 13, 17, Tolerance))
print( '18 to 20', crit(TanOfEven, 18, 20, Tolerance))