MATLAB two-dimensional finite element program (triangle unit)

  1. First of all, I made reference to the principle of bloggers, Bo speaker is very detailed, but some details are not mentioned in detail in this reference to himself in after the blogger's own matlab code programming process record if someone look bloggers already understand the article, the article then I might these visitors redundant. https://blog.csdn.net/lusongno1/article/details/81125167
  2. Bloggers are painted himself grid, COMSOL has exported grid function, by means of a COMSOL so I painted a simple model and then export the grid and then MATLAB processing, feel the size of the grid can be customized, so choose this method.
  3. Processing grid, because the derived COMSOL grid has its own format, and which boundary cell has the disadvantage that the node does not distinguish between the edge of the piece is given, this problem due to the boundary are set to 0, so no influence, but if Di type boundary value is nonzero, the COMSOL will not tell you which edge to a non-zero value. Of course, the boundaries are set to 0, the two-dimensional finite element program as the entry-sufficient. As mesh data format
  4. Entered, solving entitled:
  5. At this point I need to explain, I hope we will not commit the same mistake, I used to think the aid of programming solver functions, in fact, so many steps just like to know, from beginning to end without solving the function again, feeling that with his manually solving no different. This idea is defective, your whole program is actually written in the derivation procedure, even if you come how each step, the next is not clear how to get there, how about it compiled program, so the entire first equation derivation again is the foundation. In my understanding it seems, programming that is considered integral unit assembly, useful for solving part of other steps which are paving the way.
  6. The derivation process:
  7. %读入网格
    clear all;
    filename='mm1.txt';
    %%节点坐标
    [node_num]=textread(filename,'%n',1);%节点个数
    n_coor=zeros(node_num,2);%节点坐标
    m=1;
    [n1,n2]=textread(filename,'%n%n','headerlines',1);
    n_coor(:,1)=n1(1:node_num);
    n_coor_x=n1(1:node_num);
    n_coor_y=n2(1:node_num);
    n_coor(:,2)=n2(1:node_num);
    %%边界点
    bou_num=n1(node_num+1);
    boundary=zeros(bou_num,2);
    a1=node_num+2;
    a2=a1+bou_num-1;
    boundary(:,1)=n1(a1:a2);
    boundary(:,2)=n2(a1:a2);
    %%单元索引
    ele_num=n1(node_num+2+bou_num);%单元个数
    ele_index=zeros(ele_num,3);%单元索引
    a3=node_num+5+bou_num;
    [ele_index(:,1),ele_index(:,2),ele_index(:,3)]=textread(filename,'%n%n%n','headerlines',a3);
    
    %%计算单个矩阵
    K=sparse(node_num,node_num);
    F=sparse(node_num,1);
    for i=1:ele_num
        ke=caculate1(i,ele_index,n_coor);
        f=caculate2(i,ele_index,n_coor);   
        q1=ele_index(i,1)+1;
        q2=ele_index(i,2)+1;
        q3=ele_index(i,3)+1;
        q=zeros(1,3);
        q(1)=q1;
        q(2)=q2;
        q(3)=q3;
        K(q,q)=K(q,q)+ke;
        F(q,1)=F(q,1)+f;
    end
    %施加边界条件
    b=zeros(node_num,1);
    u_b=0;
    K(1,1)=1;
    for i=1:bou_num
        w1=boundary(i,1)+1;
        w2=boundary(i,2)+1;
       if(b(w1)==0)
           F(w1,1)=u_b;
           K(w1,:)=0;
           K(:,w1)=0;
           K(w1,w1)=1.0;
           b(w1)=1;
       end
       if(b(w2)==0)
           F(w2,1)=u_b;      
           K(w2,:)=0;
           K(:,w2)=0;
           K(w2,w2)=1;
           b(w2)=1;
       end   
    end
    u=K\F;
    subplot(1,2,1);
    plot3(n_coor_x,n_coor_y,u);
    %scatter3(n_coor(:,1),n_coor(:,2),u);
    L =1;
    nsamp = 1001;
    xsamp = linspace(0,L,nsamp);%100等分区间中间有100个数
    [X,Y] = meshgrid(xsamp,xsamp);
    uexact = sin(pi*X).*sin(pi*Y);
    uexact_re = reshape(uexact,nsamp,nsamp);
    subplot(1,2,2);
    mmm=mesh(xsamp,xsamp,uexact_re);%%%%%
    
    
    function [k] = caculate1(i,ele_index,n_coor)
    %UNTITLED11 此处显示有关此函数的摘要
    %   此处显示详细说明
    a1=ele_index(i,1)+1;
    a2=ele_index(i,2)+1;
    a3=ele_index(i,3)+1;
    x1=n_coor(a1,1);
    y1=n_coor(a1,2);
    x2=n_coor(a2,1);
    y2=n_coor(a2,2);
    x3=n_coor(a3,1);
    y3=n_coor(a3,2);
    A=0.5*((x2*y3-x3*y2)-(y3-y2)*x1+y1*(x3-x2));
    A=abs(A);
    A=1/(2*A);
    J=(x3-x1)*(y2-y3)-(y3-y1)*(x2-x3);
    J1=A*[(y2-y3) (x3-x2);(y3-y1) (x1-x3)];
    
    a11=J.*([1 0]*J1*(J1'*[1;0])).*0.5;
    a12=J.*([0 1]*J1*(J1'*[1;0])).*0.5;
    a13=J.*([-1 -1]*J1*(J1'*[1;0])).*(0.5);
    a22=J.*([0 1]*J1*(J1'*[0;1])).*0.5;
    a23=J.*([-1 -1]*J1*(J1'*[0;1])).*(0.5);
    a33=J.*(([-1 -1]*J1)*(J1'*[-1;-1]))*(0.5);
    k=[a11 a12 a13; a12 a22 a23;a13 a23 a33];
    end
    
    function [f] = caculate2(i,ele_index,n_coor)
    %UNTITLED12 此处显示有关此函数的摘要
    %   此处显示详细说明
    a1=ele_index(i,1)+1;
    a2=ele_index(i,2)+1;
    a3=ele_index(i,3)+1;
    x1=n_coor(a1,1);
    y1=n_coor(a1,2);
    x2=n_coor(a2,1);
    y2=n_coor(a2,2);
    x3=n_coor(a3,1);
    y3=n_coor(a3,2);
    J=(x3-x1)*(y2-y3)-(y3-y1)*(x2-x3);
    f1=@(lam1,lam2) fun(x1*lam1+x2*lam2+x3*(1-lam2-lam1),y1*lam1+y2*lam2+y3*(1-lam2-lam1)).*lam1.*J;
    f2=@(lam1,lam2) fun(x1*lam1+x2*lam2+x3*(1-lam2-lam1),y1*lam1+y2*lam2+y3*(1-lam2-lam1)).*lam2.*J;
    f3=@(lam1,lam2) fun(x1*lam1+x2*lam2+x3*(1-lam2-lam1),y1*lam1+y2*lam2+y3*(1-lam2-lam1)).*(1-lam1-lam2).*J;
    lam=@(lam1) 1-lam1;
    g1=integral2(f1,0,1,0,lam);
    g2=integral2(f2,0,1,0,lam);
    g3=integral2(f3,0,1,0,lam);
    f=zeros(3,1);
    f(1)=g1;
    f(2)=g2;
    f(3)=g3;
    end
    
    function bx = fun(x,y)
    bx = (2*pi^2)*sin(pi*x).*sin(pi*y);
    end
    

     

  8. And Comparative true solution to FIG Solving
  9. If you have any questions, feel free to contact me or write your problems in the comment region.
Published 58 original articles · won praise 20 · views 50000 +

Guess you like

Origin blog.csdn.net/GENGXINGGUANG/article/details/104684283