finite element to calculate deflection of fixed beam and distributed load in matlab
main.m
% Define propertiesL = 2; % Length of the beam in metersE = 200e9; % Young's Modulus in PaI = 4.1667e-6; % Moment of Inertia in m^4q = 10000; % Distributed load in N/m% Create the finite element modelnumElements = 5; % Number of elementsnumNodes = numElements + 1; % Number of nodesnodeCoordinates = linspace(0, L, numNodes); % Coordinates of nodes% Preallocate global stiffness matrix and force vectorK = zeros(numNodes, numNodes);
F = zeros(numNodes, 1);
% Assemble global stiffness matrix and force vectorfor 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 displacementsdisplacements = K\F;
% Calculate the deflectiondeflection = displacements(end);
disp(deflection);