For loop to calculate N times?

1 次查看(过去 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 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by