MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

For loop to calculate N times?

Asked by Christian on 29 Mar 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?

Products

No products are associated with this question.

Answer by Mats on 29 Mar 2013

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