-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewton's Method.py
55 lines (44 loc) · 1.1 KB
/
Newton's Method.py
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
__author__ = 'Wangj1'
# Zn+1 = Zn - (Z^4 - 1)/4Z^3
import graphics
def newton(z):
try:
new_z = z - (z**4 - 1)/(4 * z**3)
except ZeroDivisionError:
return z
return new_z
def dbtp(x1, y1, x2, y2):
return ((x2 - x1)**2 + (y2 - y1)**2)**0.5
def distance(z):
real = z.real
imag = z.imag
d1 = dbtp(real, imag, 1, 0)
d2 = dbtp(real, imag, 0, -1)
d3 = dbtp(real, imag, -1, 0)
d4 = dbtp(real, imag, 0, 1)
dmin = min(d1, d2, d3, d4)
if dmin == d1:
return "red"
if dmin == d2:
return "blue"
if dmin == d3:
return "yellow"
if dmin == d4:
return "green"
return "white"
width = 800
height = 800
iteration = 50
win = graphics.GraphWin("Newton's Law Exploer", width, height, autoflush=False)
win.setCoords(-400, -400, 400, 400)
for i in range(width):
for j in range(height):
a = (i - 400.0)/200.0
b = (j - 400.0)/200.0
z = complex(a, b)
for c in range(iteration):
z = newton(z)
color = distance(z)
win.plot(i - 400, j - 400, color)
win.getMouse()
win.close()