Asked by Charlie
on 18 Mar 2013

Hi all, I'm new to the forums. I'm having issues with my code and I have no idea where to go to. My instructor advised I tried the helpdesk and this was the only place where it seemed right to ask. This is my code:

function [Kstruct] = HomeworkCode(E,A,L1,L3,h)

%establish geometry

L2 = sqrt((L3-L1)^2+h^2); L4 = sqrt(L1^2+h^2);

%Local, trans, global element stiffness matrices

Kprime0 = (E*A/L1)*[1,-1;-1,1]; Trans0 = [cos(0),sin(0),0,0;0,0,cos(0),sin(0)]; K0 = Trans0.'*Kprime0*Trans0

Kprime2 = (E*A/L2)*[1,-1;-1,1]; Trans2 = [(L3-L1)/L2,h/L2,0,0;0,0,(L3-L1)/L2,h/L2]; K2 = Trans2.'*Kprime2*Trans2

Kprime3 = (E*A/L3)*[1,-1;-1,1]; Trans3 = [cos(0),sin(0),0,0;0,0,cos(0),sin(0)]; K3 = Trans3.'*Kprime3*Trans3

Kprime4 = (E*A/L4)*[1,-1;-1,1]; Trans4 = [L1/L4,h/L4,0,0;0,0,L1/L4,h/L4]; K4 = Trans4.'*Kprime4*Trans4

%Local structure stiffness Kstruct = [K0(3,3)+K2(1,1)+K4(3,3),K0(3,4)+K2(1,2)+K4(3,4),K2(1,3),K2(1,4),K0(3,1),K0(3,2),K4(3,1),K4(3,2); K0(4,3)+K2(2,1)+K4(4,3),K0(4,4)+K2(2,2)+K4(4,4),K2(2,3),K2(2,4),K0(4,1),K0(4,2),K4(4,1),K4(4,2); K2(3,1),K2(3,2),K2(3,3)+K3(3,3),K2(3,4)+K3(3,4),0,0,K3(3,1),K3(3,2); K2(4,1),K2(4,2),K2(4,3)+K3(4,3),K2(4,4)+K3(4,4),0,0,K3(4,1),K3(4,2); K0(1,3),K0(1,4),0,0,K0(1,1),K0(1,2),0,0; K0(2,3),K0(2,4),0,0,K0(2,1),K0(2,2),0,0; K4(1,3),K4(1,4),K3(1,3),K3(1,4),0,0,K4(1,1)+K3(1,1),K4(1,2)+K3(1,2); K4(2,3),K4(2,4),K3(2,3),K3(2,4),0,0,K4(2,1)+K3(2,1),K4(2,2)+K3(2,2)]

inv(Kstruct)

Everything seems fine for me so far up until I try to take the inverse of the variable Kstruct. I've looked over the lines 5 times and even rewrote it incase it was something I'm just ignoring by sight, but I'm still getting a singular matrix for my Kstruct. Any help on this issue would be greatly appreciated.

*No products are associated with this question.*

Answer by Jan Simon
on 19 Mar 2013

Edited by Jan Simon
on 19 Mar 2013

Please note, that it is not possible in theorie to give a suggestion for an improvement based on failing code only. How could we fix an error, when all we have is the code, which produces the error? We even do not know, if there is an error at all, because it could be a valid answer, that the matrix is (near to) singular. Does anything disallow this result?

I'm convinced, that terms like "K2(3,4)+K3(3,4)" attract typos as much as possible. Perhaps something like this would simplifiy the program optically, such that finally the problem becomes visible automatically:

... K4(1,3),K4(1,4),K3(1,3),K3(1,4),0,0,K4(1,1)+K3(1,1),K4(1,2)+K3(1,2); K4(2,3),K4(2,4),K3(2,3),K3(2,4),0,0,K4(2,1)+K3(2,1),K4(2,2)+K3(2,2)

==>

K4(1:2,3:4), K3(1:2,3:4), zeros(2), K4(1:2,1:2)+K3(1:2,1:2)

Opportunities for recent engineering grads.

## 1 Comment

## bym (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/67719#comment_137557

the structure stiffness matrix will be singular until you impose boundry conditions