finite element to calculate deflection of fixed beam and distributed load in matlab

main.m
% Define properties
L = 2; % Length of the beam in meters
E = 200e9; % Young's Modulus in Pa
I = 4.1667e-6; % Moment of Inertia in m^4
q = 10000; % Distributed load in N/m

% Create the finite element model
numElements = 5; % Number of elements
numNodes = numElements + 1; % Number of nodes
nodeCoordinates = linspace(0, L, numNodes); % Coordinates of nodes

% Preallocate global stiffness matrix and force vector
K = zeros(numNodes, numNodes);
F = zeros(numNodes, 1);

% Assemble global stiffness matrix and force vector
for e = 1:numElements
    % Local element stiffness matrix
    Le = L/numElements;
    k = (E*I/Le^3) * [12, 6*Le, -12, 6*Le; 6*Le, 4*Le^2, -6*Le, 2*Le^2; -12, -6*Le, 12, -6*Le; 6*Le, 2*Le^2, -6*Le, 4*Le^2];
    
    % Assemble into global stiffness matrix
    nodes = [e, e+1];
    K(nodes, nodes) = K(nodes, nodes) + k;
    
    % Local element load vector
    fe = [q*Le/2; q*Le^2/12; q*Le/2; -q*Le^2/12];
    
    % Assemble into global force vector
    F(nodes) = F(nodes) + fe;
end

% Apply boundary conditions (Fixed beam)
K(1, :) = 0;
K(1, 1) = 1;
F(1) = 0;

% Solve for displacements
displacements = K\F;

% Calculate the deflection
deflection = displacements(end);
disp(deflection);
1215 chars
44 lines

gistlibby LogSnag