-
Notifications
You must be signed in to change notification settings - Fork 0
/
Levenberg-Marquardt.py
52 lines (35 loc) · 1.1 KB
/
Levenberg-Marquardt.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
def f(x, y):
return y * y - 2 * x * y - 6 * y + 2 * x * x + 12
def dfx(x, y):
return 4 * x - 2 * y
def dfy(x, y):
return 2 * y - 2 * x - 6
def dfxx(x, y):
return 4
def dfyy(x, y):
return 2
def dfyx(x, y):
return -2
def dfxy(x, y):
return -2
# xn+1 xn - invert(hessiana).gradient + lambda.gradiente
def levenberg(x, y, lamb):
x_ant = x
y_ant = y
for i in range(20):
det = dfxx(x_ant, y_ant) * dfyy(x_ant, y_ant) - dfxy(x_ant, y_ant) * dfyx(x_ant, y_ant)
x = x_ant - (dfyy(x_ant, y_ant) * dfx(x_ant, y_ant) - dfxy(x_ant, y_ant) * dfy(x_ant, y_ant)) / det - lamb * (
dfx(x_ant, y_ant))
y = y_ant - (-dfyx(x_ant, y_ant) * dfx(x_ant, y_ant) + dfxx(x_ant, y_ant) * dfy(x_ant,
y_ant)) / det - lamb * dfy(
x_ant, y_ant)
if f(x_ant, y_ant) > f(x, y):
lamb /= 2
else:
lamb *= 2
x_ant = x
y_ant = y
print(x, y)
return [x, y]
r = levenberg(1, 1, 0.1)
print(f(r[0], r[1]))