Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use matlab to complete the following excercies and verify code where possible: R

ID: 1766758 • Letter: U

Question

Use matlab to complete the following excercies and verify code where possible:

Rotation matrices Rotation matrices are useful when you have a vector in 2 or 3 dimensions that must be rotated around some central pivot point. cos (sin (0)Xo [sin () cos ()] = where (x0,y0) is a position vector that defines the initial position of a point, (x1.y) is a position vector defining the final location of the point and is the angle of rotation. Rotating rods Consider two rods lying on a 2D plane and fused to each other at one end (B) with the angle between the rods given by . The first rod is attached to a small motor that rotates the first rod by 2 radians (and by extension the second rod as well) Co 2(t) Bo 1. Write a MATLAB function (as a .m file) titled n#######RotatingRod. The function should accept a vector consisting of the initial location of the first rod (B) = ) and the time that the rods rotates for (t). The function should then calculate and output the final location of the first rod (B-xi,Vi) after 2t rotating for the given time, where e-sin 2. Write a function that modifies the previous function such that it also recieves the length of the second rod (L) and the angle between the two rods (9) as inputs. Calculate and output the initial location (G) and final location (C1) of the second rod in additional to B, from question 1 3a. Write a function that modifies the question from Q2 so that it also outputs a plot containing the rod in its initial and final position (it should look similar to the diagram in this document, but nodes and angles don't need to be labelled) 3b. Plot (each in different figures) the initial and final position of the two rods for t = 0.5s, 1s, 2s, 3s and 45, given (Xy,Yo) = ( 1, 0), L = 1 and ,-1 Comment on what you observe.

Explanation / Answer

function B1 = RotationRod1(x0,y0,t)
%We get the angle
theta=sin(2*t/(pi));

% Rotation matrix
A=[cos(theta),-sin(theta);sin(theta),cos(theta)];
%New coordinates
B1=A*[x0;y0];

end

function [B1,C0,C1] = RotationRod2(x0,y0,t,d,theta)
%We get the angle and vector B0
theta2=sin(2*t/(pi));
B0=[x0;y0];
% Rotation matrix
A=[cos(theta2),-sin(theta2);sin(theta2),cos(theta2)];
%New coordinates
B1=A*[x0;y0];
%Calculating the distance of C0 using Law of Cosines
d2=sqrt(d^2+norm(B0)^2-2*norm(B0)*d*cos(theta));
%We calculate the angle between B0 and the origin
theta3=arctan(y0/x0);
%Now the angle between C0 and B0 using Law of Sines
theta4=arcsin(d*sin(theta)/d2);
%So the angle between C0 and the origin is:
thetaC0=theta3+theta4;
%then
C0=[d2*cos(thetaC0);d2*sin(thetaC0)];
%And we rotate C0 to obtain C1
C1=A*C0;


end

function [B1,C0,C1] = RotationRod3(x0,y0,t,d,theta)
%We get the angle and vector B0
theta2=sin(2*t/(pi));
B0=[x0;y0];
% Rotation matrix
A=[cos(theta2),-sin(theta2);sin(theta2),cos(theta2)];
%New coordinates
B1=A*[x0;y0];
%Calculating the distance of C0 using Law of Cosines
d2=sqrt(d^2+(norm(B0))^2-2*norm(B0)*d*cos(theta));
%We calculate the angle between B0 and the origin
theta3=atan(y0/x0);
%Now the angle between C0 and B0 using Law of Sines
theta4=asin(d*sin(theta)/d2);
%So the angle between C0 and the origin is:
thetaC0=theta3+theta4;
%then
C0=[d2*cos(thetaC0);d2*sin(thetaC0)];
%And we rotate C0 to obtain C1
C1=A*C0;

%**********PLOT************
maxi=max([x0,C0(1),y0,C0(2),B1(1),C1(1),B1(2),C1(2)]);
figure

plot([0,x0],[0,y0])
hold on;
plot([x0,C0(1)],[y0,C0(2)])
plot([0,B1(1)],[0,B1(2)])
plot([B1(1),C1(1)],[B1(2),C1(2)])
axis([-maxi maxi -maxi maxi])

end