??? Index exceeds matrix dimensions.

I keep getting this error every time I run this code. Can you tell me where I am wrong? I have run through the debugger and can't seem to figure out where I went wrong.
K = zeros(3*NN,3*NN);
M = zeros(3*NN,3*NN);
for n = 1:NE
i = Dir(n,2);
j = Dir(n,3);
K(3*i-2:3*i,3*i-2:3*i)= K11(:,:,n) + K(3*i-2:3*i,3*i-2:3*i);
K(3*i-2:3*i,3*j-2:3*j)= K12(:,:,n);
K(3*j-2:3*j,3*i-2:3*i)= K21(:,:,n);
K(3*j-2:3*j,3*j-2:3*j)= K22(:,:,n) + K(3*j-2:3*j,3*j-2:3*j);
M(3*i-2:3*i,3*i-2:3*i)= M11(:,:,n) + M(3*i-2:3*i,3*i-2:3*i);
M(3*i-2:3*i,3*j-2:3*j)= M12(:,:,n);
M(3*j-2:3*j,3*i-2:3*i)= M21(:,:,n);
M(3*j-2:3*j,3*j-2:3*j)= M22(:,:,n) + M(3*j-2:3*j,3*j-2:3*j);
end

3 comentarios

Azzi Abdelmalek
Azzi Abdelmalek el 31 de Jul. de 2012
Editada: Azzi Abdelmalek el 31 de Jul. de 2012
what are NN NE Dir,..?
Walter Roberson
Walter Roberson el 31 de Jul. de 2012
Azzi is right: the information you have given us does not allow us to determine whether 3*i-2 to 3*i and 3*j-2 to 3*j are within the size of K and M; we also cannot tell that the third dimension of K* and M* are at least as big as NE.
Jonathan
Jonathan el 31 de Jul. de 2012
Thank you for your suggestions. I am a nubie at Matlab and I'm trying to learn it as fast as I can. In response to your questions, NN and NE are the number of nodes and number of elements respectively.
I will try to post most of the code for you to see what I'm talking about.
NN = xlsread('test','Input','B2');
coord = xlsread('test','Input','A4:C8');
for j = 1:NN
i = coord(j,1);
x(i) = coord(j,2);
y(i) = coord(j,3);
% plot(x,y)
% hold on
end
NE = xlsread('test','Input','F2');
Pro = xlsread('test','Input','E4:K7');
for j = 1:NE
i = Pro(j,1);
I(i) = Pro(j,4);
A(i) = Pro(j,5);
E(i) = Pro(j,6);
m_bar(i) = Pro(j,7);
Dir(i,1) = i;
Dir(i,2) = Pro(j,2);
Dir(i,3) = Pro(j,3);
end
NC = xlsread('test','Input','N2');
cons = xlsread('test','Input','M4:O6');
for i = 1:NE
L(i) = sqrt((x(Dir(i,3))-(x(Dir(i,2))))^2 + (y(Dir(i,3))-(y(Dir(i,2))))^2);
Cos(i) = ((x(Dir(i,3))-(x(Dir(i,2)))))/L(i);
Sin(i) = ((y(Dir(i,3))-(y(Dir(i,2)))))/L(i);
end
mass_kind = 0;
for i = 1:NE
S(i) = (E(i)*A(i))/L(i)^3;
k11(:,:,i) = S(i)*[A(i)*L(i)^2/I(i) 0 0;0 12 6*L(i);0 6*L(i) 4*L(i)^2];
k12(:,:,i) = S(i)*[-A(i)*L(i)^2/I(i) 0 0;0 -12 6*L(i);0 -6*L(i) 2*L(i)^2];
k21(:,:,i) = S(i)*[-A(i)*L(i)^2/I(i) 0 0;0 -12 -6*L(i);0 6*L(i) 2*L(i)^2];
k22(:,:,i) = S(i)*[A(i)*L(i)^2/I(i) 0 0;0 12 -6*L(i);0 -6*L(i) 4*L(i)^2];
if mass_kind == 0
Q(i) = m_bar(i)*L(i)/420;
m11(:,:,i) = Q(i)*[140 0 0;0 156 22*L(i);0 22*L(i) 4*L(i)^2];
m12(:,:,i) = Q(i)*[70 0 0;0 54 -13*L(i);0 13*L(i) -3*L(i)^2];
m21(:,:,i) = Q(i)*[70 0 0;0 54 13*L(i);0 -13*L(i) -3*L(i)^2];
m22(:,:,i) = Q(i)*[140 0 0;0 156 -22*L(i);0 -22*L(i) 4*L(i)^2];
else
Q(i) = m_bar(i)*L(i)/2;
m11(:,:,i) = Q(i)*eye(3,3); m11(3,3,i) = 0;
m12(:,:,i) = Q(i)*eye(3,3);
m21(:,:,i) = Q(i)*eye(3,3);
m22(:,:,i) = Q(i)*eye(3,3); m22(3,3,i) = 0;
end
end
for n = 1:NE
T(:,:,n) = [Cos(n) Sin(n) 0;-Sin(n) Cos(n) 0;0 0 1];
for i = 1:3
for j = 1:3
Tt(i,j,n) = T(j,i,n);
end
end
end
for i = 1:NE
K11 = Tt(:,:,i)*k11(:,:,i)*T(:,:,i);
K12 = Tt(:,:,i)*k12(:,:,i)*T(:,:,i);
K21 = Tt(:,:,i)*k21(:,:,i)*T(:,:,i);
K22 = Tt(:,:,i)*k22(:,:,i)*T(:,:,i);
M11 = Tt(:,:,i)*m11(:,:,i)*T(:,:,i);
M12 = Tt(:,:,i)*m12(:,:,i)*T(:,:,i);
M21 = Tt(:,:,i)*m21(:,:,i)*T(:,:,i);
M22 = Tt(:,:,i)*m22(:,:,i)*T(:,:,i);
end
K = zeros(3*NN,3*NN);
M = zeros(3*NN,3*NN);
for n = 1:NE
i = Dir(n,2);
j = Dir(n,3);
K(3*i-2:3*i,3*i-2:3*i)= K11(:,:,n) + K(3*i-2:3*i,3*i-2:3*i);
K(3*i-2:3*i,3*j-2:3*j)= K12(:,:,n);
K(3*j-2:3*j,3*i-2:3*i)= K21(:,:,n);
K(3*j-2:3*j,3*j-2:3*j)= K22(:,:,n) + K(3*j-2:3*j,3*j-2:3*j);
M(3*i-2:3*i,3*i-2:3*i)= M11(:,:,n) + M(3*i-2:3*i,3*i-2:3*i);
M(3*i-2:3*i,3*j-2:3*j)= M12(:,:,n);
M(3*j-2:3*j,3*i-2:3*i)= M21(:,:,n);
M(3*j-2:3*j,3*j-2:3*j)= M22(:,:,n) + M(3*j-2:3*j,3*j-2:3*j);
end

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 31 de Jul. de 2012
Editada: Jan el 31 de Jul. de 2012
Use the debugger again:
dbstop if error
Then start the code. Matlab stops when the error occurs and you can evaluate the parts of the currently processed line in the command window, e.g.:
K11(:, :, n)
K(3*i-2:3*i, 3*i-2:3*i)
Then you will find out, which of the statements cause the error. Btw. the responsible line is shown in the error message also, and therefore it is a good idea to post the complete error messages, if you want help in the forum.
Another hint: If you use temporary variables for the index expressions, the code will look cleaner:
a = 3*i-2;
b = 3*i;
c = 3*j-2;
d = 3*j;
Beside the reduction of runtime, the improved readability will allow for an easier debugging.

3 comentarios

Jonathan
Jonathan el 31 de Jul. de 2012
Thank you for your suggestions. I will try to clean up the code so its easier to read.
Jonathan
Jonathan el 31 de Jul. de 2012
Here is the cleaned up version of code.
dbstop if error
%input data
NN = xlsread('test','Input','B2'); %Input Number of Nodes
coord = xlsread('test','Input','A4:C8'); %Coordinates
for j = 1:NN
i = coord(j,1);
x(i) = coord(j,2);
y(i) = coord(j,3);
% plot(x,y) %Plots OK
% hold on
end
NE = xlsread('test','Input','F2'); %Number of Elements
Pro = xlsread('test','Input','E4:K7'); %Properties of Elements
for j = 1:NE
i = Pro(j,1);
I(i) = Pro(j,4);
A(i) = Pro(j,5);
E(i) = Pro(j,6);
m_bar(i) = Pro(j,7);
Dir(i,1) = i;
Dir(i,2) = Pro(j,2);
Dir(i,3) = Pro(j,3);
end
NC = xlsread('test','Input','N2'); %Number of Constraints
cons = xlsread('test','Input','M4:O6'); %Constraints
%Length of Elements
for i = 1:NE
L(i) = sqrt((x(Dir(i,3))-(x(Dir(i,2))))^2 + (y(Dir(i,3))-(y(Dir(i,2))))^2);
Cos(i) = ((x(Dir(i,3))-(x(Dir(i,2)))))/L(i);
Sin(i) = ((y(Dir(i,3))-(y(Dir(i,2)))))/L(i);
end
%Insert here for nodal loads
mass_kind = 0;
%Element Stiffness/Mass Matrics
for i = 1:NE
S(i) = (E(i)*A(i))/L(i)^3;
k11(:,:,i) = S(i)*[A(i)*L(i)^2/I(i) 0 0;0 12 6*L(i);0 6*L(i) 4*L(i)^2];
k12(:,:,i) = S(i)*[-A(i)*L(i)^2/I(i) 0 0;0 -12 6*L(i);0 -6*L(i) 2*L(i)^2];
k21(:,:,i) = S(i)*[-A(i)*L(i)^2/I(i) 0 0;0 -12 -6*L(i);0 6*L(i) 2*L(i)^2];
k22(:,:,i) = S(i)*[A(i)*L(i)^2/I(i) 0 0;0 12 -6*L(i);0 -6*L(i) 4*L(i)^2];
if mass_kind == 0
Q(i) = m_bar(i)*L(i)/420;
m11(:,:,i) = Q(i)*[140 0 0;0 156 22*L(i);0 22*L(i) 4*L(i)^2];
m12(:,:,i) = Q(i)*[70 0 0;0 54 -13*L(i);0 13*L(i) -3*L(i)^2];
m21(:,:,i) = Q(i)*[70 0 0;0 54 13*L(i);0 -13*L(i) -3*L(i)^2];
m22(:,:,i) = Q(i)*[140 0 0;0 156 -22*L(i);0 -22*L(i) 4*L(i)^2];
else
Q(i) = m_bar(i)*L(i)/2;
m11(:,:,i) = Q(i)*eye(3,3); m11(3,3,i) = 0;
m12(:,:,i) = Q(i)*eye(3,3);
m21(:,:,i) = Q(i)*eye(3,3);
m22(:,:,i) = Q(i)*eye(3,3); m22(3,3,i) = 0;
end
end
%Transformation Matrices
for n = 1:NE
T(:,:,n) = [Cos(n) Sin(n) 0;-Sin(n) Cos(n) 0;0 0 1];
for i = 1:3
for j = 1:3
Tt(i,j,n) = T(j,i,n);
end
end
end
%Transformation from Local to Global Coordinates
for i = 1:NE
K11 = Tt(:,:,i)*k11(:,:,i)*T(:,:,i);
K12 = Tt(:,:,i)*k12(:,:,i)*T(:,:,i);
K21 = Tt(:,:,i)*k21(:,:,i)*T(:,:,i);
K22 = Tt(:,:,i)*k22(:,:,i)*T(:,:,i);
M11 = Tt(:,:,i)*m11(:,:,i)*T(:,:,i);
M12 = Tt(:,:,i)*m12(:,:,i)*T(:,:,i);
M21 = Tt(:,:,i)*m21(:,:,i)*T(:,:,i);
M22 = Tt(:,:,i)*m22(:,:,i)*T(:,:,i);
end
%Global/Mass Matrices
K = zeros(3*NN,3*NN);
M = zeros(3*NN,3*NN);
for n = 1:NE
i = Dir(n,2);
j = Dir(n,3);
a = 3*i-2;
b = 3*i;
c = 3*j-2;
d = 3*j;
K(a:b,a:b)= K11(:,:,n) + K(a:b,a:b);
K(a:b,c:d)= K12(:,:,n);
K(c:d,a:b)= K21(:,:,n);
K(c:d,c:d)= K22(:,:,n) + K(c:d,c:d);
M(a:b,a:b)= M11(:,:,n) + M(a:b,a:b);
M(a:b,c:d)= M12(:,:,n);
M(c:d,a:b)= M21(:,:,n);
M(c:d,c:d)= M22(:,:,n) + M(c:d,c:d);
end
Jonathan
Jonathan el 31 de Jul. de 2012
The error message as shown in the command window. I appreciate your help with this program.
??? Index exceeds matrix dimensions.
Error in ==> main3 at 100
K(a:b,a:b)= K11(:,:,n) + K(a:b,a:b);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Object Properties en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 31 de Jul. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by