Matlab Code 1D FEM
Matlab Code 1D FEM
Matlab Code 1D FEM
clc
clear all;
Total_length_of_system = 5;% Change % Total Length of system = 5 units
Area_of_element = 1; % Change % Area of cross-section for each Spring
Es = 100; % Change % Element Stiffness for each Spring = 100 units
E_of_element = 1; % Change % Young's Modulus of Elasticity for each Spring = 1 units
No_of_elements = 5; % Change % Discretization Parameters
No_of_global_nodes = No_of_elements + 1;
No_of_nodes_per_element = 2; % Number of nodes per element = 2
No_DOF_per_node = 1; % Degree of Freedom at each node = 1
Length_of_element = (Total_length_of_system / No_of_elements);
IEN = [1:No_of_elements;2:No_of_elements+1] % IEN Array
No_of_constraint_nodes = 1; % Change % Essential Boundary Conditions
Constraint_nodes = [1]; % Change % Displacement at Node No 1 = 0 units
ID = ones(1,No_of_elements + 1);
ID (1,Constraint_nodes) = 0;
ElementValue = 0;
for i=1:No_of_elements + 1
if ID (1,i) == 0
ID (1,i) = 0;
else
ID (1,i) = ElementValue+1;
ElementValue = ElementValue+1;
end
end
ID % ID Array
No_of_loding_nodes = 2; % Change % Essential Boundary Conditions
Loading_nodes = [3 6]; % Change % Also change in Construction of Displacement Vector
LM = zeros(2,No_of_elements);
for i=1:2
for j=1:No_of_elements
x = IEN(i,j);
LM(i,j) = ID(1,x);
end
end
LM % LM Array
Order_of_K = max(ID); % Element Stiffness Matrix
K = zeros(Order_of_K,Order_of_K);
for Go_element_wise = 1:No_of_elements
ElementNo = Go_element_wise;
Ke = ((Area_of_element*E_of_element)/Length_of_element)*[(Es) (-1*Es);(-1*Es) (Es)];
formatSpec = "K%d%d";
Element_Stiffness_Matrix = sprintf(formatSpec,ElementNo,ElementNo)
Ke % Formulation of Element Stiffness Matrix
for aa = 1:No_of_nodes_per_element*No_DOF_per_node % Assembly of K
i = LM(aa,Go_element_wise);
for ee = 1:No_of_nodes_per_element*No_DOF_per_node
j = LM(ee,Go_element_wise);
if LM(aa,Go_element_wise) == 0
elseif LM(ee,Go_element_wise) == 0
else
K(i,j) = K(i,j) + Ke(aa,ee);
end
end
end
end
K % Global Stiffness Matrix
D = ones(No_of_global_nodes,No_DOF_per_node); % Construction of Displacement Vector
D(Constraint_nodes,1) = 0;
Overall_displacement_vector = D % Overall Displacement Vector
Displacement_vector = ones(No_of_global_nodes - No_of_constraint_nodes,1);
for i = No_DOF_per_node
for j = 1:No_of_global_nodes
if Overall_displacement_vector(j,i) == 0
else
for ii = No_DOF_per_node
for jj = 1:No_of_global_nodes - No_of_constraint_nodes
Displacement_vector(jj,ii) = 0;
end
end
end
end
end
Displacement_vector % Displacement Vector
F = Overall_displacement_vector;
F(3,1) = 3; % Change % Also change in Natural Boundary Conditions Column
F(6,1) = 6;% Change % Also change in Natural Boundary Conditions Column
Overall_force_vector = F % Overall Force Vector
Force_vector = ones(No_of_global_nodes - No_of_constraint_nodes,1);
ii = 1;
jj = 0;
for i = No_DOF_per_node
for j = 1:No_of_global_nodes
if Overall_force_vector(j,i) == 0
else
jj = jj + 1;
if Overall_force_vector(j,i) == 1
Force_vector(jj,ii) = 0;
else
Force_vector(jj,ii) = Overall_force_vector(j,i);
end
end
end
end
Force_vector % Force Vector
%%
D_Matrix_Solved = K^-1*Force_vector;
Displacement_vector_solved = ones(No_of_global_nodes,1);
de = 1;
ed = 1;
for i=1:No_of_global_nodes
for j = No_DOF_per_node
if Overall_displacement_vector(i,j) == 0
Displacement_vector_solved(i,j) = 0;
else
Displacement_vector_solved(i,j) = D_Matrix_Solved (de,ed);
de = de+1;
end
end
end
Displacement_vector_solved % Solution of Primary unknowns