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;...
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
>>
Best Answer