-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNewton.m
115 lines (81 loc) · 2.69 KB
/
Newton.m
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
classdef Newton
properties (Constant)
end
properties
x0;
eps;
trigger;
w;
N;
d;
h;
sol;
xold;
end
methods
function obj = Newton(eps, w, h, N, d)
obj.eps = eps;
obj.w = w;
obj.h = h;
obj.N = N;
obj.d = d;
end
function obj = Solve(obj, x0, xold)
obj.xold = xold;
obj.x0 = x0;
x1 = x0;
goon = true;
obj.trigger = 1;
while goon && obj.trigger < 100
x0 = x1;
x1 = obj.Iteration(x0);
goon = norm(x1-x0)>obj.eps;
obj.trigger = obj.trigger + 1;
end
obj.sol = x1;
end
function res = Iteration(obj, xl)
res = reVtoM((reMtoV(xl) - reMAtoM(obj.GF(xl))^(-1)*reMtoV(obj.F(xl))), obj.d);
end
function res = F(obj, x)
% res = zeros(obj.N, obj.d);
%
% for i = 1:obj.N
% temp = zeros(1, obj.d);
% for j = 1: obj.N
% if i~=j
% temp = temp + obj.w(i, j)*(x(j, :) - x(i, :));
% end
% end
% res(i, :) = x(i, :) - obj.h*x(i, :).*temp - obj.xold(i, :);
% end
res = x - obj.h*f(x, obj.w, obj.N, obj.d) - obj.xold;
end
function res = GF(obj, x)
% res = zeros(obj.d, obj.d, obj.N, obj.N);
% for i = 1:obj.N
% for j = 1: obj.N
% if i~=j
% %% dFi/dxj
% res(:, :, i, j) = -obj.w(i, j)*diag(x(i, :));
% end
% end
% %% dFi/dxi
% temp = zeros(1, obj.d);
% for j = 1: obj.N
% if i~=j
% temp = temp + obj.w(i, j)*(x(j, :) - x(i, :));
% end
% end
% res(:, :, i, i) = eye(obj.d, obj.d) - obj.h*diag((temp - obj.w(i, i)*x(i, :)));
% end
res = - obj.h*Gf(x, obj.w, obj.N, obj.d);
%only diagonal values are different from -h*Gf
for i = 1:obj.N
res(:, :, i, i) = res(:, :, i, i) + eye(obj.d, obj.d);
end
end
end
methods (Static)
end
end