function plotInternalForces2dBeam(AllElement_data,QLocal_elem_list,theTitle)
%% Bending moment diagram
% Initialize the figure
figure('Color', 'white');
hold on;

% Initialize bending moment array
numElems = numel(AllElement_data);
M = zeros(numElems, 2);

% Plot undeformed structure and calculate bending moments
for elemIdx = 1:numElems
    elem = AllElement_data{elemIdx};
    coordinatesI = elem{3};
    coordinatesJ = elem{4};
    
    % Plot undeformed structure
    x = [coordinatesI(1), coordinatesJ(1)];
    y = [coordinatesI(2), coordinatesJ(2)];
    plot(x, y, 'k.-', 'LineWidth', 2.,'MarkerSize',20);
    
    % Fill the arrays with the bending moment values
    M(elemIdx, 1) = QLocal_elem_list(3,elemIdx) / 1e6; % Moment at node I
    M(elemIdx, 2) = QLocal_elem_list(6,elemIdx) / 1e6; % Moment at node J
end

% Determine the scale factor
max_abs_M = max(abs(M(:)));
scaleFactor = 2000 / max_abs_M;

% Plot moments with scale
for elemIdx = 1:numElems
    elem = AllElement_data{elemIdx};
    coordinatesI = elem{3};
    coordinatesJ = elem{4};
    x = [coordinatesI(1), coordinatesJ(1)];
    y = [coordinatesI(2), coordinatesJ(2)];
    dx = x(2) - x(1);
    dy = y(2) - y(1);
    
    % Retrieve moments
    moments = M(elemIdx, :);
    
    % Compute scaled positions for moment vectors
    x4M_I_1 = x(1);
    y4M_I_1 = y(1);
    x4M_I_2 = x4M_I_1 + scaleFactor * (dy / (abs(dy) + 1e-6)) * moments(1);
    y4M_I_2 = y4M_I_1 + scaleFactor * (dx / (abs(dx) + 1e-6)) * moments(1);
    
    x4M_J_1 = x(2);
    y4M_J_1 = y(2);
    x4M_J_2 = x4M_J_1 + scaleFactor * (dy / (abs(dy) + 1e-6)) * -1 * moments(2);
    y4M_J_2 = y4M_J_1 + scaleFactor * (dx / (abs(dx) + 1e-6)) * -1 * moments(2);
    
    % Plot moment vectors
    plot([x4M_I_1, x4M_I_2], [y4M_I_1, y4M_I_2], 'k-');
    plot([x4M_I_2, x4M_J_2], [y4M_I_2, y4M_J_2], 'k-');
    plot([x4M_J_1, x4M_J_2], [y4M_J_1, y4M_J_2], 'k-');
    
    % Add moment values near the points
    text(x4M_I_2, y4M_I_2, sprintf('%.2f', moments(1)), 'FontSize', 8, 'HorizontalAlignment', 'right', 'Color', 'black');
    text(x4M_J_2, y4M_J_2, sprintf('%.2f', moments(2)), 'FontSize', 8, 'HorizontalAlignment', 'left', 'Color', 'black');
end

% Adjust plot settings
axis off;
axis equal;
hold off;
end