matlab三维三⾓⽹格,有限元分析利⽤matlab的gplot函数实现
三维划分⽹格的⽅法
在开发有限元程序的时候,⾸先遇到的问题就是实现⾃动划分⽹格(meshgrid),matlab可以实现这⼀功能,它的基本原理就是根据图论和邻接矩阵的原理利⽤gplot函数和sparse稀疏矩阵实现的,但是它只能实现⼆维!本⼈重新编写了⼀个程序,可以实现三维的⽹格⾃动划分(包括三⾓形单元、矩形单元和六⾯体单元),程序如下:
a1=zeros(m1*n1);
b1=zeros(m1*n1);
XY1=zeros(m1*n1,2);
x1=zeros(1,m1*n1);
y1=zeros(1,m1*n1);
z1=zeros(1,m1*n1);
for k=1:n1
for i=1:m1
x1(i+(k-1)*m1)=xmin1+(k-1)*(xmax1-xmin1)/(n1-1);
z1(i+(k-1)*m1)=zmin1+(i-1)*(zmax1-zmin1)/(m1-1);
end
end
clear k i
a1(m1,m1)=2;
a1((n1-1)*m1+1,(n1-1)*m1+1)=2;
a1(1,1)=3;
a1(n1*m1,n1*m1)=3;
for i=1:(n1-2)
a1(i*m1+1,i*m1+1)=4;
a1((i+1)*m1,(i+1)*m1)=4;
end
clear i
for j=2:(m1-1)
a1(j,j)=4;
a1((n1-1)*m1+j,(n1-1)*m1+j)=4;
end
clear j
for i=1:(n1-2)
for j=2:(m1-1)
a1(i*m1+j,i*m1+j)=6;
end
end
clear i j
a1=sparse(a1);
for i=1:(n1-1)*m1-1
b1(i,i+1)=-1;
end
clear i
for i=1:(n1-1)*m1
b1(i,i+m1)=-1;
end
clear i
for i=1:(n1-1)*m1-1
b1(i,i+m1+1)=-1;
end
clear i
for k=1:(n1-1)
for h=1:(n1-1)
b1(m1*k,m1*h+1)=0;
end
end
clear k h
for i=(n1-1)*m1+1:n1*m1-1 b1(i,i+1)=-1;
end
clear i
b1=sparse(b1);
b1=a1+b1'+b1;
clear a1
a1=sparse(b1);
用subplot函数clear b1
XY1=[x1',y1',z1'];
a2=zeros(m2*n2);
b2=zeros(m2*n2);
XY2=zeros(m2*n2,2);
x2=0.5*ones(1,m2*n2);
y2=zeros(1,m2*n2);
z2=zeros(1,m2*n2);
for k=1:n2
for i=1:m2
z2(i+(k-1)*m2)=zmin2+(k-1)*(zmax2-zmin2)/(n2-1); y2(i+(k-1)*m2)=ymin2+(i-1)*(ymax2-ymin2)/(m2-1); end
end
clear k i
a2(m2,m2)=2;
a2((n2-1)*m2+1,(n2-1)*m2+1)=2;
a2(1,1)=3;
a2(n2*m2,n2*m2)=3;
for i=1:(n2-2)
a2(i*m2+1,i*m2+1)=4;
a2((i+1)*m2,(i+1)*m2)=4;
end
clear i
for j=2:(m2-1)
a2(j,j)=4;
a2((n2-1)*m2+j,(n2-1)*m2+j)=4;
end
clear j
for i=1:(n2-2)
for j=2:(m2-1)
a2(i*m2+j,i*m2+j)=6;
end
end
clear i j
a2=sparse(a2);
for i=1:(n2-1)*m2-1
end
clear i
for i=1:(n2-1)*m2
b2(i,i+m2)=-1;
end
clear i
for i=1:(n2-1)*m2-1
b2(i,i+m2+1)=-1;
end
clear i
for k=1:(n2-1)
for h=1:(n2-1)
b2(m2*k,m2*h+1)=0;
end
end
clear k h
for i=(n2-1)*m2+1:n2*m2-1 b2(i,i+1)=-1;
end
clear i
b2=sparse(b2);
b2=a2+b2'+b2;
clear a2
a2=sparse(b2);
clear b2
XY2=[x2',y2',z2'];
a3=zeros(m3*n3);
b3=zeros(m3*n3);
XY3=zeros(m3*n3,2);
x3=1.5*ones(1,m3*n3);
y3=zeros(1,m3*n3);
z3=zeros(1,m3*n3);
for k=1:n3
z3(i+(k-1)*m3)=zmin3+(k-1)*(zmax3-zmin3)/(n3-1); y3(i+(k-1)*m3)=ymin3+(i-1)*(ymax3-ymin3)/(m3-1); end
end
clear k i
a3(m3,m3)=2;
a3((n3-1)*m3+1,(n3-1)*m3+1)=2;
a3(1,1)=3;
a3(n3*m3,n3*m3)=3;
for i=1:(n3-2)
a3(i*m3+1,i*m3+1)=4;
a3((i+1)*m3,(i+1)*m3)=4;
end
clear i
for j=2:(m3-1)
a3(j,j)=4;
a3((n3-1)*m3+j,(n3-1)*m3+j)=4;
end
clear j
for i=1:(n3-2)
for j=2:(m3-1)
a3(i*m3+j,i*m3+j)=6;
end
end
clear i j
a3=sparse(a3);
for i=1:(n3-1)*m3-1
b3(i,i+1)=-1;
end
clear i
for i=1:(n3-1)*m3
b3(i,i+m3)=-1;
end
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论