In Matlab Write a function rotateLine(). The function has three parameters, x, y
ID: 3689388 • Letter: I
Question
In Matlab Write a function rotateLine(). The function has three parameters, x, y and theta. x is a vector contains some x coordinates. y is a vector containing the same number of y coordinates. That is, x and y represent a bunch of points. The third parameter is theta, representing an angle in degrees. The function returns xprime and yprime where xprime and yprime represent that x and y rotated about the origin by the angle theta. Hint: Use the 2-D rotation matrix. % For example: > x = [ 7 7 11 11 7]; > y = [-5 -9 -9 -5 -5]; > [xprime yprime] = rotateLine(x, y, 90) %returns xprime = [5 9 9 5 5] yprime = [7 7 11 11 7]
Explanation / Answer
function [xf,yf] = rotateLine (x,y,t) % This is the definition of the rotation matrix (R=[ ... cosd(t) -sind(t) sind(t) cosd(t) ]; %This is to perform rotation of the points x and y xf = R*x; yf = R*y; %This is used to plot of the original line plot(x(1),x(2),'k*', y(1),y(2),'b*'); line([x(1) y(1)],[x(2) y(2)], 'Color','r'); grid on hold on % This is used to plot the rotated line plot(xf(1),xf(2),'g*', yf(1),yf(2),'r*'); line([xf(1) yf(1)],[xf(2) yf(2)], 'Color','b'); legend('x','y','line xy','xf','Byf','line AfBf','Location','northwestoutside'); daspect([1 1 1]) calling: >>[xf,yf] = rotateline(x,y,20) 20=>Theta x-VECTOR y->Vector
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.