Trying to create a set of matrixes using a for loop. I have an error on line 23: Array indices must be positive integers or logical values. Basically making matrixes labeled ke1, ke2, ke3...

1 view (last 30 days)
clear all;close all;clc;
E=230*10^9; %Pa (young's modulus of the steel truss members)
A=20*10^-4; %m^2 (cross sectional area of truss members)
%7 elements
%5 nodes with 2 degrees of freedom each so {d} will be a 10*1 matrix
%also {f} will be 10*1 and [K] will be 10*10
%hooke's law -> F=AE(lf-li)/l
%spring constant -> k=AE/l
le=[5;1.5;1.5;5;3;(sqrt((1.5^2)+(5^2)));(sqrt((1.5^2)+(5^2)))]; %lengths of elements
k=A*E./le; %spring constants of each element
%if [K] is a 10*10 matrix
%each elemental stiffness matrix will be 4x4 because there are 2 nodes and
%2 degrees of freedom for each element
thetae=[0;pi/2;pi/2;0;pi/2;atan(1.5/5);atan(-1.5/5)];
c=cos(thetae);
s=sin(thetae);
for i=[0;pi/2;pi/2;0;pi/2;atan(1.5/5);atan(-1.5/5)]
ke(i)=[c(i)^2,c(i)*s(i),-c(i)^2,-c(i)*s(i);
c(i)*s(i),s(i)^2,-c(i)*s(i),-s(i)^2; %elemental global stiffness matrix
-c(i)^2,-c(i)*s(i),c(i)^2,c(i)*s(i);
-c(i)*s(i),-s(i)^2,c(i)*s(i),s(i)^2;];
end

Accepted Answer

Stephen23
Stephen23 on 4 Jul 2020
Edited: Stephen23 on 4 Jul 2020
There are multiple issues with your loop:
  1. you are attempting to use invalid indices (e.g. 0, pi/2, etc.).
  2. you are providing for with a column vector, which means that your loop will iterate exactly once and i will be that column vecctor. As the for documentation explains, it loops over the columns of the provided array.
  3. you are trying to store a non-scalar matrix in a scalar array element.
The simple and robust MATKAB approach is to loop over indices (not data values) and to provide a row vector, e.g.:
N = numel(thetae);
ke = cell(1,N);
for kk = 1:N % loop over row of indices
ke{kk} = [... your matrix...];
end
It is not possible to store a non-scalar matrix in one element of a numeric array, so I used a cell array:
  3 Comments
Stephen23
Stephen23 on 4 Jul 2020
Edited: Stephen23 on 4 Jul 2020
With cell arrays (also tables and string arrays)
  • () parenthese refer to the container array itself.
  • {} curly braces refer to the contents of the array.
Read more:
Alternatively you could concatenate all of the matrices into one 3D numeric array, e.g.:
A = cat(3,ke{:})

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!