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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by