3-dimensional orthonormal rotation symbolical

4 views (last 30 days)
Mark S
Mark S on 24 Nov 2021
Commented: Mark S on 24 Nov 2021
I want to create symbolical 3-dimensional orthonormal rotation matrices for a pure rotation of phi_x, phi_y and phi_z.
But my code did not work i get this error: Error using sym/subsindex (line 953)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic
variables, and function body must be sym expression.
Maybe someone can have a look what I am doin wrong here
% Clean up
clear all
close all
% Variable Definition
phi_x = pi/2;
phi_y = 0;
% Define symbolic variables
syms phi_x phi_y phi_z
% Create Rotation around axis
R_x = [1 0 0
0 cos(phi_x) -sin(phi_x);
0 sin(phi_x) cos(phi_x)];
R_y = [ cos(phi_y) 0 sin(phi_y);
0 1 0;
-sin(phi_y) 0 cos(phi_y)];
R_z = [cos(phi_z) -sin(phi_z) 0;
sin(phi_z) cos(phi_z) 0;
0 0 1];
% Overall Orthonormal Rotation Matrix
R = R_x*R_y*R_z
R= R_x(phi_x) * R_y(phi_y) * R_z(phi_z);
% Convert to Numerical

Answers (1)

John D'Errico
John D'Errico on 24 Nov 2021
Edited: John D'Errico on 24 Nov 2021
You have several misunderstandings here. First:
phi_x = pi/2;
phi_y = 0;
% Define symbolic variables
syms phi_x phi_y phi_z
So what happened when you wrote those lines? First, you created DOUBLE variables, named phi_x, phi_y, phi_z. You gave them numerical values.
But then what does the syms line do? It ovewrites the existing variables, replacing them with new ones that no longer have the values you assigned. So at this point, phi_x is no longer pi/2. It is just a symbolic variable with an unknown value. It might be a complex number. It might be anything.
Next, just because you have a symbolic expression like R_x, does not mean you can then perform a direct substitution using R_x(phi_x). R_x is a symbolic expression, not a function that can be used for evaluation.
You can use tools like subs, to substitute a value for a symbolic variable. So this would work:
Or you could use a tool like matlabFunction to convert them into functions.
  1 Comment
Mark S
Mark S on 24 Nov 2021
Thanks for your help. Anyway I have found now the function tranimate(R). With that I can draw the rotation (what I also wanted)

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!