MATLAB Answers

deguchi
0

Error when calling a function : "Dimensions of arrays being concatenated are not consistent."

Asked by deguchi
on 8 May 2019
Latest activity Edited by deguchi
on 15 May 2019
Hey guys and girls :)
I am writing a Code for Finite Element calculation on beams.
function y = fun_BeamFrameStiff2D(E,A,I,cosa,sina,len)
b1 = A*cosa^2 + 12*I*sina^2/len^2;
b2 = A*sina^2 + 12*I*cosa^2/len^2;
b3 = (A-12*I/len^2)*cosa*sina;
b4 = 6*I*sina/len;
b5 = 6*I*cosa/len;
y = E/len*[b1 b3 -b4 -b1 - b3 -b4; b3 b2 b5 -b3 -b2 b5;...
-b4 b5 4*I b4 -b5 2*I; -b1 -b3 b4 b1 b3 b4;...
-b3 -b2 -b5 b3 b2 -b5; -b4 b5 2*I b4 -b5 4*I]
E=1;
A=1;
I=1;
% Abfrage der Koordinaten der Elemente
elements=input('Anzahl der Elemente')
sysnodes=input('Anzahl der Systemknoten')
nodes=zeros(sysnodes,2);
cords=zeros(2*elements,2);
for p=1:sysnodes
nodes(p,1)=input('Systemstartknoten des Element:')
nodes(p,2)=input('Systemendknoten des Element:')
end
for p=1:2*elements
cords(p,1)=input('x-Koordinate des Knotens i von Element i :')
cords(p,2)=input('y-Koordinate des Knotens i von Element i :')
end
lenx=zeros(elements,1);
leny=zeros(elements,1);
len=zeros(elements,1);
for p=1:elements
for q=1:2:2*elements-1
lenx(p)=cords(q+1,1)-cords(q,1)
leny(p)=cords(q+1,2)-cords(q,2)
len(p)=sqrt(lenx(p)^2+leny(p)^2)
end
end
sina=zeros(elements);
cosa=zeros(elements);
for p=1:elements
sina(p)=leny(p)/len(p);
cosa(p)=lenx(p)/len(p);
end
k1=fun_BeamFrameStiff2D(E,A,I,cosa(1),sina(1),len(1))
When I try to call the function, I always get the error mentioned in the title - whats wrong about this?
I thought about changing the inputs to cosa(elements), sina(elements).... but then when I tried I got the error "invalid expression. when calling a function or indexing a variable, use parentheses. otherwise check for mismatched delimiters."

  0 Comments

Sign in to comment.

1 Answer

Answer by Stephen Cobeldick on 8 May 2019
Edited by Stephen Cobeldick on 9 May 2019
 Accepted Answer

The full error message is this:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in fun_BeamFrameStiff2D (line 8)
y = E/len*[b1 b3 -b4 -b1 - b3 -b4; b3 b2 b5 -b3 -b2 b5;...
Note that it also shows you the line where the error was detected. In your case, the problem is caused by the unreliable usage of whitespace to delimit the elements of the matrix. This is unfortunately common, but very liable to bugs because whitespace has multiple meaning in this context: it can delimit array elements or it can separate binary operators and the arrays which they operate on. These two uses are easily confused and mixed up without noticing, which is what happened with your code:
y = E/len*[b1 b3 -b4 -b1 - b3 -b4; b3 b2 b5 -b3 -b2 b5;...
% ^ this is a binary operator on -b1 and b3, NOT a unary operator on b3
The solution is very simply to always use commas as the element delimiter, which makes the intent clear and any bugs much easier to locate:
function y = fun_BeamFrameStiff2D(E,A,I,cosa,sina,len)
b1 = A*cosa^2 + 12*I*sina^2/len^2;
b2 = A*sina^2 + 12*I*cosa^2/len^2;
b3 = (A-12*I/len^2)*cosa*sina;
b4 = 6*I*sina/len;
b5 = 6*I*cosa/len;
y = E / len * [...
+b1,+b3,-b4,-b1,-b3,-b4;...
+b3,+b2,+b5,-b3,-b2,+b5;...
-b4,+b5,4*I,+b4,-b5,2*I;...
-b1,-b3,+b4,+b1,+b3,+b4;...
-b3,-b2,-b5,+b3,+b2,-b5;...
-b4,+b5,2*I,+b4,-b5,4*I];
end
and tested:
>> fun_BeamFrameStiff2D(1,1,1,1,1,1)
ans =
13 -11 -6 -13 11 -6
-11 13 6 11 -13 6
-6 6 4 6 -6 2
-13 11 6 13 -11 6
11 -13 -6 -11 13 -6
-6 6 2 6 -6 4
>>

  3 Comments

Thank you man! Really appreciate your help! Wouldn't have seen this for years. Better using commas from now on :)
There is now another problem. If I type in the coordinates of the element nodes as follows:
0 0
200 0
200 0
0 -100
0 -100
0 0
the length of the three elements can't be the same. It is obvious, cause the three elements form a rectangular triangle. The three length should be 200, sqrt(200^2+(-100)^2), 10
best regards
p.s. If I should make a new thread, just comment on this!
@deguchi: do you have a new question?
Please remember to accept my answer if it resolves your original question.
Hey Stephen,
I accepeted your answer and voted it, thank you again!
I've got a new question now.
I want to write the programm in a way, so that I can adopt it easily to any beam problem.
Therefore I need to create sometimes 2 single stiffnessmatrices or 4 or sometimes three (depends on the amount of elements - for a crane like structure it can be 30 elements or more..)
So I do not want to write:
k1=funbeam...
k2=funbeam....
k3=funbeam....
k4=....
...
kn=......
  • I would love to get this in a loop done if possible :)
Is there a solution for this problem?
Second question:
E=1;
A=1;
I=1;
% Abfrage der Koordinaten der Elemente
dof=3;
elements=input('Anzahl der Elemente')
sysnodes=input('Anzahl der Systemknoten')
nodes=zeros(elements,2);
cords=zeros(2*elements,2);
for p=1:elements
nodes(p,1)=input('Systemstartknoten des Element:')
nodes(p,2)=input('Systemendknoten des Element:')
end
for p=1:2*elements
cords(p,1)=input('x-Koordinate des Knotens i von Element i :')
cords(p,2)=input('y-Koordinate des Knotens i von Element i :')
end
lenx=zeros(elements,1);
leny=zeros(elements,1);
len=zeros(elements,1);
for p=1:elements
lenx(p)=cords(2+(p-1)*2,1)-cords(1+(p-1)*2,1)
leny(p)=cords(2+(p-1)*2,2)-cords(1+(p-1)*2,2)
len(p)=sqrt(lenx(p)^2+leny(p)^2)
end
sina=zeros(elements);
cosa=zeros(elements);
for p=1:elements
sina(p)=leny(p)/len(p);
cosa(p)=lenx(p)/len(p);
end
k1=fun_BeamFrameStiff2D(E,A,I,cosa(1),sina(1),len(1))
k2=fun_BeamFrameStiff2D(E,A,I,cosa(2),sina(2),len(2))
K=zeros(sysnodes*dof,sysnodes*dof);
for p=1:elements
K=fun_BeamStiffAssembly2D(K,k1,nodes(p,1),nodes(p,2));
end
K
bound=zeros(sysnodes*dof,1);
for p=1:sysnodes*dof
bound(p,1)=input('Eingabe der Randbedingung (3 Freiheitsgrade je Systemknoten) - 0 FG ist gebunden, 1 ungebunden...; Reihenfolge ist ux, uy, phi:');
if bound(p,1)==0
K(:,p)=[];
K(p,:)=[];
end
end
K
anzNullen=sum(bound==0);
force=zeros(sysnodes*dof-anzNullen,1);
for p=1:sysnodes*dof-anzNullen
force(p,1)=input('Angabe der Kräfte und Momente an den Knoten(Fx,Fy,M):')
end
u=K\force
This is the complete source code till now, without the used functions.
I tried it now for 2 straight linear beams, with 3 nodes. But when I came to the boundary conditions and tried to typed them in, I couldnt write the 9th boundary condition cause Matlab stops with the error:
Matrix index is out of range for deletion.
Error in Beam_solution (line 52)
K(:,p)=[];
Maybe you can help me out here as well :)
Thank you!

Sign in to comment.