how to do a vector rotation?

217 views (last 30 days)
Taylor Vogt
Taylor Vogt on 8 Sep 2019
Answered: Sriram Tadavarty on 25 Apr 2020
Complete the implementation of the vectorRotate function. The function rotates the input column vector, a, by 45 degrees to get b. It then rotates b by 45 degrees to get c. It then checks that the c is perpendicular to a to a tolerance of 1e-10. If they are equal within tolerance, then d should equal 1. Otherwise, d should equal zero.
Step 1: Create a rotation matrix R =
cos(θ)-sin(θ)
sin(θ)cos(θ)
.
Step 2: Rotate the vector by 45 degrees twice. To rotate a 2D column vector a, by an angle θ, apply the matrix multiplication a_rot = R a.
Step 3: Use an if statement to check whether the corresponding vector c is perpendicular to a. Because of errors associated with floating point arithmetic, we do not want to check orthogonality by checking whether the dot product is equal to zero. Instead, you should check whether the absolute value of the dot product is below a tolerance of 1e-10. If it is, the vectors are orthogonal and d should have a value of 1. Otherwise, d should have a value of 0.
  3 Comments
Sean Astill
Sean Astill on 25 Apr 2020
my issue was with the creation of the 2x2 rotation matrix, it was how to write theta for cos(θ)
Any advice on this?
James Tursa
James Tursa on 25 Apr 2020
Write it exactly as they have listed it (where theta is in degrees):
rot = [cosd(theta) -sind(theta);
sind(theta) cosd(theta)]

Sign in to comment.

Answers (1)

Sriram Tadavarty
Sriram Tadavarty on 25 Apr 2020
Hi Taylor,
Hope you have solved this long before.
Placing the answer to help others, who might having the issue.
a = [1;2]; % Some random two-element vector
theta = 45;
% Step 1
% Create a rotation matrix
R_fun = @(theta) ([cosd(theta) -sind(theta); sind(theta) cosd(theta)]); % As function handle
R = [cosd(theta) -sind(theta); sind(theta) cosd(theta)];
% Step 2
% Apply the rotation to the matrix twice by 45 degrees
b = R*a;
b_fun = R_fun(45)*a; % Through function handle rotation
c = R*b;
c_fun = R_fun(45)*b_fun; % Through function handle rotation
% Step 3
% Calculate the dot product and check against the tolerance
if abs(sum(c.*a)) < 1e-10
d = 1;
else
d = 0;
end
% Through function handle usage
if abs(sum(c_fun.*a)) < 1e-10
d_fun = 1;
else
d_fun = 0;
end
Hope this helps.
Regards,
Sriram

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!