Friday, November 11, 2011

3 node triangular element

Below you may find a Matlab function to generate 3 node triangular elements. The output will not be symmetric, so I will try to write one for symmetric triangular elements soon.
Note that nx and ny are the number of rectangular elements in each direction.

function [Edof,Ex,Ey]=trimesh(xcorner,ycorner,nx,ny)

% function [Edof,Ex,Ey]=trimesh(xcorner,ycorner,nx,ny)
%----------- Description ---------
% Generates triangular 3 node elements
% ------------ INPUT -------------
% xcorner=[lower left, lower right, upper right, upper left] 
% ycorner=[lower left, lower right, upper right, upper left]
% nx= No. elements in x direction
% ny= No. elements in y direction
%    3         4
%    o---------o
%    | +       |
%    |   +     |
%    |     +   | 
%    |       + |
%    o---------o
%    1         2
% ----------- OUTPUT -------------
% Ex= element x coordinates matrix [nrelem X 3]
% Ey= element y coordinates matrix [nrelem X 3]
% Edof= element DOF [nrelem X 7]
% Written by 
% Shah, 11 November 2011

l=xcorner(2)-xcorner(1);
h=ycorner(3)-ycorner(2);
for i=1:ny
        for j=1:nx
            dl=(i-1)*(nx+1)+j;
            ul=dl+(nx+1); 
            dr=dl+1;
            ur=ul+1;
            xy_dl=[(j-1)*(l/nx)+xcorner(1) (i-1)*(h/ny)+ycorner(1)];
            xy_dr=[j*(l/nx)+xcorner(1) (i-1)*(h/ny)+ycorner(1)];
            xy_ul=[(j-1)*(l/nx)+xcorner(1) i*(h/ny)+ycorner(1)];
            xy_ur=[j*(l/nx)+xcorner(1) i*(h/ny)+ycorner(1)];
            el=2*(i-1)*nx+2*j-1;
            Edof(el,:)=[el,2*dl-1,2*dl,2*dr-1,2*dr,2*ul-1,2*ul];
            Edof(el+1,:)=[el+1,2*dr-1,2*dr,2*ur-1,2*ur,2*ul-1,2*ul];
            Ex(el,:)=[xy_dl(1) xy_dr(1) xy_ul(1)];
            Ex(el+1,:)=[xy_dr(1) xy_ur(1) xy_ul(1)];
            Ey(el,:)=[xy_dl(2) xy_dr(2) xy_ul(2)];
            Ey(el+1,:)=[xy_dr(2) xy_ur(2) xy_ul(2)];
        end
end

figure(1);
hold on;
for i=1:length(Edof(:,1));
    plot(Ex(i,[1:end 1]),Ey(i,[1:end 1]));
end
axis equal

No comments:

Post a Comment