MATLAB: Index Exceeds Matrix Dimensions

dimensionindexmatrix

I have the following code:
clear;
clc;
load nodes2.txt;
load elements2.txt;
load rightlist2.txt;
load leftlist2.txt;
E = 210e9;
v = .3;
t = .001;
D = (E/(1-((v)^2)))*[1 v 0; v 1 0; 0 0 .5*(1-v)];
K = zeros(2*(length(nodes2)),2*(length(nodes2)));
nodal_load = zeros(2*(length(nodes2)),1);
displacement = zeros(2*(length(nodes2)),1);
for i = 1:length(elements2)
X(:,:,i) = [1 nodes2(elements2(i,1),1) nodes2(elements2(i,1),2); 1 nodes2(elements2(i,2),1) nodes2(elements2(i,2),2); 1 nodes2(elements2(i,3),1) nodes2(elements2(i,2),3) ];
A(i) = .5*det(X);
betai(i) = (nodes2(elements2(i,2),2) - nodes2(elements2(i,3),2));
betaj(i) = (nodes2(elements2(i,3),2) - nodes2(elements2(i,1),2));
betam(i) = (nodes2(elements2(i,1),2) - nodes2(elements2(i,2),2));
gammai(i) = (nodes2(elements2(i,3),1) - nodes2(elements2(i,2),1));
gammaj(i) = (nodes2(elements2(i,1),1) - nodes2(elements2(i,3),1));
gammam(i) = (nodes2(elements2(i,2),1) - nodes2(elements2(i,1),1));
B = (1/(2*A(i)))*[betai(i) 0 betaj(i) 0 betam(i) 0;0 gammai(i) 0 gammaj(i) 0 gammam(i); gammai(i) betai(i) gammaj(i) betaj(i) gammam(i) betam(i)];
which_elem = i;
k = t*A(i)*B'*D*B;
k(:,:,i) = k;
B(:,:,i) = B;
Ilist = [2*(elements2(i,1)) - 1; 2*(elements2(i,1))];
Jlist = [2*(elements2(i,2)) - 1; 2*(elements2(i,2))];
Mlist = [2*(elements2(i,3)) - 1; 2*(elements2(i,3))];
list = [Ilist;Jlist;Mlist];
totallist(:,i) = list;
for q = 1:6
for j = 1:6
K(list(j),list(q)) = K(list(j),list(q)) + k(j,q);
end
end
end
K;
for b = 1:length(leftlist2)
listdofremove(b,1) = 2*(leftlist2(b)) - 1;
end
listdofremove(length(listdofremove) + 1,1) = 2*leftlist2(b);
listdofremove = [1;9;2];
listkeep = 1:(2*length(nodes2));
listkeep(listdofremove) = [];
listkeep = listkeep';
K_reduce = K(listkeep,listkeep)
nodal_load(2*(rightlist2) - 1) = 1000/(length(rightlist2));
nodal_load_reduce = nodal_load(listkeep);
displacement_reduced = K_reduce\nodal_load_reduce;
displacement(listkeep) = displacement_reduced(1:end);
for a = 1:size(B,3)
stress(:,a) = D*B(:,:,a)*displacement(totallist(:,a));
end
maxstress = max(stress(1,:))
with the files:
nodes2 =
0 0
1.1000 0
1.2000 1.3000
0.9000 2.9000
0.1000 2.1000
elements2 =
4 5 3
1 3 5
1 2 3
And when run, I receive the error code:
Index exceeds matrix dimensions.
Error in fem (line 26)
X(:,:,i) = [1 nodes2(elements2(i,1),1) nodes2(elements2(i,1),2); 1
nodes2(elements2(i,2),1) nodes2(elements2(i,2),2); 1 nodes2(elements2(i,3),1)
nodes2(elements2(i,2),3) ];
Can anyone help me understand the why the error is happening?

Best Answer

Due to the absence of comments, we cannot guess the intention of the code. You do not provide the files "rightlist2.txt" and "leftlist2.txt", such that we cannot run your code. Without knowing, what you want to achieve an the possibility to run the code, there is no chance to guess the cause of the problem.
So all I cann suggest is using the debugger:
dbstop if error
Now Matlab stops when the error occurs. Then check the current values:
size(X)
size(nodes2)
elements2(i,1)
elements2(i,2)
elements2(i,3)
By the way, this looks nicer:
X(:,:,i) = [1, nodes2(elements2(i,1),1:2); ...
1, nodes2(elements2(i,2),1:2);
1, nodes2(elements2(i,3),1:3)];
And when I see this leaner formulation, I think the 3rd line requests the 3rd column of nodes2, which has 2 columns only?! Perhaps it should be "1:2" also.