MATLAB Answers

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...

7 views (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
for i=[0;pi/2;pi/2;0;pi/2;atan(1.5/5);atan(-1.5/5)]
c(i)*s(i),s(i)^2,-c(i)*s(i),-s(i)^2; %elemental global stiffness matrix


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 4 Jul 2020
Edited: Stephen Cobeldick 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...];
It is not possible to store a non-scalar matrix in one element of a numeric array, so I used a cell array:


Thanks that worked, but how do you use the internal matrixes for other calculations after? Even just typing ke(1) ends up giving this message : ans =
1×1 cell array
{4×4 double}
Stephen Cobeldick
Stephen Cobeldick 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)




Translated by