Lecture26 May 05, 2008 Lecture 26: Putting it all together #5... Galerkin Finite Element Methods for ODE BVPs Outline 1) Galerkin Finite Elements: Review 2) A specific Example: Piecewise linear elements 3) Computational Issues and Algorithms A) Calculation of integrals: Basis functions vs Element View B) Element Matrices C) Global Assembly 4) Matlab Demo 5) The End (of the beginning) Galerkin Finite Elements Example problem: Find best fit piecewise linear function ũ(x), that satisfies -u''+u = f(x) (u(0)=α, u(L)=β) in a least-squares sense ui-1 u xi-1 i xi x u i+1 xi+1 Lecture26 May 05, 2008 Galerkin Finite Elements Basis Function view: linear combination of "hat functions" ui-1 ui ui+1 xi xi+1 1 0 xi-1 x Galerkin Finite Elements Element view: union of piecewise linear elements (with local element basis functions) ui-1 ui ui+1 xi xi+1 1 0 x i-1 Element i hi Lecture26 May 05, 2008 Galerkin Finite Elements Galerkin FEM as a least squares problem: The Continuous problem: find u(x) that satisfies The Discrete problem: find ũ(x) such that the residual r(x) is orthogonal to the basis functions Galerkin Finite Elements Evaluating the integrals: 3 critical integrals to evaluate 1) Components of the stiffness matrix Kij 2) Components of the Mass matrix Mij 3) Components of the force vector f i Lecture26 May 05, 2008 Galerkin Finite Elements Putting it together, basis function view vs "element view": the Mass matrix 1 0 xi-1 xi xi+1 for basis function ϕi want: Galerkin Finite Elements Putting it together, basis function view vs "element view": the local element Mass matrix Me: consider the contributions from one element i N1(t) Element basis functions: N1(t), N2(t) N2(t) xi -1 t xi+1 1 Need _____ integrals for element mass matrix Lecture26 May 05, 2008 Galerkin Finite Elements Putting it together, basis function view vs "element view": the local element Mass matrix Me: consider the contributions from one element i N1(t) Element Mass Matrix: N2(t) xi -1 t xi+1 1 Galerkin Finite Elements Putting it together, basis function view vs "element view": Element Stiffness Matrix: N (t) 1 N2(t) N'2(t) N'1(t) Lecture26 May 05, 2008 Galerkin Finite Elements Putting it together, basis function view vs "element view": Element Force vector: N1(t) N2(t) -1 t 1 Lecture26 May 05, 2008 Galerkin Finite Elements: Matlab function [Ke,Me] = getElementMatrix(xCoords) % GETELEMENTMATRIX - returns element matrices for an individual element % % [Ke,Me] = getElementMatrix(xCoords) % % xCoords:- coordinates of 1D element [ xMin xMax ] % Ke: - element stiffness matrix (assuming linear elements) % Me: - element mass matrix (assuming linear hat elements) % N1 = @(t) (1-t)/2; % linear hat function N2 = @(t) (1+t)/2; N1p = @(t) -.5*ones(size(t)); % derivative dN1/dt (not really a function) N2p = @(t) .5*ones(size(t)); % quadrature points and weights (2-point Gauss-legendre) tQ = [ -1 1 ]'/sqrt(3); wQ = [ 1 1 ]; h = xCoords(2)-xCoords(1); %element width Ke = zeros(2); Me = Ke; %stiffness matrix Ke(1,1) = wQ*(N1p(tQ).*N1p(tQ)); Ke(1,2) = wQ*(N1p(tQ).*N2p(tQ)); Ke(2,1) = Ke(1,2); Ke(2,2) = wQ*(N2p(tQ).*N2p(tQ)); Ke = Ke*2/h; %Mass matrix Me(1,1) = wQ*(N1(tQ).*N1(tQ)); Me(1,2) = wQ*(N1(tQ).*N2(tQ)); Me(2,1) = Me(1,2); Me(2,2) = wQ*(N2(tQ).*N2(tQ)); Me = Me*h/2; Galerkin Finite Elements: Matlab function [fe] = getElementForceVector(func,xCoords) % getElementForceVector - returns element force vector for an individual element % % [fe] = getElementForceVector(func,xCoords) % % func:-function handle to RHS f(x) % xCoords:- coordinates of 1D element [ xMin xMax ] % % fe: - element force vector % h = xCoords(2)-xCoords(1); %element width N1 = @(t) (1-t)/2; % linear hat function N2 = @(t) (1+t)/2; x = @(t) xCoords(1) + h*(t + 1)/2; % affine transformation % quadrature points and weights (2-point Gauss-legendre) tQ = [ -1 1 ]'/sqrt(3); wQ = [ 1 1 ]; h = xCoords(2)-xCoords(1); %element width %Force vector matrix fe(1) = wQ*(func(x(tQ)).*N1(tQ)); fe(2) = wQ*(func(x(tQ)).*N2(tQ)); fe = fe'*h/2; Lecture26 May 05, 2008 Galerkin Finite Elements: Global Matrix assembly 1 0 xi-1 xi xi+1 Galerkin Finite Elements: Matlab function [A,f] = assembleGlobalProblem(xCoords,func,alpha,beta) % assembleGlobalProblem - loops over elements and assembles global matrix % and right-hand side and sets Dirichlet Boundary conditions % % [A,f] = assembleGlobalProblem(func,xCoords,alpha,beta) % % xCoords:- coordinates of all elements % func:-function handle to RHS f(x) % alpha: - dirichlet condition at xCoords(1); % beta:- dirichlet condition at xCoords(end); % % A: Global stiffness+Mass matrix K+M % f: Global force vector % N = length(xCoords); % numbier of points nels = N - 1; % number of elements A = spalloc(N,N,3*N); f = zeros(N,1); for k = 1:nels % loop over elements and assemble global matrix kEl = k:k+1; % index of element k [Ke,Me] = getElementMatrix(xCoords(kEl)); fe = getElementForceVector(func,xCoords(kEl)); A(kEl,kEl) = A(kEl,kEl) + Ke+Me; f(kEl) = f(kEl)+fe; end %fix dirichlet boundary conditions A(1,1:2)=[ 1 0 ]; A(end,end-1:end)=[ 0 1 ]; f([ 1 end ]) = [ alpha ; beta ]'; Lecture26 May 05, 2008 Galerkin Finite Elements: The future Linear hats on a line is the simplest problem... but Finite Elements is a very rich subject... Possible extensions: 1) higher order elements (quadratic, cubic, "spectral", mixed) 2) Higher spatial dimensions: meshing in 2 (and 3-D) Finite element calculation of viscous fluid flow around a "dolfin" Galerkin Finite Elements: The future Extensions: 3) Space-time PDE's 4) Multi-physics/Multi-Scale models 5) Peta-scale high-performance computation 6) Advanced software design Point: If you're interested, computational science and modeling can be a very rewarding mix of Mathematics, Computer Science and domain specific science and engineering. We've just scratched the surface in this course...but understanding these building blocks is where you start...the fun is just beginning...
© Copyright 2026 Paperzz