# advance a vector coordinates in a loop

조회 수: 2 (최근 30일)
댓글: KSSV 2021년 3월 20일
Hi all,
I'd like to advance an intial vector forward in something similar to this:
where the blue arrow is the initial one, and the the green is the next one, and so on so forth. The code I have at the moment is:
%angle of the norm
theta_norm = pi/2;
r = 0.7071/2;
%Initialise the arrow on the xy grid
x0 = 0.5; %initial x position
y0 = 0; %initial y position
%looping
for i = 1:4
%the angle between the vector and the norm (orientation)
theta = i*theta_norm - theta_xaxis;
%find out the Cartesian coordinates
x = r*cos(theta);
y = r*sin(theta);
%draw an arrow in each loop
quiver(x0,y0,x,y,0,'b','LineWidth',3) %here is my problem!!
pause(0.1)
end
Any help would be appreictaed! Thanks.

댓글을 달려면 로그인하십시오.

### 답변 (1개)

KSSV 2021년 3월 20일
m = 25 ; n = 25 ;
x = linspace(0,1,m) ;
y = linspace(0,1,n) ;
dx = min(diff(x)) ;
dy = min(diff(y)) ;
dxy = sqrt(dx^2+dy^2) ;
[X,Y] = meshgrid(x,y) ;
r = 0.7071/2;
theta = pi/4 ;
%Initialise the arrow on the xy grid
x0 = 0.5; %initial x position
y0 = 0; %initial y position
%looping
for i = 1:4
%the angle between the vector and the norm (orientation)
r = r+dxy ;
%find out the Cartesian coordinates
x = r*cos(theta);a = pi/4 ;
y = r*sin(theta);
%draw an arrow in each loop
plot(X,Y,'.r')
hold on
quiver(x0,y0,x,y,'b','LineWidth',3) %here is my problem!!
hold off
drawnow
pause(0.1)
end
##### 댓글 수: 2없음 표시없음 숨기기
Thanks for your reply and attempt to help! But when running your code, the blue arrow is still in the (x0=0.5,y0=0) poistion, i.e., it hasn't moved/translated to the second poition (x0=1,y0=0.5), and next to the third (x0=0.5,y0=1), and so on to the fourth position (x0=0,y0=0.5), which was my main problem.
Thanks.
KSSV 2021년 3월 20일
Include another loop for x0 and y0.

댓글을 달려면 로그인하십시오.

### 카테고리

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