Function returns different outputs with same inputs
8 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
Context (not relevant for Problem):
For a school project, im playing around with "rotation matrix" and "Quaternions", i already calculated the Quaternions with a different methode, and now i want to compare the results i get from rotationsmatrix2quaterionen(numeric_result) with the ones i calculated earlier. The function seems to work, rr is nearly zero (not exactly because of numeric imprecision)
Problem:
I don't understand why 'rr' and 'gh' give me different results. The calculation should be the same both times, only that one time at first i write the output in a seperate array.
I assume i made some stupid mistake, but i can't find it.
numeric_result = [
0.8138 -0.4063 0.4155 10.0000;
0.4698 0.8808 -0.0590 12.5000;
-0.3420 0.2432 0.9077 64.0000;
0 0 0 1.0000]
e0 = 0.9490
e1 = 0.0796
e2 = 0.1996
e3 = 0.2308
[e0_, e1_, e2_, e3_] = rotationsmatrix2quaterionen(numeric_result)
rr=[e0, e1, e2, e3]-[e0_, e1_, e2_, e3_]
% Output:
% rr = 1×4
% 1.0e-04 *
%
% 0.1370 -0.1127 0.4496 0.0109
gh=[e0, e1, e2, e3]-rotationsmatrix2quaterionen(numeric_result)
% Output:
% gh = 1×4
% 0.0000 -0.8694 -0.7494 -0.7182
function [e0, e1, e2, e3] = rotationsmatrix2quaterionen(RotMat)
% e = e0 + e1*i + e2*j + e3*k
R=RotMat;
e0 = 0.5*sqrt(1+R(1,1)+R(2,2)+R(3,3));
e1 = (R(3,2)-R(2,3)) / (4*e0);
e2 = (R(1,3)-R(3,1)) / (4*e0);
e3 = (R(2,1)-R(1,2)) / (4*e0);
end
0 个评论
采纳的回答
Stephen23
2024-1-5
编辑:Stephen23
2024-1-5
The difference is very simple:
Here you return FOUR output arguments from your function call:
[e0_, e1_, e2_, e3_] = rotationsmatrix2quaterionen(numeric_result)
Here you only return ONE output argument from your function call:
gh=[e0, e1, e2, e3]-rotationsmatrix2quaterionen(numeric_result)
2 个评论
Stephen23
2024-1-5
编辑:Stephen23
2024-1-5
The first output argument has a value of approx. 0.9490, so your second attempt simply subtracts one scalar from a vector of four values. Lets try it right now (due to floating point accuracy the result will only be similar to what you showed):
[0.9490,0.0796,0.1996,0.2308] - 0.9490
You seem to be writing your MATLAB code as if it were Python.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!