나는 공유 목적지 xd 로의 이동 계획을 위해 인공 전위 필드를 사용하는 모바일 로봇 네트워크를 시뮬레이션하려고합니다. 이것은 계산식 시간과 정확도면에서 가장 좋은 방법 인 것처럼 상징적 인 표현식에서 일련의 m 파일 (각 로봇마다 하나씩)을 생성하여 수행됩니다. 그러나 그라디언트 계산에 어떤 문제가 있는지 파악할 수 없습니다. 계산중인 분석 그라디언트가 잘못되어있는 것 같지만 수치 그라디언트는 올바르게 계산됩니다 (아래 그림 참조). 아래에 나와있는 MWE를 작성했으며이 문제도 있습니다. 코드의 일부를 생성하는 파일을 검사하고 올바른 그라데이션으로 올바른 함수 파일을 반환합니다. 그러나 왜 분석적이며 수치적인 그래디언트가 다른지는 알 수 없습니다.분석 그라디언트와 크게 다른 상징적 인 그라디언트
% create symbolic variables
xd = sym('xd',[1 2]);
x = sym('x',[2 2]);
% create a potential function and a gradient function for both (x,y) pairs
% in x
for i=1:size(x,1)
phi = norm(x(i,:)-xd)/norm(x(1,:)-x(2,:)); % potential field function
xvector = reshape(x.',1,size(x,1)*size(x,2)); % reshape x to allow for gradient computation
grad = gradient(phi,xvector(2*i-1:2*i)); % compute the gradient
gradx = grad(1);grady=grad(2); % split the gradient in two components
% create function file names
gradfun = strcat('GradTester',int2str(i),'.m');
phifun = strcat('PotTester',int2str(i),'.m');
% generate two output files
matlabFunction(gradx, grady,'file',gradfun,'outputs',{'gradx','grady'},'vars',{xvector, xd});
matlabFunction(phi,'file',phifun,'vars',{xvector, xd});
end
clear all % make sure the workspace is empty: the functions are in the files
pause(0.1) % ensure the function file has been generated before it is called
% these are later overwritten by a specific case, but they can be used for
% debugging
x = 0.5*rand(2);
xd = 0.5*rand(1,2);
% values for the Stackoverflow case
x = [0.0533 0.0023;
0.4809 0.3875];
xd = [0.4087 0.4343];
xp = x; % dummy variable to keep x intact
% compute potential field and gradient for both (x,y) pairs
for i=1:size(x,1)
% create a grid centered on the selected (x,y) pair
xGrid = (x(i,1)-0.1):0.005:(x(i,1)+0.1);
yGrid = (x(i,2)-0.1):0.005:(x(i,2)+0.1);
% preallocate the gradient and potential matrices
gradx = zeros(length(xGrid),length(yGrid));
grady = zeros(length(xGrid),length(yGrid));
phi = zeros(length(xGrid),length(yGrid));
% generate appropriate function handles
fun = str2func(strcat('GradTester',int2str(i)));
fun2 = str2func(strcat('PotTester',int2str(i)));
% compute analytic gradient and potential for each position in the xGrid and
% yGrid vectors
for ii = 1:length(yGrid)
for jj = 1:length(xGrid)
xp(i,:) = [xGrid(ii) yGrid(jj)]; % select the position
Xvec = reshape(xp.',1,size(x,1)*size(x,2)); % turn the input into a vector
[gradx(ii,jj),grady(ii,jj)] = fun(Xvec,xd); % compute gradients
phi(jj,ii) = fun2(Xvec,xd); % compute potential value
end
end
[FX,FY] = gradient(phi); % compute the NUMERICAL gradient for comparison
%scale the numerical gradient
FX = FX/0.005;
FY = FY/0.005;
% plot analytic result
subplot(2,2,2*i-1)
hold all
xlim([xGrid(1) xGrid(end)]);
ylim([yGrid(1) yGrid(end)]);
quiver(xGrid,yGrid,-gradx,-grady)
contour(xGrid,yGrid,phi)
title(strcat('Analytic result for position ',int2str(i)));
xlabel('x');
ylabel('y');
subplot(2,2,2*i)
hold all
xlim([xGrid(1) xGrid(end)]);
ylim([yGrid(1) yGrid(end)]);
quiver(xGrid,yGrid,-FX,-FY)
contour(xGrid,yGrid,phi)
title(strcat('Numerical result for position ',int2str(i)));
xlabel('x');
ylabel('y');
end
I가 발생하는 것을 시도하고 잠재적 필드에서, (x, y)의 위치에 의해 정의된다 (아래 here를 찾을 수있는 영상의 확대) 내 코드는 입니다. x
은 차원 N x 2의 위치 행렬입니다. 첫 번째 열은 x1, x2 등을 나타내고 두 번째 열은 y1, y2 등을 나타냅니다. Xvec는이 벡터를 x1, y1, x2, y2, x3, y3 등으로 재구성하는 것입니다. 생성하는 matlab 함수는 벡터 입력 만 허용합니다.
로봇 i의 기울기는 미분 w.r.t를 사용하여 계산됩니다. x_i와 y_i와 함께이 두 요소는 함께 떨림 플롯에 표시된 단일 미분 '벡터'를 산출합니다. 파생물은 this처럼 보일 것입니다. 실제로 [gradx, grady]의 상징적 표현이이되기 전에 처럼 보이고 m 파일이 생성되었는지 확인했는지 확인했습니다.
분석적으로 어려운 일이 아닌 것처럼 보입니다! –
그라데이션 계산 방법에 대한 수학적 설명을 제공 할 수 있습니까? 나는'x','xd'와'Xvec'가 이해하기 쉽지 않고, 그라디언트가 표준'x'와 y' 파생물과 일치하지 않는 것처럼 보입니다. – David
@David : 코드 아래에 몇 가지 정보를 추가했습니다. 여전히 명확하지 않은 경우 알려주십시오. –