Creating matrix inside indexed for loop

조회 수: 18 (최근 30일)
Luc Kuijken
Luc Kuijken 2020년 10월 28일
댓글: Luc Kuijken 2020년 10월 29일
I would like to know how to create a matrix inside a for loop with indexing. The indexing causes an array on its own, but I already have a vector (V, U and W) inside the for loop. Now I would like to create a matrix that shows the vector at each timestamp, but it gives the error : Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
To my knowledge, using a ' ; ' inside a vector makes it jump to the next row instead of using a ' , ' to jump to the next column. That's why I used it inside the vectors V and U. Because then the time vector would jump to the next column each iteration of the for loop, leaving a nice 2 by 1002 matrix, but as said before, it gave an error
This is my code:
%% Dimensions VAWT
R = 0.5;
H = 1.5;
%% Speeds
labda = 4;
V = [0;1];
theta(1) = 0;
omega = norm(V)*labda/R;
U_magnitude = omega*R;
%% Variables
i(1) = 1;
dt = 0.01;
rotations = 0;
%% Calculation
for t = 0:dt:10
i = i+1;
theta(i) = theta(i-1) + (omega*dt);
if theta(i) <= (2*pi)
theta(i) = theta(i-1) + (omega*dt);
else
theta(i) = theta(i) - (2*pi);
rotations = rotations+1;
end
angle_U(i) = theta(i) + (0.5*pi);
U(i) = [cos(angle_U(i))*U_magnitude;sin(angle_U(i))*U_magnitude];
W(i) = V - U(i);
end
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
rotations = rotations + (theta(i)/(2*pi));

채택된 답변

Mathieu NOE
Mathieu NOE 2020년 10월 29일
hi
as far as I understand U,V,W have 2 rows , so if I am right, this is the way to modifiy your code
%% Dimensions VAWT
R = 0.5;
H = 1.5;
%% Speeds
labda = 4;
V = [0;1];
theta(1) = 0;
omega = norm(V)*labda/R;
U_magnitude = omega*R;
%% Variables
i(1) = 1;
dt = 0.01;
rotations = 0;
%% Calculation
for t = 0:dt:10
i = i+1;
theta(i) = theta(i-1) + (omega*dt);
if theta(i) <= (2*pi)
theta(i) = theta(i-1) + (omega*dt);
else
theta(i) = theta(i) - (2*pi);
rotations = rotations+1;
end
angle_U(i) = theta(i) + (0.5*pi);
U(:,i) = [cos(angle_U(i))*U_magnitude;sin(angle_U(i))*U_magnitude];
W(:,i) = V - U(:,i);
end
  댓글 수: 1
Luc Kuijken
Luc Kuijken 2020년 10월 29일
oke, thank you. So I just needed to double index the U and the W. Which is quite logical come to think obout it.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by