Error when calling a function : "Dimensions of arrays being concatenated are not consistent."
1 view (last 30 days)
Show older comments
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
Accepted Answer
Stephen23
on 8 May 2019
Edited: Stephen23
on 9 May 2019
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
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!