2017-08-16 77 views
2

我在MATLAB中遇到了一些非常奇怪的東西,我無法弄清楚。MATLAB解決()不能解決特定值的三角矩陣

我有兩組我想要找到歐拉角的物體座標。我設置了旋轉矩陣R和符號卡丹角矩陣R_cardan來解決角度。對於第一組,我可以簡單地做solve(R==R_cardan),但是當我使用第二組時,它不起作用。返回的解決方案是空的。

這可能是什麼原因造成的?這裏是測試代碼。

clc;clear;close all; 

%% For some reason R2 does not want to give a solution 
% Find rotation matrix to transform from origin to local reference frame 
ex = [ 0.768 0.024 0.640].'; 
ey = [-0.424 0.768 0.480].'; 
ez = [-0.480 -0.640 0.600].'; 

ex2 = [ 0.612372 0.353553 -0.707107].'; 
ey2 = [0.280330 0.739199 0.612372].'; 
ez2 = [0.739199 -0.573223 0.353553].'; 

R = eye(3)*[ex ey ez] 
R2 = eye(3)*[ex2 ey2 ez2] 
% Symbolic variables 
syms beta alpha gamma 

% Set up rotatin matrices 
R_alpha = [cos(alpha) -sin(alpha) 0; sin(alpha) cos(alpha) 0; 0 0 1] 
R_beta = [cos(beta) 0 sin(beta); 0 1 0; -sin(beta) 0 cos(beta)] 
R_gamma = [1 0 0; 0 cos(gamma) -sin(gamma); 0 sin(gamma) cos(gamma)] 

% Find symbolic rotation matrix 
R_cardan = R_alpha*R_beta*R_gamma 

[alpha, beta, gamma] = find_angles(R,R_cardan) 
[alpha, beta, gamma] = find_angles(R2,R_cardan) %fails because solution is empty 

function [alpha, beta, gamma] = find_angles(R,R_cardan) 

% Solve for the angles 
sol = solve(R == R_cardan); 
alpha = double(sol.alpha(1)); 
beta = double(sol.beta(1)); 
gamma = double(sol.gamma(1)); 

end 

我目前的解決方案是手動計算角度是好的,但我對上述方法有什麼問題感興趣。

回答

2

問題將在於你解決了確切的匹配R == R_cardan。由於您正在以有限的精度進行工作,因此這很危險,並且可能沒有解決方案,因爲它發生在R2。您的目標是找到一個解決方案,使其差異非常小 - 理想情況下爲零。

我會做,而不是執行以下操作:創建一個損失函數

Loss

,並儘量減少此功能。在R = R_cardan的理想解決方案將導致零損失,但即使這在數值上是不可能的,你會得到一個儘可能接近最優解的解決方案(就歐幾里得距離而言)。

在MATLAB中,這有點複雜,但在help pages中有很好的描述。

  1. 定義中的RR_cardan方面損失函數,並把所有的未知數到載體x

    f = sum(sum((R - R_cardan).^2)); 
    x = [alpha; beta; gamma]; 
    
  2. 從分析計算損失函數的梯度和Hessian矩陣:

    gradf = jacobian(f, x).'; % column gradf 
    hessf = jacobian(gradf, x); 
    
  3. 將這些函數從符號函數轉換爲MATLAB函數han dles:

    fh = matlabFunction(f, gradf, hessf, 'vars', {x}); 
    
  4. 設置優化器使用的梯度和黑森州:

    options = optimoptions('fminunc', 'SpecifyObjectiveGradient', true, ... 
                'HessianFcn', 'objective', ... 
                'Algorithm', 'trust-region') 
    
  5. 最小化!

    solution = fminunc(fh, [0;0;0], options); 
    alpha_hat = solution(1); 
    beta_hat = solution(2); 
    gamma_hat = solution(3); 
    

對於第一個示例R,這給完全相同的溶液與solve。 對於第二個示例R2,重構的矩陣R2_hat(通過插入所述估計值獲得成R_cardan)是幾乎相同R2,但具有在至少顯著數字一些差異:

R2 = 
    0.6124 0.2803 0.7392 
    0.3536 0.7392 -0.5732 
    -0.7071 0.6124 0.3536 

R2_hat = 
    0.6125 0.2805 0.7390 
    0.3533 0.7392 -0.5734 
    -0.7071 0.6123 0.3537 
+0

非常感謝您。這是一個非常好但很複雜的解決方案。然而,瞭解解決問題的特殊方式絕對值得。 – Ortix92