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 endK;for b = 1:length(leftlist2) listdofremove(b,1) = 2*(leftlist2(b)) - 1;endlistdofremove(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));endmaxstress = max(stress(1,:))
with the files:
nodes2 = 0 0 1.1000 0 1.2000 1.3000 0.9000 2.9000 0.1000 2.1000elements2 = 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