5 views (last 30 days)

Im currently trying to use a for loop to generate different 'Ke' matrices and storing it all in its distinct location in the KG matrix. However, my for loop currently only stops at the first index of theta (which is zero) and does not move on to other values in the 'theta' matrix. Any suggestions?

KG = zeros(24);

for n = 1:21

theta = [0, 0, 90, 45, 135, 30, 90, 0, 136.4, 15, 90, 165, 46.4, 0, 90, 45, 0, 90, 150, 135, 0];

theta = theta.*pi/180;

Ke = [cos(theta(n))^2 sin(theta(n))*cos(theta(n)) -cos(theta(n))^2 -sin(theta(n))*cos(theta(n));

sin(theta(n))*cos(theta(n)) sin(theta(n))^2 -sin(theta(n))*cos(theta(n)) -sin(theta(n))^2;

-cos(theta(n))^2 -sin(theta(n))*cos(theta(n)) cos(theta(n))^2 sin(theta(n))*cos(theta(n));

-sin(theta(n))*cos(theta(n)) -sin(theta(n))^2 sin(theta(n))*cos(theta(n)) sin(theta(n))^2]*k_stiff(n);

n = n + 1;

for el = 1:21

iel = [1, 2, 2, 1, 3, 11, 3, 3, 4, 10, 4, 9, 4, 4, 5, 5, 5, 6, 8, 7, 6];

jel = [2, 3, 12, 12, 12, 12, 11, 4, 11, 11, 10, 10, 9, 5, 9, 8, 6, 8, 9, 8, 7];

i = iel(el);

j = jel(el);

KG(2*i-1, 2*i-1) = Ke(1,1);

KG(2*i-1, 2*i) = Ke(1,2);

KG(2*i-1, 2*j-1) = Ke(1,3);

KG(2*i-1, 2*j) = Ke(1,4);

KG(2*i, 2*i-1) = Ke(2,1);

KG(2*i, 2*i) = Ke(2,2);

KG(2*i, 2*j-1) = Ke(2,3);

KG(2*i, 2*j) = Ke(2,4);

KG(2*j-1, 2*i-1) = Ke(3,1);

KG(2*j-1, 2*i) = Ke(3,2);

KG(2*j-1, 2*j-1) = Ke(3,3);

KG(2*j-1, 2*j) = Ke(3,4);

KG(2*j, 2*i-1) = Ke(4,1);

KG(2*j, 2*i) = Ke(4,2);

KG(2*j, 2*j-1) = Ke(4,3);

KG(2*j, 2*j) = Ke(4,4);

el = el + 1;

end

end

Walter Roberson
on 17 Sep 2019

Sign in to comment.

Raj
on 17 Sep 2019

Edited: Raj
on 17 Sep 2019

Shift these lines of code outside the 'for' loop:

theta = [0, 0, 90, 45, 135, 30, 90, 0, 136.4, 15, 90, 165, 46.4, 0, 90, 45, 0, 90, 150, 135, 0];

theta = theta.*pi/180;

That'll work. Also you can shift these lines as well (outside the loops):

iel = [1, 2, 2, 1, 3, 11, 3, 3, 4, 10, 4, 9, 4, 4, 5, 5, 5, 6, 8, 7, 6];

jel = [2, 3, 12, 12, 12, 12, 11, 4, 11, 11, 10, 10, 9, 5, 9, 8, 6, 8, 9, 8, 7];

There is no need of

n=n+1;

and

el=el+1;

The 'for' loop takes care of increments automatically.

Walter Roberson
on 17 Sep 2019

My "More efficient" code posted in the comments does all these things.

However, none of them affect why the impression is given that only one theta value is used.

Raj
on 17 Sep 2019

Yeah right....my mistake. Thanks for pointing out walter sir. I wrote all my 'comments' on the code but forgot to address the main issue raised in the question. I missed this part in the first answer:

"my for loop currently only stops at the first index of theta (which is zero) and does not move on to other values in the 'theta' matrix" - The value of KG you are getting is not from the first index of theta but the last index value of theta which in this case is zero (same as first value) and hence you are getting confused. For each value of Ke, you get one KG matrix. So the first loop is indeed running all values of theta but the second loop is overwriting KG with each value of Ke.

@walter sir: You missed to give any suggestion that'll help solving the issue. My recommendation is to store all the KG matrices in a cell array. Please suggest any better alternative if you have any in mind. Thanks again :)

Walter Roberson
on 17 Sep 2019

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480712-how-to-iterate-through-a-matrix-using-for-loops#comment_746668

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480712-how-to-iterate-through-a-matrix-using-for-loops#comment_746668

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480712-how-to-iterate-through-a-matrix-using-for-loops#comment_748620

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480712-how-to-iterate-through-a-matrix-using-for-loops#comment_748620

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480712-how-to-iterate-through-a-matrix-using-for-loops#comment_760791

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480712-how-to-iterate-through-a-matrix-using-for-loops#comment_760791

Sign in to comment.