For loop to calculate N times?

조회 수: 10 (최근 30일)
Christian
Christian 2013년 3월 29일
Write a Matlab script that takes a (column) vector v, an angle (in radians), a natural number N and does the following: it plots the vector v in blue and performs N (counter-clockwise) rotations of v by the angle . The first rotation of v is plotted using a red dashed line and all the other rotated vectors are plotted in solid red lines.
Here is what I have so far...
function lab8taskI(v,theta,N)
m=v(1,:);
n=v(2,:);
quiver(0,0,m,n,1,'b')
grid on;
hold on;
R=[cos(theta) -sin(theta);sin(theta) cos(theta)];
A=R*v;
y=A(1,:);
z=A(2,:);
quiver(0,0,y,z,1,'--r')
I plotted the first two vectors, now I need to find the next N rotations and graph them, I can't think of how to do that with a for loop?

답변 (1개)

Mats
Mats 2013년 3월 29일
function lab8taskI(v,theta,N)
quiver(0,0,v(1),v(2),1,'b')
grid on;
hold on;
style = '--r';
for n = 1:N
R=[cos(n*theta) -sin(n*theta);sin(n*theta) cos(n*theta)];
v2 = R*v;
if n*theta>2*pi; style = '-r'; end;
quiver(0,0,v2(1),v2(2),1,style)
end
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by