Matlab Tutorials | Examples
Practice 1:
Write a MATLAB program that will evaluate the angle between two given input vectors in space.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
% Start to inputs from keyboard Px = input('Enter P(x)'); Py = input('Enter P(y)'); Pz = input('Enter P(z)'); Qx = input('Enter Q(x)'); Qy = input('Enter Q(y)'); Qz = input('Enter Q(z)'); %End of the inputs %Calculate lengths of vectors Plenght = ((Px^2)+(Py^2)+(Pz^2))^(1/2); Qlenght = ((Qx^2)+(Qy^2)+(Qz^2))^(1/2); %Calculate dot scalar of the vectors PQ = (Px*Qx) + (Py*Qy) + (Pz*Qz); %Calculate the angle of two vectors Radian = acos(PQ/(Plenght*Qlenght)); %Convert angle to degrees Angle = Radian*180/pi; %Print angle with specific fraction fprintf('The angle between P and Q is %.4f degrees.\n',Angle); %More smaller code in bellow comments %-----------------------------------------------% % fprintf('The angle between P and Q is %.4f degrees.\n',acos(((Px*Qx) + (Py*Qy) + (Pz*Qz))/(((Px^2)+(Py^2)+(Pz^2))^(1/2)*((Qx^2)+(Qy^2)+(Qz^2))^(1/2)))*180/pi); |