-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw5_5-7.py
More file actions
52 lines (34 loc) · 841 Bytes
/
hw5_5-7.py
File metadata and controls
52 lines (34 loc) · 841 Bytes
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
import numpy as np
import time
def E(u, v):
return (u*np.exp(v) - 2*v*np.exp(-u))**2
def d_E_d_u(u, v):
return 2*(u*np.exp(v)-2*v*np.exp(-u))*(np.exp(v)+2*v*np.exp(-u))
def d_E_d_v(u, v):
return 2*(u*np.exp(v)-2*v*np.exp(-u))*(u*np.exp(v)-2*np.exp(-u))
lr = 0.1
# problem 5-6
i = 0
[u, v] = np.array((1.0, 1.0), dtype=np.float64)
while True:
e = E(u, v)
print ("i:%d, u:%e, v:%e, e:%e" % (i, u, v, e))
time.sleep(0.1)
if e<1e-14:
break
d_u = lr*d_E_d_u(u, v)
d_v = lr*d_E_d_v(u, v)
u -= d_u
v -= d_v
i += 1
print("---")
[u, v] = np.array((1.0, 1.0), dtype=np.float64)
# problem 7
for i in range(15):
d_u = lr*d_E_d_u(u, v)
u -= d_u
d_v = lr*d_E_d_v(u, v)
v -= d_v
e = E(u, v)
print("i:%d, u:%e, v:%e, e:%e" % (i, u, v, e))
time.sleep(0.1)