-
Notifications
You must be signed in to change notification settings - Fork 3
/
potgrad.m
executable file
·36 lines (29 loc) · 1.08 KB
/
potgrad.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
function dvecdt = potgrad(t,y, voxData, shapecenter)
y_round = round(y);
xVal = y_round(1);
yVal = y_round(2);
zVal = y_round(3);
dist=norm(([xVal, yVal, zVal] - shapecenter),2);
if (dist < 3)
dvecdt = zeros(3,1);
else
count =1;
lhsmat = ones(27,8);
rhsVec = ones(27,1);
for i = -1:1
for j =-1:1
for k =-1:1
rhsVec(count,1)=voxData(xVal+i, yVal + j, zVal+k, 3);
lhsmat(count,:)=[(xVal+i)*(yVal+j)*(zVal+k), (xVal+i)*(yVal+j), (yVal+j)*(zVal+k), ...
(zVal+k)*(xVal+i), (xVal+i), (yVal+j), (zVal+k), 1];
count=count+1;
end
end
end
paramVec = (pinv(lhsmat))*rhsVec;
potgradVec(1,1)=(paramVec(1)*y(2)*y(3) + paramVec(2)*y(2) + paramVec(4)*y(3) +paramVec(5));
potgradVec(2,1)=(paramVec(1)*y(1)*y(3) + paramVec(2)*y(1) + paramVec(3)*y(3) +paramVec(6));
potgradVec(3,1)=(paramVec(1)*y(1)*y(2) + paramVec(3)*y(2) + paramVec(4)*y(1) +paramVec(7));
gradGain = -1/norm(potgradVec,2);
dvecdt = gradGain*potgradVec;
end