Filter löschen
Filter löschen

For loop to calculate N times?

1 Ansicht (letzte 30 Tage)
Christian
Christian am 29 Mär. 2013
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?

Antworten (1)

Mats
Mats am 29 Mär. 2013
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

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by