# Creat a big matrix by adding elements from smaller matrices

2 views (last 30 days)
Emd Amin on 1 Jul 2020
Edited: Emd Amin on 2 Jul 2020
I have created eight 14x14 submatices (a,b,c,.....) using for loop. I want to create a bigger matrix (K) as such that the first submatrix "a" is the first 14x14 of M, the second matrix "b" starts at (8,8) and the overlaped elements (8:14,8:14) are the sum of the two elements, "c" starts at (15,15) the overlaped elements (15:21,15:21), ...etc.
Thanks

#### 1 Comment

Emd Amin on 1 Jul 2020
syms Px Mz1 Mz2 Mz3 Mz4 Mz5 Qz1 Qz2 Qz3 Qz4 Qz5 My1 My2 My3 My4 My5 Qy1...
Qy2 Qy3 Qy4 Qy5 Mx1 Mx2 Mx3 Mx4 Mx5 mx1 mx2 B1 B2 B3 B4 B5...
Kb1 Kt1 Kb2 Kt2 Kb3 Kt3 Kb4 Kt4
Mz=[Mz1 Mz2 Mz3 Mz4 Mz5]; Qz=[Qz1 Qz2 Qz3 Qz4 Qz5];Px=[Px];
My=[My1 My2 My3 My4 My5]; Qy=[Qy1 Qy2 Qy3 Qy4 Qy5];
Mx=[Mx1 Mx2 Mx3 Mx4 Mx4];
B=[B1 B2 B3 B4 B5];
ez=0; ey=0; z=0; y=0; z0=0; y0=0;
beta_y= 1/Iy*(z^3*area
beta_z= 1/Iz*(y^3*area
beta_w= 1/Cw*omega_n*(y^2+z^2)*area; %(3.21)
C0= rp_sequare
Cy=ey; Cz=ez-z0; %(3.27)
d=sym(zeros(7,7));e=sym(zeros(7,7));f=sym(zeros(7,7));K=sym(zeros(35,35));
m=0;
for i=1:4
%i is the number of nodes
j=i+1 ;
if i<=2
mx=Mx(1,2);
else
mx=Mx(1,4);
end
%2.1- d Matrix
d(6,2)=-0.1*Px;
d(6,4)=-1.1*Px*Cz-0.55*(My(1,i)-My(1,j))-0.1*Qz(1,i)*l-0.45*Qz(1,j)*l;
d(6,6)=d(5,5);
d(7,2)=-0.1*Px*Cz-0.05*(My(1,i)-My(1,j))-0.05*Qz(1,j)*l;
d(7,3)=0.1*Px*Cy+0.05*(Mz(1,i)-Mz(1,j))+0.05*Qy(1,j)*l;
d(7,4)=-0.1*Px*C0-(0.05*(My(1,i)-My(1,j))+0.05*Qz(1,i)*l)*beta_y...
-(0.05*(Mz(1,i)-Mz(1,j))+0.05*Qy(1,i)*l)*beta_z...
d;
%2.2- e matrix:
e(5,4)=0.1*Px*Cy+0.05*(Mz(1,i)-Mz(1,j))-0.05*Qy(1,i)*l+0.1*Qy(1,j)*l;
e(5,7)=Px*Cy*l/30+(Mz(1,i)-Mz(1,j))*l/60+Qy(1,i)*l^2/60;
e(6,2)=d(6,2);
e(6,4)=-0.1*Px*Cz-0.05*(My(1,i)-My(1,j))+0.05*Qz(1,i)*l-0.1*Qz(1,j)*l;
e(7,6)=-Px*Cz*l/30-(My(1,i)-My(1,j))*l/60-Qz(1,j)*l^2/60;
e;
%2.33 - f matrix
f(1,1)=0;f(2,2)=d(2,2);f(3,3)=d(3,3);f(4,4)=d(4,4);f(5,5)=d(5,5);
f(6,6)=d(6,6);
f(7,7)=2*Px*C0*l/15+((My(1,i)-My(1,j))*l/15+Qz(1,i)*l^2/20+Qz(1,j)*l^2/60)...
*beta_y+((Mz(1,i)-Mz(1,j))*l/15+Qz(1,i)*l^2/20+Qz(1,j)*l^2/60)*beta_z...
+(Kb1*(B(1,i)-B(1,j))*l+Kt1*mx*l^2)*beta_w;
f(4,2)=-e(4,2);f(2,4)=f(4,2);f(4,3)=-e(4,3);f(3,4)=f(4,3);f(5,3)=-d(5,3);
f(3,5)=f(5,3);
f;
KG=[d e;e f];
Kt=KE+KG
K=
I downsized it to 4 submatrices thinking that it will be easier, K matrix should be constructed from Kt, the first Kt(i=1) is the first 14x14 element of the K then Kt(i=2) starts at K(8,8) the overlaped elements should be Kt(i=1)+Kt(i=2) and so on....

Walter Roberson on 1 Jul 2020
part1 = blkdiag(a,c,e,g, zeros(7,7));
part2 = blkdiag(zeros(7,7),b,d,f,h);
K = part1 + part2;

Emd Amin on 1 Jul 2020
Thanks Walter, I appreciate your response. I added part of the code hoping that will give you aclear idea on what I want to do. Thanks again
Walter Roberson on 1 Jul 2020
If you need to build incrementally:
existing_matrix = [];
for counter = 1 : 4
... calculate Kg
existing_matrix(end+7,end+7) = 0; %extend the matrix
existing_matrix(end-13:end,end-13:end) = existing_matrix(end-13:end,end-13:end) + Kg;
end
Emd Amin on 2 Jul 2020
Thanks Walter, I really appreciate your help. the above code nailed it.