Assembling Global Stiffness Matrix

249 views (last 30 days)
Sharanya
Sharanya on 12 Dec 2022
Answered: Arif Hoq on 13 Dec 2022
I am trying to make a global stiffness matrix using a for loop from several smaller (4X4) matrices. I was able to get a loop to run for 2X2 matrices, but when I increase the number of rows and colums in my smaller matrices it no longer works. I want the loop to run when the kn matrices is rand(4). This is what I have currently:
k1=rand(2);
k2=rand(2);
k3=rand(2);
C = {k1,k2,k3};
N = numel(C);
M = zeros(1+N,1+N);
for k = 1:N
M(k:k+1,k:k+1) = M(k:k+1,k:k+1)+C{k};
end
disp(M)

Answers (2)

Torsten
Torsten on 12 Dec 2022
This is analogous to your 2x2 code.
I don't know if it's the right way to code M.
k1=rand(4);
k2=rand(4);
k3=rand(4);
C = {k1,k2,k3};
N = numel(C);
M = zeros(3+N,3+N);
for k = 1:N
M(k:k+3,k:k+3) = M(k:k+3,k:k+3)+C{k};
end
disp(M)
0.6812 0.4903 0.5574 0.1710 0 0 0.9156 1.2886 0.9432 0.6481 0.5245 0 0.2805 1.4433 0.6168 1.3751 0.6339 0.7170 0.1670 1.5541 0.9690 1.3091 0.4106 0.6047 0 0.4948 0.9290 0.3031 0.9958 0.0024 0 0 0.1797 0.8201 0.7794 0.7742
  2 Comments
Sharanya
Sharanya on 12 Dec 2022
This is ouputting a 23X23 matrix, but I only want a 20x20 matrix. Each stiff(n) is a 4x4 matrix
k1=stiff(1);
k2=stiff(2);
k3=stiff(3);
k4=stiff(4);
k5=stiff(5);
k6=stiff(6);
k7=stiff(7);
k8=stiff(8);
k9=stiff(9);
k10=stiff(10);
k11=stiff(11);
k12=stiff(12);
k13=stiff(13);
k14=stiff(14);
k15=stiff(15);
k16=stiff(16);
k17=stiff(17);
k18=stiff(18);
k19=stiff(19);
k20=stiff(20);
C = {k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16,k17,k18,k19,k20};
N=numel(C)
M = zeros(3+N,3+N);
for k = 1:N
M(k:k+3,k:k+3) = M(k:k+3,k:k+3)+C{k};
end
disp(M)
Torsten
Torsten on 13 Dec 2022
Edited: Torsten on 13 Dec 2022
You will have to explore how the global stiffness matrix is computed from the C's.

Sign in to comment.


Arif Hoq
Arif Hoq on 13 Dec 2022
% creating stiff matrix
for i=1:20
stiff{i}=randi(100,4,4);
end
k1=stiff(1);
k2=stiff(2);
k3=stiff(3);
k4=stiff(4);
k5=stiff(5);
k6=stiff(6);
k7=stiff(7);
k8=stiff(8);
k9=stiff(9);
k10=stiff(10);
k11=stiff(11);
k12=stiff(12);
k13=stiff(13);
k14=stiff(14);
k15=stiff(15);
k16=stiff(16);
k17=stiff(17);
k18=stiff(18);
k19=stiff(19);
k20=stiff(20);
C = [k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16,k17,k18,k19,k20];
N=numel(C);
M = zeros(N,N);
for k = 1:N-3
M(k:k+3,k:k+3) = M(k:k+3,k:k+3)+C{k};
end
disp(M)
4 5 37 70 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 70 112 133 126 50 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 63 116 157 136 24 98 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 107 167 135 245 149 49 0 0 0 0 0 0 0 0 0 0 0 0 0 0 35 161 122 166 181 91 42 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 98 103 176 149 110 43 0 0 0 0 0 0 0 0 0 0 0 0 0 0 66 125 129 144 115 124 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 87 77 157 83 165 153 63 0 0 0 0 0 0 0 0 0 0 0 0 0 0 39 126 107 213 201 53 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24 92 190 87 155 74 68 0 0 0 0 0 0 0 0 0 0 0 0 0 0 68 102 85 244 165 110 54 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 90 155 241 204 87 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 42 116 177 219 134 90 58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 40 34 238 245 96 147 49 0 0 0 0 0 0 0 0 0 0 0 0 0 0 43 71 221 165 133 101 73 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 145 115 142 158 61 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 29 71 153 274 146 53 21 0 0 0 0 0 0 0 0 0 0 0 0 0 0 88 141 119 116 173 67 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 78 80 110 90 37 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 93 39 5 96

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!