fem1d-ode

4
“Nonlinear Finite Element Analysis” 1D FEM using MATLAB ([email protected]) Given the 1D differential problem: -u ′′ (x)= p x [a, b] u(a)= c u(b)= d in the following it is reported a simple MATLAB code which solves it by means of the finite element method. function u = fem1d(nel,a,b,c,d,p) %----------------------------------------------------------------- % MATLAB program % to solve with linear finite elements the ODE: % -u’’ = p = const, x in [a,b] % with boundary conditions: % u(a) = c and u(b) = d % % syntax: u = fem1d(nel,a,b,c,d,p) % input: % nel = number of elements to be used % a,b = domain limits % c,d = Dirichlet b.c. values % p = uniform body load % output: % u = ODE solution % % (by Alessandro Reali) %----------------------------------------------------------------- nnode = nel + 1; % total number of nodes %----------------------------------------- % input data for nodal coordinate values %----------------------------------------- 1

Transcript of fem1d-ode

Page 1: fem1d-ode

“Nonlinear Finite Element Analysis”

1D FEM using MATLAB

([email protected])

Given the 1D differential problem:

−u′′(x) = p x ∈ [a, b]

u(a) = c

u(b) = d

in the following it is reported a simple MATLAB code which solves it bymeans of the finite element method.

function u = fem1d(nel,a,b,c,d,p)

%-----------------------------------------------------------------

% MATLAB program

% to solve with linear finite elements the ODE:

% -u’’ = p = const, x in [a,b]

% with boundary conditions:

% u(a) = c and u(b) = d

%

% syntax: u = fem1d(nel,a,b,c,d,p)

% input:

% nel = number of elements to be used

% a,b = domain limits

% c,d = Dirichlet b.c. values

% p = uniform body load

% output:

% u = ODE solution

%

% (by Alessandro Reali)

%-----------------------------------------------------------------

nnode = nel + 1; % total number of nodes

%-----------------------------------------

% input data for nodal coordinate values

%-----------------------------------------

1

Page 2: fem1d-ode

h = (b - a)/nel;

coord = a:h:b;

%-----------------------------------------

% initialization of matrices and vectors

%-----------------------------------------

f_gl = zeros(nnode,1); % initialization of global sr-h-s

k_gl = zeros(nnode,nnode); % initialization of global stiffnes

%-----------------------------------------------------------------

% computation of element matrices and vectors and their assembly

%-----------------------------------------------------------------

for iel = 1:nel % loop over the total number of elements

k_el = el_stiff(h); % compute element stiffness

f_el = el_rhs(h,p); % compute element r-h-s

for i = 1:2 % assemble stiffness and r-h-s

f_gl(iel-1+i) = f_gl(iel-1+i) + f_el(i);

for j = 1:2

k_gl(iel-1+i,iel-1+j) = k_gl(iel-1+i,iel-1+j) + k_el(i,j);

end

end

end

%----------------------------

% apply boundary conditions

%----------------------------

k_gl(1,:) = 0;

k_gl(1,1) = 1;

f_gl(1) = c;

k_gl(nnode,:) = 0;

k_gl(nnode,nnode) = 1;

f_gl(nnode) = d;

%--------------------------

% solve the linear system

%--------------------------

u = k_gl\f_gl;

2

Page 3: fem1d-ode

%-----------------

% exact solution

%------------------

x = a:h/100:b;

c1 = (d + p*b^2/2 - c - p*a^2/2)/(b - a);

c2 = c + p*a^2/2 - a*c1;

exact = -p/2*x.^2 + c1*x + c2;

%------------------------------------

% plot both exact and FEM solutions

%------------------------------------

figure(1)

plot(coord,u,x,exact)

title(’-d^2u/dx^2 = f’)

xlabel(’x’)

ylabel(’u’)

legend(’f.e. solution’,’exact solution’)

%-----------------------------------------------------------------

function [k_el] = el_stiff(e_length)

%-----------------------------------------------------------------

% element stiffness matrix for -u’’ = f using linear elements

% e_length = element length

%-----------------------------------------------------------------

k_el=[1 -1; -1 1]/e_length;

return

%----------------------------------------------------------------

function [f_el]=el_rhs(e_length,p)

%-----------------------------------------------------------------

% element right-hand-side for f(x)=p using linear elements

% e_length = element length

% p = uniform body load

%-----------------------------------------------------------------

f_el=[1; 1]*p*e_length/2;

return

%-----------------------------------------------------------------

3

Page 4: fem1d-ode

Homework

Extend the code fem1d including:

• the possibility of imposing Neumann boundary conditions;

• the possibility of applying a non-uniform body load p = p(x);

• the possibility of employing a non-uniform mesh.

With your modified code, solve the following problem using 3, 4 and 5elements and discussing your mesh choices (compare your results with thoseobtained from a uniform mesh choice).

−u′′(x) = x8 x ∈ [0, 1]

u(0) = 0

u′(1) = 1/20

4