Function returns different outputs with same inputs

1 vue (au cours des 30 derniers jours)
Daniel
Daniel le 5 Jan 2024
Modifié(e) : Stephen23 le 5 Jan 2024
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

Réponse acceptée

Stephen23
Stephen23 le 5 Jan 2024
Modifié(e) : Stephen23 le 5 Jan 2024
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 commentaires
Stephen23
Stephen23 le 5 Jan 2024
Modifié(e) : Stephen23 le 5 Jan 2024
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 le 5 Jan 2024
Thank you very much, that makes sense

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by