Creating matrix inside indexed for loop

7 views (last 30 days)
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));

Accepted Answer

Mathieu NOE
Mathieu NOE on 29 Oct 2020
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 Comment
Luc Kuijken
Luc Kuijken on 29 Oct 2020
oke, thank you. So I just needed to double index the U and the W. Which is quite logical come to think obout it.

Sign in to comment.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!