For For
For For
clear ;
% number of elements
nx = 5; % number of elements in x-dir (along L)
ny = 10; % number of elements in y-dir (along H)
% dimensions
ndim = 2; % 2D problem
% size of elements
elx = sz_x / nx; % element size in x-dir (L)
ely = sz_y / ny; % element size in y-dir (H)
% Loop through elements and assign global node numbers to local nodes
elem = 0;
for j = 1:ny % Loop over elements in y-direction
for i = 1:nx % Loop over elements in x-direction
elem = elem + 1;
bottom_left = (j - 1) * (nx + 1) + i;
conn(elem, :) = [bottom_left, bottom_left + 1, bottom_left + nx + 2,
bottom_left + nx + 1];
end
end
1
% Number of quadrature points
nquad = 4;
2
xi_2 = iso_coord(q, 2);
for j = 1:ndim
for l = 1:ndim
k(row, col) = k(row, col) +
3
elasticity_tensor(i,j,m,l) * shdxi(a_hat,j) * invjacob(j,l) * shdxi(b_hat,l)
* wt(q) * detjacob;
end
end
end
end
end
end
% Define time instances
time_25 = 25; % t = 25 seconds
time_50 = 50; % t = 50 seconds
% Traction at t = 25 seconds
traction_25 = [0; 1 * time_25]; % 1 MPa/s * 25s = 25 MPa in y-
direction
% Traction at t = 50 seconds
traction_50 = [0; 1 * time_50]; % 1 MPa/s * 50s = 50 MPa in y-
direction
4
% ASSEMBLY - Add local element force vector to global force vector
for a_hat = 1:nnode_el
for i = 1:ndim
a = gnodes(a_hat); % global node a
row_global = (a - 1) * ndim + i;
ff(row_global) = ff(row_global) + f((a_hat - 1) * ndim + i);
end
end
5
% Extract the displacement magnitude for each node
displacement_magnitude = sqrt(displacement(1:2:end).^2 +
displacement(2:2:end).^2);
displacement_magnitude1 = sqrt(displacement1(1:2:end).^2 +
displacement1(2:2:end).^2);
displacement_field1 = reshape(displacement_magnitude1, [nx + 1, ny + 1])'; %
Transpose it for correct orientation
6
% Shape functions and their derivatives
for node_el = 1:nnode_el
switch node_el
case 1
sh(node_el) = 0.25 * (1 - xi_1) * (1 - xi_2);
case 2
sh(node_el) = 0.25 * (1 + xi_1) * (1 - xi_2);
case 3
sh(node_el) = 0.25 * (1 + xi_1) * (1 + xi_2);
case 4
sh(node_el) = 0.25 * (1 - xi_1) * (1 + xi_2);
end
invjacob = inv(jacob);
detjacob = det(jacob);
% Calculate
the displacement gradient
for a_hat =
1:nnode_el
for i =
1:ndim
for
b_hat = 1:nnode_el
for k = 1:ndim
for p = 1:ndim
for j = 1:ndim
disp_grad(i, j) = disp_grad(i, j) +
invjacob(i, p) * shdxi(a_hat, p) * displacement1((conn(elem, a_hat) - 1) *
ndim + k);
end
end
end
end
end
end
% Strain tensor
strain = 0.5 * (disp_grad + disp_grad');
7
% Stress tensor using Hooke's law: sigma = D * epsilon
stress = zeros(ndim, ndim);
for i = 1:ndim
for j = 1:ndim
for k = 1:ndim
for l = 1:ndim
stress(i, j) = stress(i, j) +
elasticity_tensor(i,j,k,l) * strain(k, l);
end
end
end
end
gstress(elem, :, :) = stress_avg;
gstrain(elem, :, :) = strain_avg;
end
% Displacement magnitudes
displacement_magnitude_25 = sqrt(displacement(1:2:end).^2 +
displacement(2:2:end).^2);
displacement_magnitude_50 = sqrt(displacement1(1:2:end).^2 +
displacement1(2:2:end).^2);
8
% Create unique stress and strain values for plotting
unique_stress = unique(stress_xx);
unique_strain = unique(strain_xx);
% Create unique stress and strain values for Y direction for plotting
unique_stress_y = unique(stress_yy);
unique_strain_y = unique(strain_yy);
9
% Add labels and units
xlabel('Strain (_{yy}) (dimensionless)'); % or 'Strain (%)' if you are using
percentage
ylabel('Stress (_{yy}) (Pa)'); % or 'Stress (MPa)' if using
megapascals
10
size(stress_xx_field);
disp('Displacement vector:');
disp(displacement);
disp('Stress and strain matrices calculated.');
ans =
50 1
Displacement vector:
0
0
0
11
0
0
0
0
0
0
0
0
0
0
0.0080
0
0.0080
0
0.0080
0
0.0080
0
0.0080
0
0.0080
0
0.0152
0
0.0152
0
0.0152
0
0.0152
0
0.0152
0
0.0152
0
0.0215
0
0.0215
0
0.0215
0
0.0215
0
0.0215
0
0.0215
0
0.0270
0
0.0270
0
0.0270
0
0.0270
0
12
0.0270
0
0.0270
0
0.0316
0
0.0316
0
0.0316
0
0.0316
0
0.0316
0
0.0316
0
0.0354
0
0.0354
0
0.0354
0
0.0354
0
0.0354
0
0.0354
0
0.0384
0
0.0384
0
0.0384
0
0.0384
0
0.0384
0
0.0384
0
0.0405
0
0.0405
0
0.0405
0
0.0405
0
0.0405
0
0.0405
0
0.0418
0
13
0.0418
0
0.0418
0
0.0418
0
0.0418
0
0.0418
0
0.0422
0
0.0422
0
0.0422
0
0.0422
0
0.0422
0
0.0422
14
15
16
Published with MATLAB® R2024b
17