2

I don't know if this is the right forum but

I am trying to code a rotating vector v by an angle theta anticlockwise n times around a circle. Afterwards, set a variable r to be the number of these n+1 vectors that are within (strictly less than) distance d of vector w.

So far what I've done is times (n+1) by the rotational vector [cos(theta),-sin(theta);sin(theta),cos(theta)] with vector v.

So

a=(n+1)*[cos(theta),-sin(theta);sin(theta),cos(theta)]*v;

I know I have to somehow incorporate norm(a-w) <= w or something as that gives the distance between the two vectors but I'm kind of stuck on how to do so.

Alternatively, I am also thinking that instead of times'ing by (n+1), I'm thinking that maybe it's wrong and that I have to make a loop like for n=1,2,...,k, if this vector is within norm(a - w) < d then sum, else disregard or something.

This would be easy if it was manual as it would only require multiplying the rotational vector by v and find the distance between the new rotated vector and vector w, and repeat it again for however many n's but I am having trouble coding this.

Any help would be appreciated

i9-9980XE
  • 187
  • 1
  • 13

1 Answers1

2

You want to do a for loop, as you suggest. Something similar to the following:

A = [cos(theta), -sin(theta); sin(theta), cos(theta)];

r = 0;
for k=1:n
    v = A*v;
    if norm(v-w) < d
        r = r + 1;
    end
end
Nick
  • 5,948
  • Right! This looks perfect. How would I include the first case before rotating as well ? to include the vector before any rotation ? – i9-9980XE Apr 28 '19 at 22:43
  • I think maybe just move the "$v = Av$" line to after the "if" statement, and then change the "for" loop to start at 0 instead of 1. – Nick Apr 28 '19 at 22:46
  • I have tried both
    for k=0:n
     v = A*v;
     if norm(v-w) < d
    
            r = r + 1;
        end
    end```
    as well as
    ```r = 0;
    for k=0:n
     if norm(v-w) < d
     v = A*v;
            r = r + 1;
        end
    end```
    
    First answer I get is 6, and the next answer I get is 0. The correct answer is 5, which is what I get from your original coding. But if I try it with another input, the value is missing the original vector counted.
    
    – i9-9980XE Apr 28 '19 at 22:50
  • you could just add another "if" statement before the for loop to check if the original vector is close enough. – Nick Apr 28 '19 at 22:53
  • Thank you ! I ended up trying ```A = [cos(theta), -sin(theta); sin(theta), cos(theta)];

    r = 0; v=v; if norm(v-w) < d; r=r+1; end for k=1:n v = A*v; if norm(v-w) < d r = r + 1;

    end
    

    end```

    – i9-9980XE Apr 28 '19 at 22:58