0% found this document useful (0 votes)
42 views

Assignment # 2:-Create A MATLAB Program To Make A Mesh With Right-Angled Triangular Elements

The document describes a MATLAB program that creates a mesh of right-angled triangular elements. It defines the number of nodes in rows and columns, assigns node numbers and calculates coordinates using nested for loops. It then uses additional nested for loops to analyze each square and triangular element, calculate the nodes and coordinates, and plot the elements to display the mesh. Finally, it outputs a table showing the element numbers, node numbers, and node coordinates.

Uploaded by

Hassan Uetmeche
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Assignment # 2:-Create A MATLAB Program To Make A Mesh With Right-Angled Triangular Elements

The document describes a MATLAB program that creates a mesh of right-angled triangular elements. It defines the number of nodes in rows and columns, assigns node numbers and calculates coordinates using nested for loops. It then uses additional nested for loops to analyze each square and triangular element, calculate the nodes and coordinates, and plot the elements to display the mesh. Finally, it outputs a table showing the element numbers, node numbers, and node coordinates.

Uploaded by

Hassan Uetmeche
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment # 2:Create a MATLAB program to make a mesh with right-angled

triangular elements
nx = 6 ;
ny = 6 ;

% no. of nodes in row


% no. of nodes in column

% 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

Assignment # 3:Create a MATLAB program


triangular elements
nx = [5 6];
% no.
ny = 5 ;
% no.
o = 2 ;
% o
w = 1/(nx(1)-1);
mn = (nx(1)-1 + nx(2)-1) *(ny-1);
m1= mn/2;

to

make

mesh

with

isosceles

of nodes in odd and even rows resp.


of nodes in columns
introduced to decide b/w odd or even
% width of each element
% total no. of elements in the mesh
% half of number of elements

% 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

You might also like