how to do a vector rotation?

조회 수: 217 (최근 30일)
Taylor Vogt
Taylor Vogt 2019년 9월 8일
답변: Sriram Tadavarty 2020년 4월 25일
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
Sean Astill
Sean Astill 2020년 4월 25일
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 2020년 4월 25일
Write it exactly as they have listed it (where theta is in degrees):
rot = [cosd(theta) -sind(theta);
sind(theta) cosd(theta)]

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

답변 (1개)

Sriram Tadavarty
Sriram Tadavarty 2020년 4월 25일
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

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by