% Unit 2 (MATLAB Course @ UAM)
% (c) Alexander Knebe

%========================================================
% rotation of arbitray vector about arbitray angle in 3D
%========================================================
clear all
close all
clc

v     = [5.23; 0.5; 2.5]; % some vector
phi   = 37.8;             % angles given in degrees
theta = 12.8;

% define rotation matrix
deg2rad   = pi/180;
phi_rad   = phi*deg2rad;
theta_rad = theta*deg2rad;
Mx        = [1, 0, 0; 0, cos(phi_rad), -sin(phi_rad); 0, sin(phi_rad), cos(phi_rad)];
Mz        = [cos(theta_rad), -sin(theta_rad), 0; sin(theta_rad), cos(theta_rad), 0; 0,0,1];

% rotate vector
v_rot_zx = Mx*(Mz*v);
v_rot_xz = Mz*(Mx*v);
norm(v_rot_zx),norm(v_rot_xz),norm(v)

quiver3(0,0,0,v(1),v(2),v(3)), hold on
quiver3(0,0,0,v_rot_zx(1), v_rot_zx(2), v_rot_zx(3))
quiver3(0,0,0,v_rot_xz(1), v_rot_xz(2), v_rot_xz(3))
axis equal
