Can someone help me make a function for determining a global stiffness matrix for a truss?

9 views (last 30 days)
I am confused on where to start and I can't get function command to not have errors.
  2 Comments
Geoff Hayes
Geoff Hayes on 4 Feb 2016
Benjamin - please clarify what you mean by I can't get function command to not have errors. If you have coded a function then please post it with the errors that you are observing so that we can try to help you understand where the problem is.
KSSV
KSSV on 4 Feb 2016
Are you using Finite element Method to get stiffness matrix of truss? You will not get a function inbuilt in MATLAB to get stiffness, you have to go through the equations and write you code depending on the numerical technique you are following.

Sign in to comment.

Answers (1)

Nagulavancha Sai Preetham
Nagulavancha Sai Preetham on 18 Jun 2022
clear
ne = input("No Of Elements : ");
n = input("No Of Nodes : ");
A = input("Enter A in m^2 : ");
E = input("Enter E kN/m^2 : ");
K(2*n, 2*n) = 0;
XY(n,2)=0;
for i = 1 : n
XY(i,1) = input("X Coordinates of Node " + i + " : ");
XY(i,2) = input("Y Coordinates of Node " + i + " : ");
end
Ele(ne, 4) = 0;
for i = 1 : ne
Ele(i,1) = input("Start Node of Element " + i + " : ");
Ele(i,2) = input("End Node of Element " + i + " : ");
Ele(i,3) = ((XY(Ele(i,1),1)-XY(Ele(i,2),1))^2 + (XY(Ele(i,1),2)-XY(Ele(i,2),2))^2 )^(1/2);
Ele(i,4) = atan((XY(Ele(i,1),2)-XY(Ele(i,2),2))/(XY(Ele(i,1),1)-XY(Ele(i,2),1)));
end
for i = 1 : ne
K(Ele(i,1)*2-1,Ele(i,1)*2-1) = K(Ele(i,1)*2-1,Ele(i,1)*2-1) + cos(Ele(i,4))^2*(A*E/Ele(i,3));
K(Ele(i,1)*2-1,Ele(i,1)*2) = K(Ele(i,1)*2-1,Ele(i,1)*2) + cos(Ele(i,4))*sin(Ele(i,4))*(A*E/Ele(i,3));
K(Ele(i,1)*2-1,Ele(i,2)*2-1) = K(Ele(i,1)*2-1,Ele(i,2)*2-1) - cos(Ele(i,4))^2*(A*E/Ele(i,3));
K(Ele(i,1)*2-1,Ele(i,2)*2) = K(Ele(i,1)*2-1,Ele(i,2)*2) - cos(Ele(i,4))*sin(Ele(i,4))*(A*E/Ele(i,3));
K(Ele(i,1)*2,Ele(i,1)*2-1) = K(Ele(i,1)*2,Ele(i,1)*2-1) + cos(Ele(i,4))*sin(Ele(i,4))*(A*E/Ele(i,3));
K(Ele(i,1)*2,Ele(i,1)*2) = K(Ele(i,1)*2,Ele(i,1)*2) + sin(Ele(i,4))^2*(A*E/Ele(i,3));
K(Ele(i,1)*2,Ele(i,2)*2-1) = K(Ele(i,1)*2,Ele(i,2)*2-1) - cos(Ele(i,4))*sin(Ele(i,4))*(A*E/Ele(i,3));
K(Ele(i,1)*2,Ele(i,2)*2) = K(Ele(i,1)*2,Ele(i,2)*2) - sin(Ele(i,4))^2*(A*E/Ele(i,3));
K(Ele(i,2)*2-1,Ele(i,1)*2-1) = K(Ele(i,2)*2-1,Ele(i,1)*2-1) - cos(Ele(i,4))^2*(A*E/Ele(i,3));
K(Ele(i,2)*2-1,Ele(i,1)*2) = K(Ele(i,2)*2-1,Ele(i,1)*2) - cos(Ele(i,4))*sin(Ele(i,4))*(A*E/Ele(i,3));
K(Ele(i,2)*2-1,Ele(i,2)*2-1) = K(Ele(i,2)*2-1,Ele(i,2)*2-1) + cos(Ele(i,4))^2*(A*E/Ele(i,3));
K(Ele(i,2)*2-1,Ele(i,2)*2) = K(Ele(i,2)*2-1,Ele(i,2)*2) + cos(Ele(i,4))*sin(Ele(i,4))*(A*E/Ele(i,3));
K(Ele(i,2)*2,Ele(i,1)*2-1) = K(Ele(i,2)*2,Ele(i,1)*2-1) - cos(Ele(i,4))*sin(Ele(i,4))*(A*E/Ele(i,3));
K(Ele(i,2)*2,Ele(i,1)*2) = K(Ele(i,2)*2,Ele(i,1)*2) - sin(Ele(i,4))^2*(A*E/Ele(i,3));
K(Ele(i,2)*2,Ele(i,2)*2-1) = K(Ele(i,2)*2,Ele(i,2)*2-1) + cos(Ele(i,4))*sin(Ele(i,4))*(A*E/Ele(i,3));
K(Ele(i,2)*2,Ele(i,2)*2) = K(Ele(i,2)*2,Ele(i,2)*2) + sin(Ele(i,4))^2*(A*E/Ele(i,3));
end
k = input("Enter Number of Q known : ");
Qk(k,1) = 0;
Dk(2*n-k,1) = 0;
Qu(2*n-k,1) = 0;
Du(k,1) = 0;
for i = 1 : k
Qk(i,1) = input("Enter known Q " + i + " in kN/m^2 : " );
end
for i = k+1 : 1 : 2*n
Dk(i,1) = input("Enter known D " + i + " in m : " );
end
k1 = K(1:k,1:k);
k2 = K(k+1:2*n,1:k);
Du = (k1^-1)*Qk;
Qu = k2*Du;
disp("ELement Information");
disp(Ele);
disp("Stiffness Matrix of System");
disp(K);
disp("D unknown in mm");
disp(1000*Du);
disp("Q unknown");
disp(Qu);
K is the Global Stiffness Matrix. Just make sure you number the nodes and members beforehand and enter the values in the right order. The code also returns the reaction forces and displacements aswell.

Community Treasure Hunt

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

Start Hunting!