how to do a vector rotation?
222 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Sean Astill
el 25 de Abr. de 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
el 25 de Abr. de 2020
Write it exactly as they have listed it (where theta is in degrees):
rot = [cosd(theta) -sind(theta);
sind(theta) cosd(theta)]
Respuestas (1)
Sriram Tadavarty
el 25 de Abr. de 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
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!