-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient_sd.m
More file actions
42 lines (33 loc) · 933 Bytes
/
gradient_sd.m
File metadata and controls
42 lines (33 loc) · 933 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
% Gradient Steepest Descent Function
% --- Parameters ---
% A = N x N non-singular matrix
% b = N x 1 vector
% x0 = N x 1 vector
% --- Return Value ---
% xks = every xk value found along the way is stored in a list
function xks = gradient_sd(A, b, x0)
% Initializing variables
xks = [];
xks = [xks; x0.'];
rk = b - A*x0;
xk = x0;
k = 0;
% Stepping through the algorithm
while true
% Calculating ak based on rk and A
ak = (rk.'*rk) / (rk.'*A*rk);
x = xk + ak*rk;
k = k + 1;
% Appending new xk to the list of xk values
xks = [xks; x.'];
% Calculating the error
error = norm(x - xk);
% If the algorithm has converged, exit
if (error < 1.0e-7)
return;
end
% Update variables
rk = rk - ak*A*rk;
xk = x;
end
end