Assignment # 2:-Create A MATLAB Program To Make A Mesh With Right-Angled Triangular Elements
Assignment # 2:-Create A MATLAB Program To Make A Mesh With Right-Angled Triangular Elements
triangular elements
nx = 6 ;
ny = 6 ;
% nested for loop for allotting numbers to nodes and calculating the
% coordinates
for ix = 1:nx
for iy = 1:ny
i(ix,iy) = (iy - 1) * nx + ix ;
c = i(ix,iy);
% c or i is node number
X(c) = (ix-1)/(nx-1);
Y(c)= (iy-1)/(ny-1);
end
end
% nested for loop for analyzing square and triangular elements
% and plotting them
for ix = 1:nx
for iy = 1:ny
c = i(ix,iy);
% taking care of elements range
if (ix<nx & iy<ny)
% formula for square elements
nq(ix,iy) = (iy-1)*(nx-1)+ix;
for t=1:2
% formula for triangular elements
m(ix,iy,t) = 2*nq(ix,iy)-2+t;
d=m(ix,iy,t);
M(d)=d;
% calculating nodes of each element and
% coordinate of each node
I(d) = c; e = I(d);
Ic(1:2,d) = [X(e) ; Y(e)];
J(d) = c+1-1*(t-2)*nx; f = J(d);
Jc(1:2,d) = [X(f) ; Y(f)];
K(d) = c+nx+1*(t-1); g = K(d);
Kc(1:2,d) = [X(g) ; Y(g)];
% plotting the lines making triangles
plot ( [X(e) X(f)],[Y(e) Y(f)] , 'g' )
hold on
plot ( [X(f) X(g)],[Y(f) Y(g)] , 'g' )
plot ( [X(g) X(e)],[Y(g) Y(e)] , 'g' )
% inserting element number at centre
Cx = (X(e)+X(f)+X(g))/3; Cy = (Y(e)+Y(f)+Y(g))/3;
text (Cx,Cy,['(',num2str(d)] )
text (Cx,Cy,['
)'] )
end
end
% inserting node number with coordinates at each node
text
text
text
text
(X(c),Y(c),[' ',num2str(c)] )
(X(c),Y(c),['
(',num2str(X(c))] )
(X(c),Y(c),['
,',num2str(Y(c))] )
(X(c),Y(c),['
)'] )
end
end
% displaying in table form desired items i.e element no., nodes, coordinates
O = [ M' I' J' K' Ic' Jc' Kc']
O=
1.0000
2.0000
3.0000
4.0000
5.0000
6.0000
7.0000
8.0000
9.0000
10.0000
11.0000
12.0000
13.0000
14.0000
15.0000
16.0000
17.0000
18.0000
19.0000
20.0000
21.0000
22.0000
23.0000
24.0000
25.0000
26.0000
27.0000
28.0000
29.0000
30.0000
31.0000
32.0000
33.0000
34.0000
35.0000
36.0000
37.0000
1.0000
1.0000
2.0000
2.0000
3.0000
3.0000
4.0000
4.0000
5.0000
5.0000
7.0000
7.0000
8.0000
8.0000
9.0000
9.0000
10.0000
10.0000
11.0000
11.0000
13.0000
13.0000
14.0000
14.0000
15.0000
15.0000
16.0000
16.0000
17.0000
17.0000
19.0000
19.0000
20.0000
20.0000
21.0000
21.0000
22.0000
8.0000
2.0000
9.0000
3.0000
10.0000
4.0000
11.0000
5.0000
12.0000
6.0000
14.0000
8.0000
15.0000
9.0000
16.0000
10.0000
17.0000
11.0000
18.0000
12.0000
20.0000
14.0000
21.0000
15.0000
22.0000
16.0000
23.0000
17.0000
24.0000
18.0000
26.0000
20.0000
27.0000
21.0000
28.0000
22.0000
29.0000
7.0000
8.0000
8.0000
9.0000
9.0000
10.0000
10.0000
11.0000
11.0000
12.0000
13.0000
14.0000
14.0000
15.0000
15.0000
16.0000
16.0000
17.0000
17.0000
18.0000
19.0000
20.0000
20.0000
21.0000
21.0000
22.0000
22.0000
23.0000
23.0000
24.0000
25.0000
26.0000
26.0000
27.0000
27.0000
28.0000
28.0000
0
0
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
0
0
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
0
0
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
0
0
0.2000
0.2000
0.4000
0.4000
0.6000
0
0
0
0
0
0
0
0
0
0
0.2000
0.2000
0.2000
0.2000
0.2000
0.2000
0.2000
0.2000
0.2000
0.2000
0.4000
0.4000
0.4000
0.4000
0.4000
0.4000
0.4000
0.4000
0.4000
0.4000
0.6000
0.6000
0.6000
0.6000
0.6000
0.6000
0.6000
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
1.0000
1.0000
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
1.0000
1.0000
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
1.0000
1.0000
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.2000
0
0.2000
0
0.2000
0
0.2000
0
0.2000
0
0.4000
0.2000
0.4000
0.2000
0.4000
0.2000
0.4000
0.2000
0.4000
0.2000
0.6000
0.4000
0.6000
0.4000
0.6000
0.4000
0.6000
0.4000
0.6000
0.4000
0.8000
0.6000
0.8000
0.6000
0.8000
0.6000
0.8000
0
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
1.0000
0
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
1.0000
0
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
1.0000
0
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.2000
0.2000
0.2000
0.2000
0.2000
0.2000
0.2000
0.2000
0.2000
0.2000
0.4000
0.4000
0.4000
0.4000
0.4000
0.4000
0.4000
0.4000
0.4000
0.4000
0.6000
0.6000
0.6000
0.6000
0.6000
0.6000
0.6000
0.6000
0.6000
0.6000
0.8000
0.8000
0.8000
0.8000
0.8000
0.8000
0.8000
38.0000
39.0000
40.0000
41.0000
42.0000
43.0000
44.0000
45.0000
46.0000
47.0000
48.0000
49.0000
50.0000
22.0000
23.0000
23.0000
25.0000
25.0000
26.0000
26.0000
27.0000
27.0000
28.0000
28.0000
29.0000
29.0000
23.0000
30.0000
24.0000
32.0000
26.0000
33.0000
27.0000
34.0000
28.0000
35.0000
29.0000
36.0000
30.0000
29.0000
29.0000
30.0000
31.0000
32.0000
32.0000
33.0000
33.0000
34.0000
34.0000
35.0000
35.0000
36.0000
0.6000
0.8000
0.8000
0
0
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
0.6000
0.6000
0.6000
0.8000
0.8000
0.8000
0.8000
0.8000
0.8000
0.8000
0.8000
0.8000
0.8000
0.8000
1.0000
1.0000
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
1.0000
1.0000
0.6000
0.8000
0.6000
1.0000
0.8000
1.0000
0.8000
1.0000
0.8000
1.0000
0.8000
1.0000
0.8000
0.8000
0.8000
1.0000
0
0.2000
0.2000
0.4000
0.4000
0.6000
0.6000
0.8000
0.8000
1.0000
0.8000
0.8000
0.8000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
to
make
mesh
with
isosceles
% nested for loop for allotting numbers to nodes and calculating their
% coordinates
for iy = 1:ny
% condition of o=1 for odd row and o=2 for even row
if (o==1)
o=2;
else
o=1;
end
for ix = 1:nx(o)
i(ix,iy) = (iy - 1)*(nx(1) + 0.5) - (o-1)*0.5 + ix ;
c = i(ix,iy);
% c or i is node number
X(c) = (ix-1)/(nx(1)-1);
% X and Y are coordinates for c
Y(c)= (iy-1)/(ny-1);
% condition for dealing with anomalous even row nodes
if (o==2)
X(c) = X(c)- 0.5*w ;
if (ix==1)
X(c) = 0;
elseif (ix==nx(2))
X(c) = 1;
end
end
end
end
o = 2 ;
% even/odd check is reset
% nested for loop for analyzing isosceles triangular elements
% and plotting them
for iy = 1:ny
% condition of o=1 for odd row and o=2 for even row
if (o==1)
o=2;
else
o=1;
end
for ix = 1:nx(o)
c = i(ix,iy);
% rewinding c and i into memory which is node number
for t=1:2
if (ix<nx(o))
% condition for elements range
% add. condition for elements range in odd rows (t=1)
if (t==1 & iy<ny)
m(ix,iy,t) = (2*nx(1)-1)/2 * (iy-1) - (o-1)*0.5 + ix;
d=m(ix,iy,t);
% d or m is triangular element
M(d)=d;
I(d) = c; e = I(d);
Ic(1:2,d) = [X(e) ; Y(e)];
J(d) = c+1; f = J(d);
Jc(1:2,d) = [X(f) ; Y(f)];
K(d) = c+nx(2); g = K(d);
Kc(1:2,d) = [X(g) ; Y(g)];
End
% add. condition for elements range in even rows (t=2)
if (t==2 & iy>1)
m(ix,iy,t) = m1 + (2*nx(1)-1)/2 * (iy-2) + (2-o)*0.5+ix;
d=m(ix,iy,t);
% d or m is triangular element
M(d)=d;
% calculating nodes of each element and
% coordinate of each node
K(d) = c; g = K(d);
Kc(1:2,d) = [X(g) ; Y(g)];
I(d) = c-nx(1); e = I(d);
Ic(1:2,d) = [X(e) ; Y(e)];
J(d) = c+1; f = J(d);
Jc(1:2,d) = [X(f) ; Y(f)];
end
% plotting each element using nodes coordinates
plot ( [X(e) X(f)],[Y(e) Y(f)] , 'm' )
hold on
plot ( [X(f) X(g)],[Y(f) Y(g)] , 'm' )
plot ( [X(g) X(e)],[Y(g) Y(e)] , 'm' )
% inserting element number at centre
Cx = (X(e)+X(f)+X(g))/3; Cy = (Y(e)+Y(f)+Y(g))/3;
text (Cx,Cy,['(',num2str(d)] )
text (Cx,Cy,['
)'] )
end
end
% inserting node number with coordinates at each node
text (X(c),Y(c),[' ',num2str(c)] )
text (X(c),Y(c),['
(',num2str(X(c))] )
text (X(c),Y(c),['
, ',num2str(Y(c))] )
text (X(c),Y(c),['
)'] )
end
end
% displaying in table form desired items i.e element no., nodes, coordinates
O = [ M' I' J' K' Ic' Jc' Kc']
O=
1.0000
2.0000
3.0000
4.0000
5.0000
6.0000
1.0000
2.0000
3.0000
4.0000
6.0000
7.0000
2.0000
3.0000
4.0000
5.0000
7.0000
8.0000
7.0000
8.0000
9.0000
10.0000
12.0000
13.0000
0
0.2500
0.5000
0.7500
0
0.1250
0
0
0
0
0.2500
0.2500
0.2500
0.5000
0.7500
1.0000
0.1250
0.3750
0
0
0
0
0.2500
0.2500
0.1250
0.3750
0.6250
0.8750
0
0.2500
0.2500
0.2500
0.2500
0.2500
0.5000
0.5000
7.0000
8.0000
9.0000
10.0000
11.0000
12.0000
13.0000
14.0000
15.0000
16.0000
17.0000
18.0000
19.0000
20.0000
21.0000
22.0000
23.0000
24.0000
25.0000
26.0000
27.0000
28.0000
29.0000
30.0000
31.0000
32.0000
33.0000
34.0000
35.0000
36.0000
8.0000
9.0000
10.0000
12.0000
13.0000
14.0000
15.0000
17.0000
18.0000
19.0000
20.0000
21.0000
1.0000
2.0000
3.0000
4.0000
5.0000
7.0000
8.0000
9.0000
10.0000
12.0000
13.0000
14.0000
15.0000
16.0000
18.0000
19.0000
20.0000
21.0000
9.0000
10.0000
11.0000
13.0000
14.0000
15.0000
16.0000
18.0000
19.0000
20.0000
21.0000
22.0000
7.0000
8.0000
9.0000
10.0000
11.0000
13.0000
14.0000
15.0000
16.0000
18.0000
19.0000
20.0000
21.0000
22.0000
24.0000
25.0000
26.0000
27.0000
14.0000
15.0000
16.0000
18.0000
19.0000
20.0000
21.0000
23.0000
24.0000
25.0000
26.0000
27.0000
6.0000
7.0000
8.0000
9.0000
10.0000
12.0000
13.0000
14.0000
15.0000
17.0000
18.0000
19.0000
20.0000
21.0000
23.0000
24.0000
25.0000
26.0000
0.3750
0.6250
0.8750
0
0.2500
0.5000
0.7500
0
0.1250
0.3750
0.6250
0.8750
0
0.2500
0.5000
0.7500
1.0000
0.1250
0.3750
0.6250
0.8750
0
0.2500
0.5000
0.7500
1.0000
0.1250
0.3750
0.6250
0.8750
0.2500
0.2500
0.2500
0.5000
0.5000
0.5000
0.5000
0.7500
0.7500
0.7500
0.7500
0.7500
0
0
0
0
0
0.2500
0.2500
0.2500
0.2500
0.5000
0.5000
0.5000
0.5000
0.5000
0.7500
0.7500
0.7500
0.7500
0.6250
0.8750
1.0000
0.2500
0.5000
0.7500
1.0000
0.1250
0.3750
0.6250
0.8750
1.0000
0.1250
0.3750
0.6250
0.8750
1.0000
0.2500
0.5000
0.7500
1.0000
0.1250
0.3750
0.6250
0.8750
1.0000
0.2500
0.5000
0.7500
1.0000
0.2500
0.2500
0.2500
0.5000
0.5000
0.5000
0.5000
0.7500
0.7500
0.7500
0.7500
0.7500
0.2500
0.2500
0.2500
0.2500
0.2500
0.5000
0.5000
0.5000
0.5000
0.7500
0.7500
0.7500
0.7500
0.7500
1.0000
1.0000
1.0000
1.0000
0.5000
0.7500
1.0000
0.1250
0.3750
0.6250
0.8750
0
0.2500
0.5000
0.7500
1.0000
0
0.1250
0.3750
0.6250
0.8750
0
0.2500
0.5000
0.7500
0
0.1250
0.3750
0.6250
0.8750
0
0.2500
0.5000
0.7500
0.5000
0.5000
0.5000
0.7500
0.7500
0.7500
0.7500
1.0000
1.0000
1.0000
1.0000
1.0000
0.2500
0.2500
0.2500
0.2500
0.2500
0.5000
0.5000
0.5000
0.5000
0.7500
0.7500
0.7500
0.7500
0.7500
1.0000
1.0000
1.0000
1.0000