Computing Library › Worked Examples
Worked Examples

Finite Element Solution of a 1D Bar

Solve axial displacement of an elastic bar under load using two linear elements and assemble the global stiffness system by hand.

Problem

A bar of length L=2, cross-section A=1, Young's modulus E=1, is fixed at x=0 and pulled with force F=1 at x=2. The governing equation is EA u''(x) = 0 with u(0)=0 and EA u'(2)=F. The exact answer is a linear displacement u(x)=x, but we recover it through the finite element machinery so the same steps generalize to hard problems.

Element stiffness

Split the bar into two equal linear elements of length h=1. Each two-node element has local stiffness matrix k = (EA/h)[[1,-1],[-1,1]]. With EA/h = 1 both element matrices are identical.

Local element stiffness k
1-1-11

Assemble and solve

python
import numpy as np
h=1.0; EA=1.0; k=(EA/h)*np.array([[1,-1],[-1,1]])
K=np.zeros((3,3))
for e,(a,b) in enumerate([(0,1),(1,2)]):
    for i,I in enumerate((a,b)):
        for j,J in enumerate((a,b)):
            K[I,J]+=k[i,j]
F=np.array([0.0,0.0,1.0])   # load at node 3
# apply u0=0: strike row/col 0
Kr=K[1:,1:]; Fr=F[1:]
u=np.linalg.solve(Kr,Fr)
print(np.r_[0.0,u])  # [0. 1. 2.]

The assembled 3x3 stiffness is [[1,-1,0],[-1,2,-1],[0,-1,1]]. After striking the fixed degree of freedom the reduced system gives nodal displacements 0, 1, 2 exactly matching u(x)=x. The reaction at the wall recovers -F, confirming equilibrium.