-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradientDecentForJointInitials.m
52 lines (40 loc) · 1.6 KB
/
gradientDecentForJointInitials.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
function [optimalPsiInitial,optimalQ5Initial] = localOptimizationForJointErrorInitials(K,psiSeed,q5Seed)
% maximize function f0 = [sin(psi), cos(psi),1]*K*[sin(q5), cos(q5),1]'
% minimize function f = -[sin(psi), cos(psi),1]*K*[sin(q5), cos(q5),1]' = [sin(psi), cos(psi),1]*K_N*[sin(q5), cos(q5),1]', where K_N = -K
% minimize function f = psiFeature'*K_N*q5Feature
K_N = -K;
x0 = [psiSeed,q5Seed];
% d(psiFeature')/d(psi) = [cos(psi), -sin(psi), 0 ] =[sin(psi), cos(psi),1] * G_psi = psiFeature'* G_psi;
G_psi = [0,-1,0;1,0,0;0,0,0];
df_dPsi_matrix = G_psi*K_N; % df_dPsi = psiFeature' * df_dPsi_matrix * q5Feature
% d(q5Feature)/d(q5) = [cos(q5); -sin(q5); 0 ] = G_q5 * [sin(q5); cos(q5);1]= G_q5 * q5Feature ;
G_q5 = [0,1,0;-1,0,0;0,0,0];
df_dq5_matrix =K_N*G_q5; % df_dq5 = psiFeature' * df_dq5_matrix * q5Feature
% https://www.mathworks.com/matlabcentral/answers/787539-steepest-descent-algorithm-in-matlab
stepSize = 0.1;
stepSizeLowBound =0.0001;
tol = 1e-6;
while 1
x_prev = x0;
grad_prev = [func(x_prev,df_dPsi_matrix), func(x_prev,df_dq5_matrix)];
temp = x_prev - stepSize*grad_prev;
if(func(x_prev,K_N)<func(temp,K_N))
stepSize = stepSize/2;
if stepSize <stepSizeLowBound
break;
end
else
x0=temp;
if norm(x0-x_prev)<tol
break;
end
end
%fprintf("iteration psi:%f, q5:%f, stepSize:%f,loss:%f\n",x0(1),x0(2),stepSize,func(x0,K_N))
end
% map angle to [0,2*pi)
x0 = x0 -2*pi*fix(x0/(2*pi));
x0 = x0+2*pi;
x0 = x0 -2*pi*fix(x0/(2*pi));
optimalPsiInitial = x0(1);
optimalQ5Initial = x0(2);
end