adding different matrices with for loop into another matrix

조회 수: 13 (최근 30일)
yaman arslanca
yaman arslanca 2020년 5월 23일
답변: yaman arslanca 2020년 6월 20일
Hello,
I am trying to make a stiffness matrix for a finite element problem and my question is; I have 10 element stiffness matrices, these are local matrices and they are 4x4 and I want to assemble them into a global stiffness matrix. So basically, what I am trying to below is I am trying to add them into a 22x22 zeros matrix but also I want to add them into different rows and columns with coinciding values as you can see in the picture. I have also tried something in the below code but I am getting buch of zeros. I can actually do it with like 30 lines of codes I guess but I just want to know a practical way to do it.
(Also I'd appreciate any simplification for my function code too.)
Thanks.
%%% 4-node quad plane stress element (assuming 10x1 elements)
[K1]=shape_function(0,0); %I am getting these matrices through a function command
[K4]=shape_function(1,1);
[K7]=shape_function(2,2);
[K10]=shape_function(3,3);
[K13]=shape_function(4,4);
[K16]=shape_function(5,5);
[K19]=shape_function(6,6);
[K22]=shape_function(7,7);
[K25]=shape_function(8,8);
[K28]=shape_function(9,9);
K=zeros(22,22)
for i=1:3:19
Kd=zeros(22,22);
Kd([i:i+3],[i:i+3])=K(i);
K=K+Kd;
end
display(K)
%shape function calculator
function [K] = shape_function(x1,y1)
syms x y
E=1000;
y4=y1+1;
x2=x1+1;
A=(x2-x1)*(y4-y1);
N1=(x-x2)*(y-y4)/A;
N2=(x-x1)*(y-y4)/-A;
N3=(x-x1)*(y-y1)/A;
N4=(x-x2)*(y-y1)/-A;
N=[N1 N2 N3 N4];
B(1,:)=diff(N,x);
B(2,:)=diff(N,y);
Kx=int((transpose(B)*A*E*B),x,x1,x2);
K=int(Kx,y,y1,y4);
end
What I want to achieve:

채택된 답변

yaman arslanca
yaman arslanca 2020년 6월 20일
Since I have got help from here many times, now I will share the answer that I have found from a youtube video, below code basically do the trick :
% the little trick
K1=[1 2 ;3 4];
K=zeros(5);
for i=1:4
K(i:i+1,i:i+1)=K(i:i+1,i:i+1)+K1;
end
display(K)
It will display:
K =
1 2 0 0 0
3 5 2 0 0
0 3 5 2 0
0 0 3 5 2
0 0 0 3 4
This can be quite useful in FEA codes

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Stress and Strain에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by