Function returns different outputs with same inputs

조회 수: 7 (최근 30일)
Daniel
Daniel 2024년 1월 5일
편집: Stephen23 2024년 1월 5일
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]
numeric_result = 4×4
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
e0 = 0.9490
e1 = 0.0796
e1 = 0.0796
e2 = 0.1996
e2 = 0.1996
e3 = 0.2308
e3 = 0.2308
[e0_, e1_, e2_, e3_] = rotationsmatrix2quaterionen(numeric_result)
e0_ = 0.9490
e1_ = 0.0796
e2_ = 0.1996
e3_ = 0.2308
rr=[e0, e1, e2, e3]-[e0_, e1_, e2_, e3_]
rr = 1×4
1.0e-04 * 0.1370 -0.1127 0.4496 0.0109
% Output:
% rr = 1×4
% 1.0e-04 *
%
% 0.1370 -0.1127 0.4496 0.0109
gh=[e0, e1, e2, e3]-rotationsmatrix2quaterionen(numeric_result)
gh = 1×4
0.0000 -0.8694 -0.7494 -0.7182
% 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

채택된 답변

Stephen23
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
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
ans = 1×4
0 -0.8694 -0.7494 -0.7182
You seem to be writing your MATLAB code as if it were Python.
Daniel
Daniel 2024년 1월 5일
Thank you very much, that makes sense

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by