Jonathan Whiteley Auth. Finite Element Methods A Practical Guide

Download as pdf or txt
Download as pdf or txt
You are on page 1of 236

Mathematical Engineering

JonathanWhiteley

Finite
Element
Methods
A Practical Guide
Mathematical Engineering

Series editors
Jrg Schrder, Essen, Germany
Bernhard Weigand, Stuttgart, Germany
Today, the development of high-tech systems is unthinkable without mathematical
modeling and analysis of system behavior. As such, many elds in the modern
engineering sciences (e.g. control engineering, communications engineering,
mechanical engineering, and robotics) call for sophisticated mathematical methods
in order to solve the tasks at hand.
The series Mathematical Engineering presents new or heretofore little-known
methods to support engineers in nding suitable answers to their questions,
presenting those methods in such manner as to make them ideally comprehensible
and applicable in practice.
Therefore, the primary focus iswithout neglecting mathematical accuracyon
comprehensibility and real-world applicability.
To submit a proposal or request further information, please use the PDF Proposal
Form or contact directly: Dr. Jan-Philip Schmidt, Publishing Editor (jan-philip.
[email protected]).

More information about this series at http://www.springer.com/series/8445


Jonathan Whiteley

Finite Element Methods


A Practical Guide

123
Jonathan Whiteley
Department of Computer Science
University of Oxford
Oxford
UK

Additional material to this book can be downloaded from http://extras.springer.com.

ISSN 2192-4732 ISSN 2192-4740 (electronic)


Mathematical Engineering
ISBN 978-3-319-49970-3 ISBN 978-3-319-49971-0 (eBook)
DOI 10.1007/978-3-319-49971-0
Library of Congress Control Number: 2016958482

Springer International Publishing AG 2017


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part
of the material is concerned, specically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microlms or in any other physical way, and transmission
or information storage and retrieval, electronic adaptation, computer software, or by similar or
dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this
publication does not imply, even in the absence of a specic statement, that such names are exempt
from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this
book are believed to be true and accurate at the date of publication. Neither the publisher nor the
authors or the editors give a warranty, express or implied, with respect to the material contained
herein or for any errors or omissions that may have been made.

Printed on acid-free paper

This Springer imprint is published by Springer Nature


The registered company is Springer International Publishing AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
Preface

This book has evolved from courses on the nite element method that have been
taught as part of several graduate programmes at the University of Oxford. These
courses have all focused on the practical application of the nite element method.
A typical course would begin with a specic class of differential equations being
written down. The course would then provide students with all the detail that is
required to develop a computational implementation of the nite element method
for this class of differential equations. This approach requires the following topics
to be addressed: (i) derivation of the weak formulation of a differential equation;
(ii) discretisation of the domain on which the differential equation is dened into
elements; (iii) specication of suitable basis functions; (iv) derivation of a system of
algebraic equations that is derived from the nite element formulation of the dif-
ferential equation; (v) solution of this system of algebraic equations; and (perhaps
most importantly) (vi) the practical implementation of all steps. This book is written
in the spirit in which these courses have been taught. Mathematical rigour is cer-
tainly needed when discussing some of these topics, such as the use of Sobolev
spaces when deriving the weak formulation of a differential equation. The focus,
however, is on the practical implementation of the nite element method for a given
differential equation.
This book begins with a brief overview of the nite element method in Chap. 1.
Chapters 26 then focus on the application of the nite element method to ordinary
differential equations. In Chap. 2, we begin with a very simple example differential
equation. Although simple, this example allows illustration of the key ideas that
underpin the nite element method. We then consider general, linear ordinary
differential equations in Chap. 3, providing a more rigorous discussion than in
Chap. 2. The material in Chap. 3 focuses exclusively on nite element solutions that
are a linear approximation to the true solution on each element. These concepts are
extended in Chap. 4 to explain how a higher order polynomial approximation may
be used on each element. We then explain how nonlinear ordinary differential
equations are handled by the nite element method in Chap. 5, and how systems of
ordinary differential equations are handled in Chap. 6. In all of these chapters, we
carefully explain how the values of the solution at each node of the computational

v
vi Preface

mesh satisfy a system of algebraic equations. We explain how these algebraic


equations may be assembled, and how they may be solved. Each chapter contains
an exemplar computational implementation in the form of MATLAB functions.
Chapters 712 focus on the application of the nite element method to elliptic
partial differential equations, and these chapters take a very similar approach to that
used in earlier chapters. We begin with a simple example in Chap. 7, before
considering more general linear elliptic equations in Chap. 8. In both of these
chapters, we partition the domain on which the differential equation is dened into
triangular elements. In Chap. 9, we explain how the material presented in Chap. 8
may be modied to allow the use of a mesh that arises from partitioning the domain
into quadrilateral elements. Higher order polynomial approximations to the solution
are considered in Chap. 10, followed by nonlinear partial differential equations in
Chap. 11 and systems of elliptic equations in Chap. 12. Exemplar computational
implementations of the nite element method are given in several of these chapters.
Finally, in Chap. 13, we explain how the nite element method may be applied to
parabolic partial differential equations.
Every application of the nite element method in this book requires the solution
of a system of algebraic equations. Appendix A provides a brief summary of
computational techniques that are available for solving both linear and nonlinear
systems of algebraic equations. This summary covers both direct and iterative
methods for solving linear systems, preconditioners that accelerate the convergence
of iterative methods for solving linear systems, and iterative methods for solving
nonlinear systems.
My understanding and appreciation of the nite element method has been
substantially enhanced by discussions with colleagues too numerous to mention.
Special mention must, however, be made of both the Computational Biology Group
in the Department of Computer Science at the University of Oxford, and the
Numerical Analysis Group in the Mathematical Institute at the University of
Oxford. The nite element research interests of these two groups cover a wide
spectrum, addressing a range of both practical and theoretical issues. I am very
fortunate to have been a member of both of these research groups for periods of
time over the last two decades.
Above all, I would like to thank my family for their support during the writing of
this book.

Oxford, UK Jonathan Whiteley


April 2016
Contents

1 An Overview of the Finite Element Method . . . . . . . . . . . . . . . . . . . 1


1.1 Features of the Finite Element Method . . . . . . . . . . . . . . . . . . . . 2
1.2 Using This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 A First Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1 Some Brief Mathematical Preliminaries . . . . . . . . . . . . . . . . . . . 5
2.2 A Model Differential Equation . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Elements and Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.5 Basis Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.6 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.7 Algebraic Equations Satised by the Finite
Element Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.8 Assembling the Algebraic Equations . . . . . . . . . . . . . . . . . . . . . 13
2.8.1 Calculating the Local Contributions . . . . . . . . . . . . . . . 15
2.8.2 Assembling the Global Matrix . . . . . . . . . . . . . . . . . . . . 16
2.9 A Summary of the Steps Required . . . . . . . . . . . . . . . . . . . . . . . 17
2.10 Computational Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.11 Evaluating the Finite Element Solution at a Given Point . . . . . . 19
2.12 Is the Finite Element Solution Correct? . . . . . . . . . . . . . . . . . . . 20
2.13 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3 Linear Boundary Value Problems . . . . . . . . . . . . . . . . . . ......... 25
3.1 A General Boundary Value Problem . . . . . . . . . . . . ......... 25
3.1.1 A Note on the Existence and Uniqueness
of Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.2 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2.1 Some Denitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2.2 Deriving the Weak Formulation . . . . . . . . . . . . . . . . . . 28

vii
viii Contents

3.3 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30


3.3.1 The Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.3.2 Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.3.3 Sets of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3.4 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . 34
3.3.5 The System of Algebraic Equations . . . . . . . . . . . . . . . 35
3.4 Assembling the Algebraic Equations . . . . . . . . . . . . . . . . . . . . . 37
3.4.1 The Contributions from Integrals . . . . . . . . . . . . . . . . . . 37
3.4.2 Completing the Assembly of the Linear System . . . . . . 39
3.5 Approximating Integrals Using Numerical Quadrature . . . . . . . . 40
3.6 The Steps Required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.7 Computational Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.8 Robin Boundary Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.8.1 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.8.2 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . 47
3.9 A Bound on the Accuracy of the Solution . . . . . . . . . . . . . . . . . 47
3.10 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4 Higher Order Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.1 A Model Differential Equation . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2 Quadratic Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
4.2.1 The Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
4.2.2 The Denition of Quadratic Basis Functions . . . . . . . . . 57
4.2.3 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . 60
4.2.4 The System of Algebraic Equations . . . . . . . . . . . . . . . 60
4.2.5 Assembling the Algebraic Equations . . . . . . . . . . . . . . . 62
4.2.6 Computational Implementation . . . . . . . . . . . . . . . . . . . 65
4.2.7 A Note on Using Quadrature for Higher Order
Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.3 Cubic Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.3.1 The Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
4.3.2 The Denition of Cubic Basis Functions . . . . . . . . . . . . 71
4.3.3 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . 73
4.3.4 Assembling the Algebraic Equations . . . . . . . . . . . . . . . 74
4.4 General Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4.4.1 The Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.4.2 Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.4.3 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . 77
4.5 Convergence of the Finite Element Solution . . . . . . . . . . . . . . . . 78
4.6 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Contents ix

5 Nonlinear Boundary Value Problems . . . . . . . . . . . . . . . . . . . . .... 81


5.1 A First Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .... 81
5.1.1 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . .... 82
5.1.2 The Mesh, Basis Functions and Finite
Element Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.1.3 The Algebraic Equations . . . . . . . . . . . . . . . . . . . . . . . . 83
5.1.4 Assembling the Algebraic Equations . . . . . . . . . . . . . . . 85
5.1.5 Computational Implementation . . . . . . . . . . . . . . . . . . . 87
5.1.6 Calculation of the Jacobian Matrix . . . . . . . . . . . . . . . . 90
5.1.7 Numerical Approximation of the Jacobian Matrix . . . . . 96
5.2 A General Nonlinear Boundary Value Problem . . . . . . . . . . . . . 97
5.2.1 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . 97
5.2.2 The Nonlinear System of Algebraic Equations . . . . . . . 98
5.3 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
6 Systems of Ordinary Differential Equations . . . . . . . . . . . . . . . . . . . 103
6.1 A Model Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
6.2 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
6.3 The Mesh and Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.4 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
6.5 The Algebraic Equations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
6.6 Assembling the Algebraic Equations . . . . . . . . . . . . . . . . . . . . . 109
6.7 Computational Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 110
6.8 More General Linear Problems . . . . . . . . . . . . . . . . . . . . . . . . . . 112
6.8.1 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . 113
6.8.2 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . 114
6.9 Nonlinear Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
6.10 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
7 Linear Elliptic Partial Differential Equations . . . . . . . . . . . . . . . . . . 119
7.1 A First Model Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
7.2 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
7.2.1 Sobolev Spaces in Two Dimensions . . . . . . . . . . . . . . . 120
7.2.2 Deriving the Weak Formulation . . . . . . . . . . . . . . . . . . 121
7.3 The Mesh and Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 122
7.3.1 A Mesh of Triangular Elements . . . . . . . . . . . . . . . . . . 122
7.3.2 Linear Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . 125
7.4 Sets of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
7.5 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
7.6 The System of Algebraic Equations . . . . . . . . . . . . . . . . . . . . . . 130
7.6.1 Satisfying the Dirichlet Boundary Conditions . . . . . . . . 130
7.6.2 Using Suitable Test Functions . . . . . . . . . . . . . . . . . . . . 130
7.6.3 The Linear System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
x Contents

7.7 Assembling the System of Algebraic Equations . . . . . . . . . . . . . 132


7.7.1 Assembling the Entries Dened by Integrals . . . . . . . . . 132
7.7.2 Setting the Entries Dened Explicitly . . . . . . . . . . . . . . 135
7.8 Evaluating the Solution at a Given Point . . . . . . . . . . . . . . . . . . 135
7.9 Computational Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 136
7.10 More Complex Geometries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
7.11 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
8 More General Elliptic Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
8.1 A Model Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
8.2 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
8.3 The Mesh and Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 145
8.4 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
8.5 The Algebraic Equations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
8.5.1 Satisfying the Dirichlet Boundary Conditions . . . . . . . . 146
8.5.2 Using Suitable Test Functions . . . . . . . . . . . . . . . . . . . . 146
8.5.3 The Linear System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
8.6 Assembling the Algebraic Equations . . . . . . . . . . . . . . . . . . . . . 147
8.6.1 Evaluating Integrals Over . . . . . . . . . . . . . . . . . . . . . 148
8.6.2 Evaluating Integrals Over oN . . . . . . . . . . . . . . . . . . . 149
8.6.3 Setting Entries Dened Explicitly . . . . . . . . . . . . . . . . . 150
8.7 Quadrature Over Triangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
8.8 Computational Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 152
8.9 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
9 Quadrilateral Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
9.1 A Model Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
9.2 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
9.3 The Computational Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
9.4 Basis Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
9.5 Sets of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
9.6 The Finite Element Formulation . . . . . . . . . . . . . . . . . . . . . . . . . 166
9.7 The System of Algebraic Equations . . . . . . . . . . . . . . . . . . . . . . 167
9.8 Assembling the System of Algebraic Equations . . . . . . . . . . . . . 168
9.8.1 Evaluating Integrals Over . . . . . . . . . . . . . . . . . . . . . 169
9.8.2 Evaluating Integrals Over N . . . . . . . . . . . . . . . . . . . . 171
9.8.3 Setting Entries Dened Explicitly . . . . . . . . . . . . . . . . . 171
9.9 Quadrature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
9.10 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
10 Higher Order Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
10.1 The Model Boundary Value Problem
and Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
10.2 Quadratic Basis Functions on a Mesh
of Triangular Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Contents xi

10.3 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180


10.4 The Algebraic Equations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
10.5 Assembling the Algebraic Equations . . . . . . . . . . . . . . . . . . . . . 181
10.5.1 Evaluating Integrals Over . . . . . . . . . . . . . . . . . . . . . 181
10.5.2 Evaluating Integrals Over N . . . . . . . . . . . . . . . . . . . . 183
10.5.3 Setting
Entries Dened Explicitly . . . . . . . . . . . . . ................. 184
10.6 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . ................. 184
11 Nonlinear Elliptic Partial Differential Equations . . . . . . . . . . . . . . . . 189
11.1 A Model Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
11.2 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
11.3 The Mesh and Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 191
11.4 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
11.5 The Nonlinear System of Algebraic Equations . . . . . . . . . . . . . . 192
11.5.1 Satisfying the Dirichlet Boundary Conditions . . . . . . . . 192
11.5.2 Using Suitable Test Functions . . . . . . . . . . . . . . . . . . . . 192
11.6 Assembling the Nonlinear System of Algebraic Equations . . . . . 193
11.6.1 Evaluating Integrals Over . . . . . . . . . . . . . . . . . . . . . 194
11.6.2 Evaluating Integrals Over oN . . . . . . . . . . . . . . . . . . . 195
11.6.3 Setting Entries Dened Explicitly . . . . . . . . . . . . . . . . . 196
11.7 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
12 Systems of Elliptic Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
12.1 A Model Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
12.2 The Weak Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
12.3 The Mesh and Basis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 199
12.4 The Finite Element Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
12.5 The Algebraic Equations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
12.6 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
13 Parabolic Partial Differential Equations . . . . . . . . . . . . . . . . . . . . . . 203
13.1 A Linear Parabolic Partial Differential Equation . . . . . . . . . . . . . 203
13.2 A Nonlinear Parabolic Equation . . . . . . . . . . . . . . . . . . . . . . . . . 205
13.3 A Semi-implicit Discretisation in Time . . . . . . . . . . . . . . . . . . . . 207
13.4 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Appendix A: Methods for Solving Linear and Nonlinear Systems
of Algebraic Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Appendix B: Vector Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Chapter 1
An Overview of the Finite Element Method

Differential equations play a central role in quantitative sciences and are used to
describe phenomena as diverse as the flow of traffic through a city, the evolution of
an oilfield over many years, the flow of ions through an electrochemical solution and
the effect of a drug on a given patient. Most differential equations that are encountered
in practice do not possess an analytic solution, that is a solution that can be written
in terms of known functions. Under these circumstances, a numerical solution to
the differential equation is often sought. The finite element method is one common
technique for calculating a numerical solution of a differential equation.
The underlying principle of the finite element method is very simple to express.
The domain on which the differential equation is defined is partitioned into many
smaller regions known as elements, and the solution of the differential equation is
approximated on each of these elements using a low-order polynomial function.
Consider the example partial differential equation, defined on the interior of the
ellipse x 2 /4 + y 2 = 1 by:
 
2u 2u
+ 2 = 1.
x 2 y

We apply Dirichlet boundary conditions on the boundary of the ellipse where y 0,


given by

u(x, y) = x,

and Neumann boundary conditions on the boundary of the ellipse where y > 0,
given by

(u) n = sin(2(x + y)).

Springer International Publishing AG 2017 1


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_1
2 1 An Overview of the Finite Element Method

Fig. 1.1 The partitioning of 1


the domain x 2 /4 + y 2 < 1
into triangular elements
0.5

y
0.5

1
2 1 0 1 2
x

Fig. 1.2 The finite element


approximation to the solution
of the differential equation 4

2
u

2
2
2
0 0
y 2 2 x

The vector n that appears in the specification of the Neumann boundary conditions
is the normal vector pointing out of the ellipse, with unit length.
To calculate the finite element solution of the differential equation above, we first
partition the domain on which the differential equation is defined, namely the interior
of the ellipse x 2 /4 + y 2 = 1, into elements. We illustrate how this domain may be
partitioned into triangular elements in Fig. 1.1.
Having partitioned the ellipse into triangular elements, we then calculate the
finite element solution of the differential equation. The finite element solution that
is a linear approximation to the solution on each element is shown in Fig. 1.2.

1.1 Features of the Finite Element Method

We will see in later chapters that the finite element method is a very flexible method
for calculating the numerical solution of differential equations. In particular, it has
three key features that make it very attractive from a practical viewpoint. First, the
1.1 Features of the Finite Element Method 3

finite element method can handle domains, such as that shown in Fig. 1.1, that are of
arbitrary shape. This is clearly a useful property, as in practice differential equations
are posed on domains such as the exterior of an aircraft (when estimating the forces
acting on the aircraft); a whole country (when modelling the spread of a disease); or a
subjects lungs (when modelling oxygen transport from air into the subjects blood).
The second key feature of the finite element method is that boundary conditions that
include the derivative of the solution, such as the Neumann boundary conditions for
the example differential equation presented earlier in this chapter, may be handled
very easily. Finally, when we desire more accuracy in our finite element solution, we
have two optionswe may partition the domain into smaller triangular elements or
increase the degree of the polynomial approximation on each element.
The finite element method also has attractive features when viewed from a theoret-
ical standpoint. As we are approximating the true solution by a polynomial function
on each element, functional analysis may be used to assess how well the finite element
solution approximates the true solution. Very powerful theorems exist for estimating
the error between the finite element solution and the true solution of a differential
equation. These theorems have spawned practical techniques for guiding the parti-
tioning of the domain so that the finite element solution is within a user supplied
tolerance of the true solution.

1.2 Using This Book

This book is organised so that the application of the finite element method to ordinary
differential equations is covered in Chaps. 26, and the application to partial differen-
tial equations is covered in Chaps. 713. For both ordinary differential equations and

Table 1.1 The prerequisite Chapter Prerequisite chapter(s)


chapter(s) for each chapter in
this book 2 1
3 1, 2
4 1, 2, 3
5 1, 2, 3
6 1, 2, 3
7 1, 2, 3
8 1, 2, 3,7
9 1, 2, 3, 7, 8
10 1, 2, 3, 4, 7, 8
11 1, 2, 3, 5, 7, 8
12 1, 2, 3, 6, 7, 8
13 1, 2, 3, 7, 8
4 1 An Overview of the Finite Element Method

partial differential equations, we first explain how to calculate finite element solutions
that use a linear approximation to the solution on each element. Subsequent chapters
then address the use of higher order approximations to the solution on each element;
nonlinear differential equations; and systems of differential equations. Some readers
may not want to read all chapters as they may, for example, only be interested in
learning how to apply the finite element method to linear differential equations. In
Table 1.1, we list the prerequisite chapters for each chapter of this book.
Many chapters include exemplar computational implementations that are written
for use with Matlab. The Matlab functions are intended to aid the readers under-
standing through being written in a way that is as close as possible to the algorithms
that are derived earlier in the chapter. As a consequence, some fragments of these list-
ings are written using more lines of code than is strictly necessary; some for loops,
for example, could be written in a more elegant style using the vectorisation feature
offered by Matlab. The justification for this more verbose programming style is that
it will allow readers who choose to use a programming language other than Matlab
to easily translate the listings provided into their programming language of choice.
These Matlab listings may be downloaded from http://extras.springer.com.
Chapter 2
A First Example

In Chap. 1, we explained the fundamental philosophy that underpins the finite element
methodthat is, the region on which a differential equation is defined is partitioned
into smaller regions known as elements, and the solution on each of these elements
is approximated using a low-order polynomial function. In this chapter, with the aid
of a simple example, we illustrate how this may be done. This overview will require
the definition of some terms that the reader may not be familiar with, as well as
a few technical details. We will, however, undertake to keep these definitions to a
minimum and will focus on the underlying strategy of applying the finite element
method without getting bogged down by these technical details. As a consequence,
we will inevitably skate over some mathematical rigour, but will make a note to
return to these points in later chapters.

2.1 Some Brief Mathematical Preliminaries

We make a few comments on notation before we begin our overview of the finite
element method. Throughout this book, we use the convention that a solution of a
differential equation that is represented by a lowercase letter, for example u(x), is the
exact, or analytic, solution of the differential equation. The corresponding uppercase
letter, U(x) in this case, will represent the finite element approximation to the exact
solution.
We will frequently make use of vectors and matrices, and adopt the following
conventions.

1. Vectors will be assumed to be column vectors, i.e. a vector with many rows but
only one column, and will be denoted by bold font. Entry i of the vector x will
be denoted by xi .

Springer International Publishing AG 2017 5


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_2
6 2 A First Example

2. Matrices will be represented by upper case letters typeset in italic font. The entry
in row i and column j of the matrix A will be denoted by Ai,j .
3. The indexing of both vectors and matrices will start from 1. A vector b with
N entries, for example, will have entries b1 , b2 , . . . , bN . This allows the finite
element algorithms to be written in a way that facilitates the writing of software
in programming languages or environments such as Matlab, Octave or Fortran,
where array indexing starts from 1 (known as one-based indexing). Readers
using programming languages such as C, C++ and Python, where the indexing
of arrays starts from 0 (known as zero-based indexing) will need to adapt the
algorithms to take account of this feature of the programming language being
used.

2.2 A Model Differential Equation

We will use a simple boundary value problem to demonstrate the application of


the finite element method. A boundary value problem comprises both a differential
equation and boundary conditions. In this chapter, we will consider only Dirichlet
boundary conditions, where the value of the unknown function u(x) is specified
on the boundary. More complex boundary conditions, such as those that include the
derivative of u(x) on the boundary, will be discussed in Chap. 3. Our model boundary
problem comprises the differential equation

d2 u
= 2, 0 < x < 1, (2.1)
dx 2
and the Dirichlet boundary conditions given by

u(0) = u(1) = 0. (2.2)

The region 0 < x < 1 on which the equation is defined is known as the domain.
This simple example, with solution u(x) = x(1 x), will now be used to exhibit
the underlying principles of calculating the finite element solution of a differential
equation.

2.3 The Weak Formulation

Let u(x) be the solution of the model differential equation, Eq. (2.1), subject to the
boundary conditions given by Eq. (2.2). The function u(x) is known as the classical
solution of this differential equation, and specification of u(x) through Eqs. (2.1) and
(2.2) is known as the classical formulation.
2.3 The Weak Formulation 7

We now introduce the weak formulation of the model problem, a concept that
may be unfamiliar to some readers. In this chapter, we limit ourselves to illustrating
the weak formulation of a differential equation by example, rather than giving a
watertight definition through precise mathematical statements. A more complete
and rigorous treatment of this material will be presented in Sect. 3.2.
To derive the weak formulation of our model problem, we first define v(x) to be
a continuous function that satisfies

v(0) = v(1) = 0, (2.3)

so that v(x) = 0 at the values of x where we apply Dirichlet boundary conditions.


We now multiply the differential equation, given by Eq. (2.1), by v(x), and integrate
the resulting product over the interval 0 < x < 1 on which the differential equation
is defined, giving
 1  1
d2 u
2 v(x) dx = 2v(x) dx.
0 dx 0

We now apply integration by parts to the left-hand side of the equation above to
obtain
 1  1  1
du du dv
v(x) + dx = 2v(x) dx,
dx 0 0 dx dx 0

which may be written as


   1  1
du  du  du dv
v(1) + v(0) + dx = 2v(x) dx, (2.4)
dx x=1 dx x=0 0 dx dx 0

where
 
du  du 
and
dx x=1 dx x=0

denote the value taken by du dx


at x = 1 and x = 0, respectively. Remembering our
condition on v(x), given by Eq. (2.3), we see that the first two terms in the sum on
the left-hand side of Eq. (2.4) are zero, and we may write this equation as
 1  1
du dv
dx = 2v(x) dx.
0 dx dx 0

Assuming that all derivatives and integrals we have used exist, we may now specify
the weak formulation of our model differential equation by:
8 2 A First Example

find u(x) that satisfies the Dirichlet boundary conditions given by Eq. (2.2) and
is such that
 1  1
du dv
dx = 2v(x) dx, (2.5)
0 dx dx 0

for all continuous functions v(x) that satisfy v(0) = v(1) = 0.

The solution u(x) given by Eq. (2.5) is known as the weak solution. The functions
v(x) that appear in Eq. (2.5) are known as test functions.
The concept of weak solutions will be revisited in the next chapter, providing
some mathematical details that have been omitted above. For now, the reader need
only know what has been derived above, namely that a classical solution of the
differential equation and boundary conditions given by Eqs. (2.1) and (2.2) is also a
weak solution that satisfies Eq. (2.5).

2.4 Elements and Nodes

We now begin to lay out some of the ingredients required by the finite element
method. We have already explained that we need to partition the domain on which
the differential equation is specified into smaller regions. Our model differential
equation, Eq. (2.1), is defined on the domain 0 < x < 1, and we now partition this
domain into N intervals of equal length h = N1 . If we define

i1
xi = , i = 1, 2, . . . , N + 1, (2.6)
N

then these intervals are the regions xk x xk+1 , for k = 1, 2, . . . , N. We refer


to these intervals as elements. The points xi that define the element boundaries are
known as nodes. We also define element k to be the element that occupies xk x
xk+1 , for k = 1, 2, . . . , N. The elements and nodes are collectively known as the
computational mesh, the finite element mesh or, more simply, the mesh. The nodes
and elements for the case where N = 5 are shown in Fig. 2.1.

x1 = 0 x2 = 0.2 x3 = 0.4 x4 = 0.6 x5 = 0.8 x6 = 1

Fig. 2.1 The nodes and elements in a finite element mesh when the interval 0 < x < 1 is partitioned
into five equally sized elements
2.4 Elements and Nodes 9

The mesh we have described above contains elements that are equally sized. We
will see in the next chapter that it is straightforward to generalise the definition of
the mesh so that it contains elements that are of different sizes.

2.5 Basis Functions

In this chapter, we will use the finite element method to calculate a linear approxima-
tion to the solution on each element, that is continuous across element boundaries.
In the previous section, we partitioned the domain 0 < x < 1 into N equally sized
elements, each of length h = N1 . Note that this implies that

xj+1 xj = h, j = 1, 2, . . . , N. (2.7)

We now specify some functions that will be required when calculating the finite
element solution. Using the property of h given by Eq. (2.7), the functions j (x), for
j = 1, 2, . . . , N + 1, are defined by

(x2 x)/h, x1 x x2 ,
1 (x) = (2.8)
0, otherwise,

(x xj1 )/h, xj1 x xj ,
j (x) = (xj+1 x)/h, xj x xj+1 , j = 2, 3, . . . , N, (2.9)

0, otherwise,

(x xN )/h, xN x xN+1 ,
N+1 (x) = (2.10)
0, otherwise.

These functions are illustrated in Fig. 2.2. For reasons that will soon become clear,
we refer to these functions as basis functions.
The functions j (x), j = 1, 2, . . . , N + 1, defined by Eqs. (2.8)(2.10) have three
properties that are useful when calculating the finite element solution: (i) they are
linear functions on each element; (ii) they are continuous functions; and (iii) they
satisfy, for i = 1, 2, . . . , N + 1, the condition

1, i = j,
j (xi ) = (2.11)
0, i = j.

These properties may easily be verified using the definition of basis functions given
by Eqs. (2.8)(2.10), and the plots of these functions in Fig. 2.2. As a consequence,
the function j (x) takes the value 1 at the node where x = xj , and the value 0 at all
other nodes. This is a very important property of basis functions, as we will see in
later chapters when using more general basis functions to calculate the finite element
approximation of more complex differential equations.
10 2 A First Example

1 (x) j (x) N+1 (x)

0
x1 x2 xj1 xj xj+1 xN xN+1

Fig. 2.2 The basis functions defined by Eqs. (2.8)(2.10)

2.6 The Finite Element Solution

We will now use the basis functions j (x), j = 1, 2, . . . , N +1, defined by Eqs. (2.8)
(2.10), to generate a finite element solution of our model differential equation that is
a linear approximation to the solution on each element. We claim that we can write
the finite element solution, U(x), as a linear sum of these basis functions:

N+1
U(x) = Uj j (x), (2.12)
j=1

where the as yet unknown values Uj , j = 1, 2, . . . , N + 1, are to be determined.


Our claim that the finite element solution, U(x), is of the form given by Eq. (2.12)
is straightforward to verify. As the basis functions j (x) are linear functions on each
element and continuous, the sum given by Eq. (2.12) also satisfies these properties
and is therefore a suitable candidate for the finite element solution. Finally we note
that, on using the property of basis functions given by Eq. (2.11), we may write

N+1
U(xi ) = Uj j (xi )
j=1
= Ui , (2.13)
2.6 The Finite Element Solution 11

and so Ui is the finite element approximation to the solution at the node where x = xi .
We now move on to describe how to determine the values Ui , i = 1, 2, . . . , N + 1,
that complete the definition of the finite element solution given by Eq. (2.12).

2.7 Algebraic Equations Satisfied by the Finite Element


Solution

Having prescribed the functional form for the finite element solution by Eq. (2.12),
we now generate a system of algebraic equations satisfied by the coefficients Uj ,
j = 1, 2, . . . , N + 1. This is achieved by modifying the weak formulation of the
model differential equation, given by Eq. (2.5), to specify the finite element solution
U(x) by:

find U(x) that satisfies the Dirichlet boundary conditions given by Eq. (2.2)
and is such that
 1  1
dU di
dx = 2i (x) dx, (2.14)
0 dx dx 0

for all basis functions i (x), i = 1, 2, . . . , N + 1, that satisfy i (0) =


i (1) = 0.

The statement above is nothing more than the weak formulation, Eq. (2.5), restated
with two minor modifications. First, we replace the weak solution u(x) by the finite
element solution U(x). Second we restrict the test functions to be the finite set of
basis functions i (x) that satisfy

i (0) = i (1) = 0.

We see, from the definitions of the basis functions given by Eqs. (2.8)(2.10) and their
illustration in Fig. 2.2, that this condition on i (x) is satisfied by all basis functions
except 1 (x) and N+1 (x). We therefore have N 1 functions 2 (x), 3 (x), . . . , N (x)
that satisfy this condition.
We now use Eq. (2.14) to derive a system of algebraic equations, satisfied by the
N + 1 unknown values U1 , U2 , . . . , UN+1 , that specify the finite element solution
given by Eq. (2.12). As we have N + 1 unknown values, we require a system of N + 1
algebraic equations to determine these values.
The first condition in the specification of the finite element solution by Eq. (2.14)
is that U(x) satisfies the Dirichlet boundary conditions given by Eq. (2.2). These
boundary conditions are applied at the node where x = x1 and the node where
x = xN+1 and may be written as

U(x1 ) = 0, U(xN+1 ) = 0.
12 2 A First Example

Using Eq. (2.13), we see that these Dirichlet boundary conditions are satisfied pro-
vided that

U1 = 0, (2.15)
UN+1 = 0, (2.16)

and these are our first two algebraic equations.


We have already noted that the N 1 basis functions i , where i = 2, 3, . . . , N,
satisfy the condition on test functions given in Eq. (2.14), i.e. that i (0) = i (1) = 0.
The remaining N 1 algebraic equations result from substituting these functions
into the integral equation given in Eq. (2.14). Noting that the definition of the finite
element solution given by Eq. (2.12) may be differentiated to give

dU
dj N+1
= Uj ,
dx j=1
dx

dU
substitution of dx
into the integral equation given by Eq. (2.14) yields

 1

N+1  1
dj di
Uj dx = 2i (x) dx, i = 2, 3, . . . , N,
0 j=1
dx dx 0

which, after a little manipulation, becomes


 1
N+1
di dj
  1
dx Uj = 2i (x) dx, i = 2, 3, . . . , N. (2.17)
j=1 0 dx dx 0

This may be written as

N+1
Ai,j Uj = bi , i = 2, 3, . . . , N, (2.18)
j=1

where for i = 2, 3, . . . , N,
 1
di dj
Ai,j = dx, j = 1, 2, 3, . . . , N + 1, (2.19)
0 dx dx
 1
bi = 2i (x) dx. (2.20)
0
2.7 Algebraic Equations Satisfied by the Finite Element Solution 13

We may combine Eqs. (2.15), (2.16) and (2.18) into the (N + 1) (N + 1) linear
system

AU = b. (2.21)

The entries of A and b in row i, where i = 2, 3, . . . , N, are given by Eqs. (2.19) and
(2.20). Using Eqs. (2.15) and (2.16), we see that the entries of A and b in rows 1 and
N + 1 are given by

1, j = 1,
A1,j = (2.22)
0, j = 1,

1, j = N + 1,
A(N+1),j = (2.23)
0, j = N + 1,
b1 = 0, (2.24)
bN+1 = 0. (2.25)

The algebraic equations that comprise the linear system given by Eq. (2.21) fall
into two categories. The first category is equations such as Eqs. (2.15) and (2.16) that
arise from demanding that the finite element solution satisfies all Dirichlet boundary
conditions. The second category is equations such as Eq. (2.18) that arise from using a
suitable basis function as the test function in the integral condition given by Eq. (2.14).
This categorisation of equations will be a feature of all the systems of algebraic
equations that we derive in later chapters.
Having derived the linear system of algebraic equations that determines the values
U1 , U2 , . . . , UN+1 , we now introduce a flexible technique for the practical computa-
tion of the entries of the matrix A and the vector b.

2.8 Assembling the Algebraic Equations

Most entries of the matrix A and vector b that appear in the linear system given
by Eq. (2.21) are of the form specified by Eqs. (2.19) and (2.20) and are defined by
integrals over the whole domain on which the differential equation is specified. We
will now demonstrate that, from a computational implementation viewpoint, it is
convenient to compute these entries by summing the contributions from individual
elements. That is, for i = 2, 3, . . . , N, we write Eqs. (2.19) and (2.20) as the sum of
integrals over individual elements:
14 2 A First Example

N 

xk+1
di dj
Ai,j = dx, j = 1, 2, . . . , N + 1, (2.26)
xk dx dx
k=1
N 

xk+1
bi = 2i (x) dx. (2.27)
k=1 xk

Using Eq. (2.26) we see that the contribution to entry Ai,j from integrating over
the element that occupies xk x xk+1 is given by
 xk+1
di dj
dx. (2.28)
xk dx dx

From the definition of the basis functions, given by Eqs. (2.8)(2.10) and illustrated
in Fig. 2.2, we see that k (x) and k+1 (x) are the only basis functions that are nonzero
on the element occupying xk x xk+1 : all other basis functions are identically
zero on this element. Hence, the integral above, over xk x xk+1 , yields nonzero
contributions only to the entries Ak,k , Ak,k+1 , Ak+1,k , Ak+1,k+1 of A. Motivated by this
observation, we calculate only these nonzero contributions when integrating over this
element and store these contributions in a 2 2 matrix called A(k) local . The relationship
between the location of the entries of A(k) local and the location of the entries of A that
they contribute to is given in Table 2.1.
The entries of A(k)local are known as the local contributions to the matrix A from ele-
ment k, and A is often referred to as the global matrix. We may compute A efficiently
by looping over all elements, calculating only the nonzero local contributions from
each element, before adding the local contributions to the appropriate entries of the
global matrix A. Having calculated the entries of A that are given by Eq. (2.19), we
complete the specification of the matrix by setting the remaining entries, i.e. those
defined by Eqs. (2.22) and (2.23).
The entries of b that are given by Eq. (2.27) may be calculated in a similar manner
to that used for the entries of A that are given by Eq. (2.26). The contribution to
bi , i = 2, 3, . . . , N, from integrating over the element that occupies xk x xk+1
is given by

(k)
Table 2.1 The relationship between the location of the entries of Alocal and the location of the
entries of A that they contribute to
Entry in A(k)
local Entry in A
Row Column Row Column
1 1 k k
1 2 k k+1
2 1 k+1 k
2 2 k+1 k+1
2.8 Assembling the Algebraic Equations 15

Table 2.2 The relationship Entry in b(k)


local Entry in b
between the location of the
(k) 1 k
entries of blocal and the
location of the entries of b 2 k+1
that they contribute to

 xk+1
2i (x) dx. (2.29)
xk

As k (x) and k+1 (x) are the only basis functions that are nonzero inside the element
that occupies xk x xk+1 , the only nonzero contributions to b from this element
are to the entries bk and bk+1 . We again calculate only these nonzero contributions
and store them in a vector of length 2 called b(k)
local , known as the local contribution
to the global vector b. The relationship between the location of the entries of b(k) local
and the location of the entries of b that they contribute to is given in Table 2.2. We
may then calculate the global vector b by looping over all elements, calculating
only the nonzero contributions to Eq. (2.27) from each element, and adding these
contributions into b. We then use Eqs. (2.24) and (2.25) to set the other entries.

2.8.1 Calculating the Local Contributions

We explained above that the entries of the global matrix A and global vector b
that are given by Eqs. (2.19) and (2.20) should be assembled by summing the local
contributions to these entries from each element. We now explain how these local
contributions may be calculated.
Using the mapping between the entries of A(k) local and the entries of A, given by
Table 2.1, and the definition of the contribution to entries of A from element k, given
by Eq. (2.28), we see that the entries of A(k)
local are given by
  xk+1 dk dk
 xk+1 dk dk+1

dx dx
A(k)
xk dx dx xk dx dx
local =  xk+1 dk+1 dk  xk+1 dk+1 dk+1
. (2.30)
xk dx dx
dx xk dx dx
dx

Similarly, using Table 2.2 and Eq. (2.29), the entries of b(k)
local may be written as

  xk+1 
 2k (x) dx
b(k) = xk
xk+1 . (2.31)
local
xk 2k+1 (x) dx

The integrals required to calculate the entries of A(k) (k)


local and blocal are sufficiently simple
in this case that we may evaluate them analytically. Using Eqs. (2.8)(2.10), we see
that, for xk x xk+1 ,
16 2 A First Example

xk+1 x
k (x) = ,
h
x xk
k+1 (x) = ,
h
and so the derivatives of these functions take the constant values given by

dk 1
= ,
dx h
dk+1 1
= .
dx h

Elementary integration allows us to deduce that the entries of A(k) (k)


local and blocal are
given by
 
1
h1
A(k) = h ,
local 1 1
 h h
h
b(k)
local = .
h

2.8.2 Assembling the Global Matrix

We have proposed calculating the entries of the global matrix A and global vector
b that are of the form given by Eqs. (2.19) and (2.20), by looping over all elements
in the mesh, calculating the nonzero local contributions A(k) (k)
local and blocal from each
element, and using these contributions to increment the appropriate entries of A and
b, given by Tables 2.1 and 2.2.
Readers may have spotted a potential flaw with this approach for calculating A
and b. The expressions for Ai,j and bi given by Eqs. (2.19) and (2.20) are only valid
for i = 2, 3, . . . , N, with the entries for rows 1 and N +1 given by Eqs. (2.22)(2.25).
When calculating the local contributions from the element that occupies x1 x x2 ,
we evaluate contributions to both row 1 and row 2 of A and b. The entries of row 1 of A
and b do not, however, fall into the pattern given by Eqs. (2.19) and (2.20), although
the contribution to row 2 of both A and b from this element is correct. Similarly,
when calculating the local contribution from the element occupying xN x xN+1
we calculate contributions to row N + 1 of A and b that are not of the correct form.
We could avoid adding these incorrect entries into rows 1 and N + 1 by performing
a check, before adding any local contributions into the global matrix and vector, to
ensure we do not add any entries into rows 1 and N + 1. There is nothing wrong
with this approach. However, to promote clear, modular codeespecially for the
more complex problems encountered later in this bookit is easier to add the local
contributions into rows 1 and N + 1 and then to overwrite these erroneous entries
afterwards with the correct values, given by Eqs. (2.22)(2.25).
2.8 Assembling the Global Matrix 17

Having assembled the matrix A and the vector b, most of the work required to cal-
culate the finite element solution given by Eq. (2.12) has been done. All that remains
is to solve the linear system given by Eq. (2.21) to calculate U, before substituting
the entries of U into Eq. (2.12). We now summarise the steps we have taken when
applying the finite element method and then present an exemplar computational
implementation.

2.9 A Summary of the Steps Required

The aim of this chapter has been to give an overview of the finite element method
by illustrating its application to an example boundary value problem. We now give
a summary of the steps required when applying the finite element method. We will
see in later chapters that this summary may be used as a guide for the application of
the finite element method to more general differential equations.

1. Derive the weak formulation of the boundary value problem from the specified
differential equation and boundary conditions.
2. Define the finite element mesh by partitioning the domain into elements, and
specifying the nodes defined by these elements.
3. Use the nodes and elements in the mesh to define suitable basis functions.
4. Write the finite element solution as a linear sum of the basis functions. Modify the
weak formulation of the problem to determine the system of algebraic equations
that the finite element solution satisfies. These algebraic equations will fall into
one of two categories. The first category is equations that ensure that all Dirichlet
boundary conditions are satisfied. The second category is equations that arise
from substituting suitable test functions into an integral condition on the finite
element solution.
5. Assemble the algebraic equations. Begin with the second category of equations
given in step #4 above. Calculate the nonzero contributions from each element,
and use these local contributions to increment the correct entries of the global
matrix and vector. After this has been done, modify the equations to take account
of the boundary conditions.
6. Solve the system of algebraic equations.

2.10 Computational Implementation

We will now develop a practical computational implementation of the material


described in this chapter. This implementation will assemble and solve the system
of algebraic equations.
The entries of the system of algebraic equations depend on the basis functions
used, which in turn depend on the finite element mesh. Before we can assemble the
algebraic equations, we must therefore specify the finite element mesh. The mesh we
18 2 A First Example

defined in Sect. 2.4 requires only specification of the number of elements, N. This is
sufficient to define the location of the nodes x1 , x2 , . . . , xN+1 , and the element length
h. Further, these nodes allow us to specify the basis functions through the material
given in Sect. 2.5. We are then in a position to assemble the algebraic equations. We
will therefore implement the material described in this chapter by writing a function
that accepts a positive integer, N, that represents the number of elements, as an
input, and returns the vector x (containing the nodes in the mesh) and the vector U
(containing the finite element solution at each node).
A Matlab script for the implementation described above is given in Listing 2.1.
This function may be called by saving the file as Chap2_CalculateModelFem-
Solution.m and typing, in a Matlab session,
[x, U] = Chap2_CalculateModelFemSolution(40);

to run with N = 40 elements. Variable names in this script have been chosen to
correspond to the mathematical notation used in this chapter. This function begins
by initialising entries of the global matrix A and global vector b to zero in lines 4
and 5, before generating a vector containing the equally spaced nodes in the mesh in
line 8. We then loop over all elements in lines 1223, calculating the nonzero local
contributions to A and b given by Eqs. (2.30) and (2.31), before incrementing the
appropriate entries of the global linear system, given by Tables 2.1 and 2.2. Matlab
users will be aware that the expression A(k:k+1, k:k+1) that appears in line 21
is shorthand for the submatrix given by
 
Ak,k Ak,k+1
,
Ak+1,k Ak+1,k+1

and that the expression b(k:k+1) that appears in line 22 represents the subvector
given by
 
bk
.
bk+1

Note that the loop over elements introduces incorrect entries into rows 1 and N + 1 of
A and b, for the reasons explained in Sect. 2.8.2. These entries are overwritten with
the correct values in lines 2631. Note that line 26 uses Matlab notation to set all
entries of row 1 to zero. We then solve the linear system using Matlabs backslash
operator in line 34. This is a suitable method for solving linear systems of the modest
size generated here. However, iterative techniques may be more suitable for solving
the larger systems of algebraic equations that arise from the application of the finite
element method to partial differential equations in later chapters. A summary of these
linear algebra techniques is given in Appendix A. Finally, in lines 3739, we plot the
finite element solution that has been calculated, which is a linear approximation to
the solution on each element. This finite element solution may be plotted by drawing
a straight line between the finite element solution at adjacent nodes: this is exactly
what is plotted by line 37 of the listing.
2.10 Computational Implementation 19

Listing 2.1 Chap2_CalculateModelFemSolution.m, a Matlab function for cal-


culating the finite element solution of the model problem.
1 f u n c t i o n [x , U ] = C h a p 2 _ C a l c u l a t e M o d e l F e m S o l u t i o n ( N )
2

3 % I n i t i a l i s e A and b to zero
4 A = z e r o s ( N +1 , N +1);
5 b = z e r o s ( N +1 , 1);
6

7 % G e n e r a t e N +1 nodes , e q u a l l y s p a c e d b e t w e e n 0 and 1
8 x = l i n s p a c e (0 , 1 , N +1);
9

10 % Loop over e l e m e n t s c a l c u l a t i n g local c o n t r i b u t i o n s


11 % and i n c r e m e n t i n g the g l o b a l l i n e a r s y s t e m
12 for k =1: N
13 % Calculate element length
14 h = 1/ N ;
15

16 % C a l c u l a t e local c o n t r i b u t i o n s
17 A _ l o c a l = [1/ h , -1/ h ; -1/ h , 1/ h ];
18 b _ l o c a l = [ h ; h ];
19

20 % Increment global system


21 A ( k : k +1 , k : k +1) = A ( k : k +1 , k : k +1) + A _ l o c a l ;
22 b ( k : k +1) = b ( k : k +1) + b _ l o c a l ;
23 end
24

25 % Set D i r i c h l e t b o u n d a r y c o n d i t i o n s
26 A (1 ,:) = 0;
27 A (1 ,1) = 1;
28 b (1) = 0;
29 A ( N +1 ,:) = 0;
30 A ( N +1 , N +1) = 1;
31 b ( N +1) = 0;
32

33 % Solve linear system and plot FE s o l u t i o n


34 U = A\b;
35

36 % Plot f i n i t e e l e m e n t s o l u t i o n
37 plot ( x , U , - o )
38 x l a b e l ( x )
39 y l a b e l ( U )

2.11 Evaluating the Finite Element Solution at a Given


Point

We have now developed a computational implementation of the finite element method


that computes the quantities U1 , U2 , . . . , UN+1 that appear in the finite element solu-
tion given by Eq. (2.12). We will often need to evaluate the solution at some given
20 2 A First Example

point x, that is, evaluate U(x). If x is a node in the mesh, i.e. xi = x for some i, we
may then use Eq. (2.13) to write

U(x) = Ui .

If x is not a node in the mesh, then it will lie in element k for some k, where
xk < x < xk+1 . We first locate the element k that x lies in. Noting, as we did earlier in
this chapter, that only the basis functions k (x) and k+1 (x) are nonzero on element
k, the finite element solution given by Eq. (2.12) may be written, for xk < x < xk+1 ,
as

U(x) = Uk k (x) + Uk+1 k+1 (x).

We then use the definition of the basis functions k (x) and k+1 (x), given by
Eqs. (2.8)(2.10), to deduce that

xk+1 x x xk
U(x) = Uk + Uk+1 .
h h

2.12 Is the Finite Element Solution Correct?

It is always worth checking the solution to any mathematical problem, whether the
solution has been obtained by analytic pen and paper techniques or as the output
from a computer program. Easily made typographical errors, such as a missed factor
of 2, will at best lead to inaccuracies in the solution. There is, however, the potential
for completely nonsensical output, for example a physical quantity being assigned
a value that is calculated from taking the square root of a negative number. It is,
therefore, useful to perform some verification of the computation of a finite element
solution. An exhaustive verification, such as comparing the finite element solution of
a differential equation with the true solution of the equation, is rarely possible. After
all, if we knew the true solution of a differential equation, we would be unlikely to go
to the effort of computing the finite element solution. Nevertheless, some validation
is possible. Below we list a few simple tests that can usually be carried out.
The simplest test that can be carried out is to plot the finite element solution to
check that the solution looks realistic and takes values that are reasonablea
computed chemical concentration that takes a negative value, for example, should
set alarm bells ringing.
Another simple check that should be carried out is to compare finite element
solutions that have been calculated using different numbers of elements. The finite
element solution should become more accurate, and approach a limit as the number
of elements is successively increased.
Finally, analytic solutions may exist for some choices of parameter values that
appear in the differential equation, or for simplifications of the differential equa-
2.12 Is the Finite Element Solution Correct? 21

tion. The finite element solution should be computed in these cases and compared
to the analytic solution.

2.13 Exercises

For these exercises, you will need to download the Matlab script given in Listing 2.1.
Remember that the true solution to the differential equation on which this script is
based is u(x) = x(1 x).

2.1 In this exercise, we will modify the Matlab script given in Listing 2.1 so that
it returns the finite element solution at the point x = 0.45.

(a) Modify the Matlab script so that it returns the finite element solution at
x = 0.45 when this point is a node of the mesh. Use the true solution, and
a mesh with N = 100 elements, to verify that your answer is correct.
(b) Use the material given in Sect. 2.11 to modify the Matlab script to evaluate the
finite element solution at x = 0.45 when this point is not a node of the mesh.
Use the true solution, and a mesh with N = 101 elements, to verify that your
answer is correct.
(c) Combine your answers to parts (a) and (b) so that the Matlab script first deter-
mines whether or not x = 0.45 is a node of the mesh, before returning the finite
element solution at that point.

2.2 We denote the error between the finite element solution and the true solution by
E(x) = U(x) u(x). The L 2 norm of the error of the finite element solution, EL2 ,
is defined to be

 1
EL =
2 (U(x) u(x))2 dx,
0

which may be written as the sum of the contribution from individual elements:

 N 

xk+1
EL2 = (U(x) u(x))2 dx.
k=1 xk

(a) In Sect. 2.11, we saw that, for xk < x < xk+1 , the finite element solution is given
by

xk+1 x x xk
U(x) = Uk + Uk+1 .
h h
Use this expression for U(x) to explain why, for the model problem used in this
chapter, we may write
22 2 A First Example

N 

 2
xk+1
xk+1 x x xk
E2L2 = Uk + Uk+1 x(1 x) dx.
xk h h
k=1

(b) Evaluate, by direct integration, the quantity


  2
xk+1
xk+1 x x xk
Uk + Uk+1 x(1 x) dx,
xk h h

and write your answer as a function of Uk and Uk+1 .


(c) Using your answers to parts (a) and (b), modify the Matlab script given in
Listing 2.1 so that it outputs the value of EL2 for a given number of elements, N.
(d) Define the element length by h = N1 where N is the number of elements. You
may assume that, as h 0, EL2 is related to the element length h by

EL2 = Chp ,

for some constants C and p. Taking the logarithm of both sides of this expression
gives

log (EL2 ) = log C + p log h,

and so plotting log (EL2 ) against log h will give a straight line of gradient p
as h 0. By plotting log (EL2 ) against log h for several values of h, estimate
the constant p.

2.3 In Sect. 2.11, we saw that, for xk < x < xk+1 , the finite element solution is given
by

xk+1 x x xk
U(x) = Uk + Uk+1 .
h h

(a) Write down the derivative of the finite element solution, dU


dx
, on element k.
(b) Modify the Matlab script given in Listing 2.1 so that it plots the derivative of
the solution. Note that this derivative is a function that may be discontinuous
across element boundaries.

2.4 A boundary value problem is defined by

d2 u
3 = 2, 0 < x < 1,
dx 2
together with boundary conditions

u(0) = 1, u(1) = 2.
2.13 Exercises 23

(a) Write down the analytic solution to this boundary value problem.
(b) By working through the steps required to calculate the finite element solution,
summarised in Sect. 2.9, modify the Matlab script given in Listing 2.1 to cal-
culate the finite element approximation of the boundary value problem above.
Compare your finite element solution to the true solution.
Chapter 3
Linear Boundary Value Problems

In the previous chapter, we demonstrated how to calculate the finite element solution
of a very simple differential equation. The intention of this simple example was to
give the reader an overview of the steps required when applying the finite element
method. As a consequence, for the sake of clarity, some mathematical rigour was
neglected. In this chapter, we introduce more rigour into our description of the finite
element method and demonstrate how to calculate the finite element solution of
general linear, second-order, boundary value problems.

3.1 A General Boundary Value Problem

A general linear, second-order, boundary value problem, defined on the interval


a < x < b, may be written in the form
 
d du du
p(x) + q(x) + r (x)u = f (x), (3.1)
dx dx dx

for given functions p(x), q(x), r (x) and f (x), where the function p(x) satisfies the
condition that p(x) = 0 for a < x < b. A differential equation for u(x) is described
as linear provided the differential equation is linear in u(x) and its derivatives. The
equation above will then still classified as a linear differential equation even if the
functions p(x), q(x), r (x) and f (x) are nonlinear functions of x.
To fully specify the boundary value problem, we require one boundary condition
at x = a and one boundary condition at x = b. At this point, we consider only two
types of boundary conditions: Dirichlet boundary conditions, where the value of the
function u(x) is specified on the boundary, and Neumann boundary conditions, where
the derivative of the function u(x) is specified on the boundary. We will consider
more general boundary conditions in Sect. 3.8. For the purpose of illustrating the
application of the finite element method, we will specify one Dirichlet boundary
condition and one Neumann boundary condition. This will allow us to demonstrate
Springer International Publishing AG 2017 25
J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_3
26 3 Linear Boundary Value Problems

how both categories of boundary condition are handled by the finite element method.
It should then be clear how to adapt the finite element method for any suitable
combination of these boundary conditions. We impose a Dirichlet boundary condition
at x = a:

u = ua , x = a, (3.2)

for some given constant u a and a Neumann boundary condition at x = b:

du
p(x) = b , x = b, (3.3)
dx

where b is another given constant. Note the presence of p(x) in both Eqs. (3.1) and
(3.3): the left-hand side of Eq. (3.3) has the same pattern as the contents of the brackets
on the left-hand side of Eq. (3.1). A Neumann boundary condition written in this
form is known as a natural boundary condition. The reason for this terminology will
become clear shortly when we derive the weak solution of the differential equation
given by Eq. (3.1), subject to the boundary conditions given by Eqs. (3.2) and (3.3).
We have written the Neumann boundary condition, Eq. (3.3), as a natural Neu-
mann boundary condition. It is straightforward to write other Neumann boundary
conditions, such as

du
= b , x = b, (3.4)
dx
as natural Neumann boundary conditions; we simply multiply the boundary condition
given by Eq. (3.4) by p(x), which we assumed earlier was nonzero. Writing b =
p(b)b then yields a natural Neumann boundary condition in the form of Eq. (3.3).

3.1.1 A Note on the Existence and Uniqueness of Solutions

It is important to be aware that there is no guarantee that a solution exists to a general


boundary value problem. Furthermore, if a solution does exist, then it may not be
unique. For example, suppose we attempt to solve the differential equation defined
by, for 0 < x < :

d2 u
+ u = 0, (3.5)
dx 2
subject to the boundary conditions

u(0) = 0, u( ) = 1. (3.6)
3.1 A General Boundary Value Problem 27

The general solution of Eq. (3.5) is u(x) = K 1 sin x + K 2 cos x, for constants K 1
and K 2 that we would expect to determine from the boundary conditions given by
Eq. (3.6). However, it is not possible to find values of K 1 and K 2 that satisfy both of
these boundary conditions, and we deduce that no solution exists to this boundary
value problem.
Suppose, instead, that the boundary conditions are given by

u(0) = 0, u( ) = 0. (3.7)

The solution of Eq. (3.5), subject to the boundary conditions given by Eq. (3.7),
is u(x) = K 1 sin x for any constant K 1 . Hence, the solution is not unique. A full
discussion of the existence and uniqueness of solutions to boundary value problems is
beyond the scope of this book. Throughout this book, you may assume, unless stated
otherwise, that a unique solution exists to any differential equation considered.

3.2 The Weak Formulation

The discussion of the weak formulation of a boundary value problem in Sect. 2.3,
although entirely adequate for the purposes of Chap. 2, lacked sufficient detail to
allow the reader to apply the finite element method to a general boundary value
problem such as that presented in Sect. 3.1. In particular, the model problem con-
sidered in Chap. 2 included only the very simple Dirichlet boundary conditions that
u(x) = 0 on both boundaries. As promised, we now provide more detail, allow-
ing more general boundary conditions to be handled. Our treatment of the weak
formulation will also be more rigorous than that presented in Chap. 2.

3.2.1 Some Definitions

The weak formulation of the example differential equation derived in Sect. 2.3
assumed the existence of several derivatives and integrals. We now provide some
notation that aids us in deriving the weak formulation of differential equations in
a more rigorous manner. Suppose that a function v(x) is defined on the interval
a x b. We say that v(x) is square integrable on this interval if the integral
 b
(v(x))2 dx
a

exists: in practice, this integral is said to exist if it takes a finite value. We define
L 2 (a, b) to be a set of all functions v(x) that are square integrable on the interval
a x b and write L 2 (a, b) in set notation as
28 3 Linear Boundary Value Problems
  b 
L (a, b) = functions v(x) such that the integral
2
(v(x)) dx exists .
2
(3.8)
a

We now define the Sobolev space of order 1, denoted by H 1 (a, b), to be the set of
functions v(x) that: (i) are defined and continuous on the interval a x b; (ii)
are square integrable on a x b; and (iii) possess a first derivative that is square
integrable on a x b. In set notation,

H (a, b) =
1
functions v(x) such that v(x) is continuous, and the integrals
 b  b  2 
dv
(v(x))2 dx and dx exist . (3.9)
a a dx

Suppose that a differential equation, together with suitable boundary conditions, has
been specified. Using these boundary conditions, we specify two subsets of H 1 (a, b)
that will be required later:

HE1 (a, b) = functions v(x) in H 1 (a, b) such that v(x) satisfies all Dirichlet

boundary conditions , (3.10)

H01 (a, b) = functions v(x) in H 1 (a, b) such that v(x) = 0 at all points x where

Dirichlet boundary conditions are specified . (3.11)

The use of these sets of functions when deriving the weak formulation of a differential
equation ensures the existence of all integrals that are used.

3.2.2 Deriving the Weak Formulation

The definitions of the Sobolev space of order 1, and subsets of this space, laid out
in Eqs. (3.9)(3.11), may be used to aid the derivation of the weak formulation of
the differential equation given by Eq. (3.1), subject to the boundary conditions given
by Eqs. (3.2) and (3.3). We begin by multiplying the differential equation, Eq. (3.1),
by any test function v(x) H01 (a, b), and integrating the resulting product over the
interval a < x < b:
 b    b
d du du
p v + q v + r uv dx = f v dx.
a dx dx dx a
3.2 The Weak Formulation 29

We now integrate the first term on the left-hand side by parts to give
   b  b
du b du dv du
p v + p + q v + r uv dx = f v dx. (3.12)
dx a a dx dx dx a

We have chosen v(x) H01 (a, b) and so, by the definition of this set given in
Eq. (3.11), we have v(x) = 0 at any value of x where a Dirichlet boundary condition
is applied. As a Dirichlet boundary condition, given by Eq. (3.2), is applied at x = a,
we have v(a) = 0. Substituting both this Dirichlet boundary condition, and the
Neumann boundary condition given by Eq. (3.3) into Eq. (3.12) gives, after a little
rearrangement:
 b  b
du dv du
p + q v + r uv dx = f v dx + b v(b). (3.13)
a dx dx dx a

Note that writing the Neumann boundary condition, Eq. (3.3), as a natural boundary
condition allows the contribution to Eq. (3.13) from the Neumann boundary condition
to be independent of the function p(x).
In deriving Eq. (3.13), we have used the differential equation, Eq. (3.1), and the
Neumann boundary condition, Eq. (3.3). We have not yet used the Dirichlet bound-
ary condition given by Eq. (3.2), other than to specify the set H01 (a, b). We satisfy
the Dirichlet boundary condition by demanding that the weak solution lies in the
set HE1 (a, b) defined by Eq. (3.10), as all functions in this set satisfy this Dirichlet
boundary condition.
Using Eq. (3.13), we may now define the weak solution of Eqs. (3.1)(3.3) by:

find u(x) HE1 (a, b) such that


 b  b
du dv du
p + q v + r uv dx = f v dx + b v(b), (3.14)
a dx dx dx a

for all v(x) H01 (a, b).

The derivation above has illustrated one key feature of the weak formulation of a
differential equation. Dirichlet boundary conditions are satisfied through specifying
the set in which u(x) lies, i.e. the set HE1 (a, b). Neumann boundary conditions
are handled through incorporating these boundary conditions into Eq. (3.14), the
equation that is satisfied by the weak solution for all suitable test functions. We will
see in the next section that this remark also applies to the finite element solution.
30 3 Linear Boundary Value Problems

3.3 The Finite Element Solution

We now proceed as in Chap. 2 by specifying the computational mesh and writing


the finite element solution as a linear sum of basis functions defined on this mesh.
We then use a modification of the weak formulation of the differential equation to
identify the system of algebraic equations that the coefficients of the basis functions
in this linear sum satisfy.

3.3.1 The Mesh

In Chap. 2, we used a mesh with equally spaced nodes, often known as a uniform
mesh, when computing the finite element solution. A consequence of using equally
spaced nodes is that all elements are the same size. We now relax this condition
and allow computational meshes where the nodes are not equally spaced across the
computational domain a < x < b.
As before, the computational mesh will consist of N elements and N + 1 nodes.
The nodes are again denoted by x1 , x2 , . . . , x N +1 . We demand that these nodes satisfy
three conditions: (i) x1 = a, (ii) x N +1 = b and (iii) xk+1 > xk for k = 1, 2, . . . , N .
Defining element k to occupy the region xk x xk+1 , we see that nodes
satisfying the conditions above generate elements that cover the whole of the domain
a < x < b without overlapping, and all have positive length. The mesh containing
these nodes and elements is therefore a suitable partitioning of the domain.

3.3.2 Basis Functions

We will now define basis functions that allow us to calculate a finite element solution
that is a linear approximation to the solution of a differential equation on each ele-
ment. These basis functions will be defined using two different formulations, which
we will show are equivalent. The first formulation defines the basis functions by
modifying the basis functions used in Chap. 2, so that they may be applied on the
nonuniform mesh described in Sect. 3.3.1. We then present an alternative formula-
tion for defining these basis functions. Both methods for defining the basis functions
generate identical basis functions. We will see, however, that the second technique
that we present is more flexible. Later in this chapter, we will see that the linear sys-
tem that arises from the implementation of the finite element method includes some
entries that are defined by integrals that are difficult, or impossible, to evaluate ana-
lytically. The second method for defining basis functions will aid us in approximating
these integrals using numerical techniques. Further, we will see in later chapters that
the second method for defining basis functions allows the material presented in this
chapter to be extended, relatively easily, to both higher order basis functions and the
3.3 The Finite Element Solution 31

application of the finite element method to partial differential equations. We now


describe both of these formulations.

3.3.2.1 Generalising the Basis Functions Used in Chap. 2

The basis functions defined by Eqs. (2.8)(2.10) assume that each element is of a
constant length h. Noting that element k is of length xk+1 xk , for k = 1, 2, . . . , N ,
it is straightforward to modify these basis functions for use on the nonuniform
mesh described in Sect. 3.3.1, by replacing the constant h in Eqs. (2.8)(2.10) by
the appropriate element length. We may then define the basis functions j (x), for
j = 1, 2, . . . , N + 1, on the nonuniform mesh described in Sect. 3.3.1 by
x2 x
x2 x1
, x1 x x2 ,
1 (x) = (3.15)
0, otherwise,
xx


j1
, x j1 x x j ,
x j x j1
x j+1 x
j (x) = x x , x j x x j+1 , j = 2, 3, . . . , N , (3.16)

j+1 j

0, otherwise,
xx
N
, x N x x N +1 ,
N +1 (x) = x N +1 x N (3.17)
0, otherwise.

Should each element have the same length h, the basis functions defined above are
identical to the basis functions defined by Eqs. (2.8)(2.10).
When deriving the basis functions in Sect. 2.5, we noted that the basis functions
had three useful properties: (i) they are linear functions on each element; (ii) they are
continuous functions; and (iii) they satisfy, for j = 1, 2, . . . , N + 1, the condition

1, i = j,
j (xi ) = (3.18)
0, i = j.

It is straightforward to verify that these properties are also true for the basis functions
defined above. Writing the finite element solution as

N +1

U (x) = U j j (x), (3.19)
j=1

where j (x), j = 1, 2, . . . , N + 1, are defined by Eqs. (3.15)(3.17), we see that,


as required, our finite element solution is a linear function on each element that is
continuous across element boundaries. Furthermore, Eqs. (3.18) and (3.19) allow us
to write
32 3 Linear Boundary Value Problems

U (xi ) = Ui , i = 1, 2, . . . , N + 1, (3.20)

and so Ui is, as before, the finite element solution at the node where x = xi .

3.3.2.2 A More General Method for Defining the Basis Functions

As promised earlier, we will now present a more general definition of the basis
functions. The global basis functions are now defined through: (i) a mapping between
a general element in the mesh and a canonical element, and (ii) local basis functions
defined on the canonical element.
We may map between element k of the mesh described in Sect. 3.3.1, and the unit
interval 0 X 1, by using the change of variable

x = xk + (xk+1 xk )X. (3.21)

The unit interval 0 X 1 is known as the canonical element. We now define local
basis functions local,1 (X ), local,2 (X ) on the canonical element by

local,1 (X ) = 1 X, 0 X 1, (3.22)
local,2 (X ) = X, 0 X 1. (3.23)

Global basis functions j , where j = 1, 2, . . . , N + 1, may then be defined on


element k, in terms of the change of variable given by Eq. (3.21), and the local basis
functions local,1 (X ), local,2 (X ) defined by Eqs. (3.22) and (3.23). On element k, we
set

k (X ) = local,1 (X ), 0 X 1, (3.24)
k+1 (X ) = local,2 (X ), 0 X 1, (3.25)
j (X ) = 0, 0 X 1, j = k, j = k + 1. (3.26)

Equations (3.21)(3.26) then define the global basis functions j , for j = 1, 2, . . . ,


N + 1, for every element k = 1, 2, . . . , N .
We now demonstrate that the basis functions defined by Eqs. (3.21)(3.26) are
identical to those defined by Eqs. (3.15)(3.17). Using the change of variable given
by Eq. (3.21), and the definition of the local basis functions given by Eqs. (3.22) and
(3.23), we may write local,1 , local,2 as functions of x on element k:

(k) xk+1 x
local,1 (x) = , xk x xk+1 , (3.27)
xk+1 xk
(k) x xk
local,2 (x) = , xk x xk+1 . (3.28)
xk+1 xk
3.3 The Finite Element Solution 33

(k) (k)
We introduce the superscript (k) to the functions local,1 (x), local,2 (x) when writing
these functions as a function of x, to emphasise that they depend on the element
through the change of variable given by Eq. (3.21). We place this superscript in
brackets to avoid any confusion with these functions being raised to the power k.
We may now use Eqs. (3.27) and (3.28) to write the global basis functions given
by Eqs. (3.24)(3.26) as functions of x on element k:

xk+1 x
k (x) = , xk x xk+1 , (3.29)
xk+1 xk
x xk
k+1 (x) = , xk x xk+1 , (3.30)
xk+1 xk
j (x) = 0, xk x xk+1 , j = k, j = k + 1. (3.31)

These basis functions are plotted as a function of x on element k in Fig. 3.1.


We see that the basis functions defined on any element k by Eqs. (3.21)(3.26),
and plotted in Fig. 3.1, are identical to the basis functions defined by Eqs. (3.15)
(3.17) when restricted to element k. This is true for any element k, and so the basis
functions defined by Eqs. (3.21)(3.26) are identical to those defined by Eqs. (3.15)
(3.17). Properties of basis functions derived in Sect. 3.3.2.1, such as those given by
Eqs. (3.18) and (3.20), therefore hold for the basis functions defined in this section.

1
(k)
lo cal,1(x) = k (x)

(k)
lo cal,2(x) = k+1 (x)

0
xk xk+1
x

Fig. 3.1 The basis functions k (x) and k+1 (x) defined by Eqs. (3.29) and (3.30)
34 3 Linear Boundary Value Problems

3.3.3 Sets of Functions

We now identify some sets of potential finite element solutions that mirror the Sobolev
spaces that were used when deriving the weak formulation in Sect. 3.2. First, we
specify the set of all potential finite element solutions that do not take account of any
boundary conditions, i.e. the set of continuous functions, that are linear functions
when restricted to a given element. This set, denoted by S h , is defined by

 
N +1
Sh = functions V (x) given by V (x) = V j j (x) where j , j = 1, 2, . . . , N + 1,
j=1

are suitably defined basis functions . (3.32)

Subsets of S h are defined as follows:



S E = functions V (x) S h such thatV (x) satisfies all Dirichlet boundary
h


conditions , (3.33)

S0h = functions V (x) S h such that V (x) = 0 at all points x where Dirichlet

boundary conditions are specified . (3.34)

3.3.4 The Finite Element Solution

Having defined the set S h , and the subsets S Eh and S0h of this set, in Sect. 3.3.3, we
are now in a position to define the finite element solution. As in Chap. 2, this is done
by modifying the definition of the weak solution, given by Eq. (3.14). The sets of
functions HE1 (0, 1) and H01 (0, 1) that appear in the weak solution are replaced by the
sets S Eh and S0h , allowing us to specify the finite element solution by:

find U (x) S Eh such that


 b  b
dU di dU
p +q i + rU i dx = f i dx + b i (b), (3.35)
a dx dx dx a

for all i (x) S0h .


3.3 The Finite Element Solution 35

3.3.5 The System of Algebraic Equations

We may derive the system of algebraic equations satisfied by U1 , U2 , . . . , U N +1 in


a similar way to that described in Sect. 2.7. We first write the finite element solution
as
N +1

U (x) = U j j (x), (3.36)
j=1

where j (x), j = 1, 2, . . . , N + 1, are suitably defined basis functions. Earlier,


in Sects. 3.3.2.1 and 3.3.2.2, we gave two definitions of basis functions that we
demonstrated are identical. As such, for the purposes of this section, either definition
suffices. We note that U (x) is a member of the set S h .
The finite element solution given by Eq. (3.36) contains N + 1 unknown values,
U1 , U2 , . . . , U N +1 . We therefore need N + 1 algebraic equations to determine these
unknown values. As in Chap. 2, these equations will be drawn from two sources: equa-
tions that ensure that U (x) satisfies the Dirichlet boundary conditions and equations
that arise from substituting suitable basis functions i (x) into the integral condition
given by Eq. (3.35).
The finite element solution, defined by Eq. (3.35), requires that U (x) S Eh . We
have already noted that U (x), given by Eq. (3.36), lies in the set S h . To ensure that
U (x) lies in the subset S Eh of the set S h , we need U (x) to satisfy any prescribed
Dirichlet boundary conditions. We have one Dirichlet boundary condition, given by
Eq. (3.2), and this boundary condition applies at x = a: that is, at the node where
x = x1 . Applying Eq. (3.20) at the this node gives

U (x1 ) = U1 , (3.37)

and so this Dirichlet boundary condition is satisfied if

U1 = u a . (3.38)

The condition that U (x) satisfies the Dirichlet boundary condition has allowed
us to derive one algebraic equation, Eq. (3.38). We therefore require a further N
algebraic equations to determine the N +1 unknown values. Using Eq. (3.18), we see
that i (x1 ) = 0 for i = 2, 3, . . . , N + 1. Hence, i (x) S0h for i = 2, 3, . . . , N + 1,
as these functions take the value zero at the value of x where Dirichlet boundary
conditions are applied. Substituting i (x), for i = 2, 3, . . . , N + 1, into Eq. (3.35)
will then generate a further N algebraic equations, as required. Noting that Eq. (3.36)
implies that

N +1

dU d j
= Uj , (3.39)
dx j=1
dx
36 3 Linear Boundary Value Problems

we have, on substituting i (x), i = 2, 3, . . . , N + 1, into Eq. (3.35) and rearranging,


the N algebraic equations

N +1 
 b   b
d j di d j
p +q i + r j i dx U j = f i dx + b i (b),
j=1 a dx dx dx a

(3.40)

for i = 2, 3, . . . , N + 1.
We may combine Eqs. (3.38) and (3.40) into the linear system

AU = b, (3.41)

where for j = 1, 2, . . . , N + 1,

1, j = 1,
A1, j = (3.42)
0, j = 1,
 b
di d j d j
Ai, j = p + qi + r i j dx, i = 2, 3, . . . , N + 1,
a dx dx dx
(3.43)

and

b1 = u a , (3.44)
 b
bi = f i dx + b i (b), i = 2, 3, . . . , N + 1. (3.45)
a

Appealing to Eq. (3.18), it is clear that



1, i = N + 1,
i (b) = i (x N +1 ) =
0, i = N + 1,

and so this term only affects the equation generated with i = N + 1. We may now
write Eqs. (3.44) and (3.45), the entries of b, as

b1 = u a , (3.46)
 b
bi = f i dx, i = 2, 3, . . . , N , (3.47)
a
 b
b N +1 = f N +1 dx + b . (3.48)
a

We now discuss how to assemble the algebraic equations given by Eq. (3.41), with
a particular focus on handling the integration that is required.
3.4 Assembling the Algebraic Equations 37

3.4 Assembling the Algebraic Equations

We assemble the entries of the global matrix A and the global vector b that appear in
the linear system Eq. (3.41) using the same strategy as in Sect. 2.8. We first evaluate
those entries that are defined by integrals, namely Eqs. (3.43), (3.47) and (3.48), by
decomposing these integrals into the sum of integrals over the elements in the mesh.
Remembering that element k occupies xk x xk+1 , we may write Eqs. (3.43),
(3.47) and (3.48) as follows:

N 
 xk+1
di d j d j
Ai, j = p + qi + r i j dx,
k=1 xk dx dx dx
i = 2, 3, . . . , N + 1, j = 1, 2, . . . , N + 1, (3.49)
N  xk+1

bi = f i dx, i = 2, 3, . . . , N , (3.50)
k=1 xk

N 
 xk+1
b N +1 = f N +1 dx + b . (3.51)
k=1 xk

We then complete the assembly of the linear system by setting the entries that are
defined explicitly by Eqs. (3.42) and (3.46). We now explain this process in more
detail.

3.4.1 The Contributions from Integrals

We see from Eq. (3.49) that the contribution to entry Ai, j of the global matrix A, from
integration over element k, is given by, for i = 2, 3, . . . , N +1, j = 1, 2, . . . , N +1:
 xk+1
di d j d j
p + qi + r i j dx.
xk dx dx dx

In Sect. 2.8, we noted that the only basis functions that are nonzero on element k
are k (x) and k+1 (x). The integral above will therefore be zero unless both i and
j take the values k or k + 1. Consequently, the integral above generates nonzero
contributions only to the entries Ak,k , Ak,k+1 , Ak+1,k , Ak+1,k+1 of the global matrix
(k)
A. These nonzero contributions may be stored in the local matrix Alocal , of size 2 2,
(k) (k) (k)
where entry Alocal,1,1 contributes to Ak,k ; Alocal,1,2 contributes to Ak,k+1 ; Alocal,2,1
(k) (k)
contributes to Ak+1,k ; and Alocal,2,2 contributes to Ak+1,k+1 . The entries of Alocal are
given by:
38 3 Linear Boundary Value Problems
 xk+1
(k) dk dk dk
Alocal, 1,1 = p + qk + r k k dx, (3.52)
x dx dx dx
 kxk+1
(k) dk dk+1 dk+1
Alocal, 1,2 = p + qk + r k k+1 dx, (3.53)
x dx dx dx
 kxk+1
(k) dk+1 dk dk
Alocal, 2,1 = p + qk+1 + r k+1 k dx, (3.54)
x dx dx dx
 kxk+1
(k) dk+1 dk+1 dk+1
Alocal, 2,2 = p + qk+1 + r k+1 k+1 dx. (3.55)
xk dx dx dx

Similarly, using Eqs. (3.50) and (3.51), the contribution to the entry bi , i =
2, 3, . . . , N + 1, of the global vector b from integration over element k is given by
 xk+1
f i dx.
xk

Using the same argument as above, we see that this integral generates nonzero contri-
butions only to the entries bk and bk+1 of the global vector b. We store these nonzero
(k) (k)
contributions in a local vector blocal with two entries. Entry blocal,1 of this local vector
(k)
contributes to entry bk of the global vector b, and entry blocal,2 contributes to entry
(k)
bk+1 of b. The entries of blocal are given by:
 xk+1
(k)
blocal, 1 = f k dx, (3.56)
x
 kxk+1
(k)
blocal, 2 = f k+1 dx. (3.57)
xk

We may therefore compute the integrals that appear in Eqs. (3.43), (3.47) and
(3.48) by looping over all elements, calculating only the nonzero local contributions
(k) (k)
Alocal and blocal from each element, before using these local contributions to increment
the appropriate entries of A and b. We then complete the assembly of Eqs. (3.43),
(3.47) and (3.48) by adding the extra algebraic term that appears in Eq. (3.48) to entry
b N +1 of b. However, before we may use this technique for assembling these entries
of A and b, we must first be able to evaluate the integrals that define the entries of
(k) (k)
Alocal and blocal on each element.
For some functions p(x), q(x), r (x) and f (x), the integrals given by Eqs. (3.52)
(3.57) may be evaluated analytically. This was the case for the simple example
differential equation in Chap. 2, where p(x) = 1, q(x) = 0, r (x) = 0 and f (x) = 2.
For more general functions, it may not be possible to evaluate these integrals ana-
lytically, and we must approximate these integrals numerically. This is one instance
where the specification of the global basis functions through local basis functions on
the canonical element 0 X 1, described in Sect. 3.3.2.2, is useful, as we now
describe.
3.4 Assembling the Algebraic Equations 39

We will see, in Sect. 3.5, that when making a numerical approximation to the
integrals above, it is convenient to use the change of variable, given by Eq. (3.21),
that maps between element k and the canonical element 0 X 1. Writing

h = xk+1 xk , k = 1, 2, . . . , N ,

we may write the change of variable given by Eq. (3.21) as

x = xk + h X, 0 X 1. (3.58)
(k) (k)
We now use this change of variable to write the entries of Alocal and blocal , given by
Eqs. (3.52)(3.57), as integrals over the canonical element. On using Eq. (3.24) and
(3.25), we may write k and k+1 in terms of the local basis functions defined by
Eqs. (3.22) and (3.23). Noting that

dlocal, i 1 dlocal, i
= , i = 1, 2,
dx h dX
(k) (k)
we may write the entries of Alocal and blocal as
 1
(k) 1 dlocal, i dlocal, j
Alocal, i, j = h 2
p(xk + h X ) +
0 h dX dX
1 dlocal, j
q(xk + h X )local, i (X ) +
h dX
r (xk + h X )local, i (X )local, j (X ) dX, i, j = 1, 2, (3.59)
 1
(k)
blocal, i =h f (xk + h X )local, i (X ) dX, i = 1, 2. (3.60)
0

We will explain in Sect. 3.5 how these integrals may be approximated numerically.

3.4.2 Completing the Assembly of the Linear System

We noted in Sect. 2.8.2 that when looping over elements to calculate the contributions
to A and b that arise from integrals, we introduce incorrect entries into the rows where
the entries are given by Eqs. (3.42) and (3.46). We complete the assembly of the linear
system by overwriting these incorrect entries and assigning the correct entries defined
explicitly by Eqs. (3.42) and (3.46).
40 3 Linear Boundary Value Problems

3.5 Approximating Integrals Using Numerical Quadrature

The integrals given by Eqs. (3.59) and (3.60) cannot be evaluated explicitly for general
functions p(x), q(x), r (x), f (x). Even if the integrals can be evaluated analytically,
the integration may be tedious and time-consuming. Furthermore, if these functions
were altered, we would then need to evaluate these integrals again using the new
functions. Instead, we advise allocating this task to the computer, especially as a
simple, flexible technique exists for approximating these integrals, namely numerical
quadrature.
Suppose we wish to evaluate the integral
 1
g(X ) dX,
0

which is of the form given by Eqs. (3.59) and (3.60). When using numerical quadra-
ture, the function g(X ) is evaluated at a collection of M points X 1 , X 2 , . . . , X M in
the interval 0 X 1, and the approximation to the integral is given by a weighted
sum of these function evaluations:
 1 
M
g(X ) dX wm g(X m ), (3.61)
0 m=1

where wm , m = 1, 2, . . . , M, are known as the quadrature weights or simply the


weights, and X m , m = 1, 2, . . . , M, are known as the quadrature points.
Throughout this book, we shall assume that any integral that has been evaluated
using quadrature has been approximated sufficiently accurately that it may be con-
sidered to be the true value of this integral. As such, in a slight abuse of mathematical
notation, we shall use an equals sign, rather than the approximately equals sign that
has been used in Eq. (3.61), when writing these integrals. Computational implemen-
tations will, by necessity, use an equals sign for an expression such as Eq. (3.61),
and so the use of an equals sign in the text will aid clarity when presenting these
implementations.
When designing a quadrature rule with M quadrature points, there are 2M quan-
tities that can be chosen: M weights wm and M quadrature points X m . A polynomial
of degree 2M 1 is specified by the 2M coefficients of X p , p = 0, 1, 2, . . . , 2M 1.
Gaussian quadrature, one variety of quadrature, assigns the M weights and M quadra-
ture points by insisting that Gaussian quadrature is exact for polynomials of degree
2M 1. It may be shown that for any value of M: (i) the Gaussian weights and
quadrature points exist; (ii) the weights all take positive values; and (iii) the quadra-
ture points all lie in the interval 0 < X < 1. Weights and quadrature points for
M = 1, 2, 3, 4 are given in Table 3.1. As would be expected, Gaussian quadrature
becomes a more accurate approximation of the true value of the integral when more
quadrature points are used.
3.5 Approximating Integrals Using Numerical Quadrature 41

Table 3.1 Gaussian quadrature points and weights for M = 1, 2, 3, 4 quadrature points
M =1 M =2 M =3 M =4
X m wm X m wm X m wm X m wm
    
     
2 1 2 1 2 1 7 + 7 5 72 18 30
1 1 1 1 1 3 5 1 3 2 6 1
2 1 2 5 18
3
  
    
1
2 1+ 1 1
2
1
2
4
9
1
2 1 3
7 2
7
6
5
1
72 18 + 30
3

   
    
1
2 1 + 35 5
18
1
2 1+ 3
7 2
7
6
5
1
72 18 + 30
  
  
1
2 1+ 3
7 + 2
7
6
5
1
72 18 30

The quadrature approximation given by Eq. (3.61) may now be used to approxi-
mate the integrals given by Eqs. (3.59) and (3.60) as follows: for i = 1, 2, j = 1, 2:

  (k)  (k) 
M
1 dlocal,i  dlocal, j 
(k) 
Alocal, i, j = h wm p(x + h X ) +
dX  X =X m dX  X =X m
k m
m=1
h2
(k) 
1 (k) dlocal, j 
q(xk + h X M )local,i (X M ) +
h dX  X =X m

(k) (k)
r (xk + h X M )local,i (X M )local, j (X M ) , (3.62)

and for i = 1, 2:

(k)

M
(k)
blocal, i = h wm f (xk + h X M )local,i (X M ), i = 1, 2. (3.63)
m=1

3.6 The Steps Required

In Chap. 2, we explained how to calculate the finite element solution of the simplest
possible boundary value problem. A summary of the tasks required when calculating
the finite element solution of this simple example was given in Sect. 2.9. In this
chapter, we have developed the material presented in Chap. 2 to allow calculation of
the finite element solution of a very general second-order boundary value problem. It
is worth revisiting the summary of tasks given in Sect. 2.9 at this point. We see that,
even for more general problems, this summary still forms a perfectly valid summary.
42 3 Linear Boundary Value Problems

3.7 Computational Implementation

We demonstrate application of the material presented in this chapter using a model


boundary value problem defined by
 
d du du
x (2x + 3) + (x + 2)u = x 3 e2x , 0.5 < x < 1, (3.64)
dx dx dx

together with a Dirichlet boundary condition at x = 0.5:

u = e, x = 0.5, (3.65)

and a natural Neumann boundary condition at x = 1:

du
x = 5e2 , x = 1. (3.66)
dx
The boundary value problem above has solution
   
u(x) = ex K 1 + K 2 x 3 + e2x x 2 2x + 2 ,

where
 
32 e
K 1 = 3e 3e + ,
31 4
 
8 e
K2 = 3e + .
31 4

The boundary value problem above may be related to the general differential equation
and boundary conditions given by Eq. (3.1)(3.3) by setting

p(x) = x,
q(x) = (2x + 3),
r (x) = x + 2,
f (x) = x 3 e2x ,
u a = e, (3.67)
b = 5e2 . (3.68)

A Matlab function for implementing the finite element method to solve the
boundary value problem defined by Eqs. (3.64)(3.66) is given in Listing 3.1. This
function follows the outline for calculating a finite element solution given in Sect. 2.9,
3.7 Computational Implementation 43

and variable names follow the mathematical notation used in Chaps. 2 and 3. One
difference to the implementation given in Sect. 2.9 is the input required to the Matlab
function. In this chapter, we have not assumed that the nodes are equally spaced, and
specifying only the number of elements N is no longer sufficient to define the mesh
and basis functions. Instead, the vector of nodes used in the mesh is required as
input. If N elements are used, this vector will contain N + 1 nodes. The vector of
nodes supplied as input to the function is assumed to satisfy the conditions on the
mesh set out in Sect. 3.3.1, that is (i) x1 = a, (ii) x N +1 = b and (iii) xk+1 > xk for
k = 1, 2, . . . , N . The function returns the vector U that specifies the finite element
solution at each node.
We now give a commentary on how the Matlab function in Listing 3.1 imple-
ments the material presented in this chapter. After taking a suitable vector of nodes,
x, we then define the functions p(x), q(x), r (x), f (x), the value of the Dirichlet
boundary condition, u a , and the value of the Neumann boundary condition, b , in
lines 49 of the listing. The number of nodes in the mesh is equal to length of the
vector x. The number of elements, N , will be one less than the number of nodes:
the value of N is set in line 12. After initialising all entries of A and b to zero in
lines 15 and 16, we then loop over the elements in lines 2026 calculating the local
contributions to A and b from each element, before incrementing the global matrix
A and global vector b appropriately. The local contributions on each element are
calculated using the function CalculateContributionOnElement which is
defined in lines 4589 of the listing. In this function, we first deduce the element
length, h, in line 49. We define the quadrature scheme we are usingthat is, the
number of quadrature points M, the weights and the quadrature pointsin lines 52
(k) (k)
54. We initialise the local contributions to Alocal and blocal to zero in lines 5758 and
(k) (k)
then use this quadrature scheme to calculate the entries of Alocal and blocal , given by
Eqs. (3.62) and (3.63), by looping over the quadrature points in lines 6189. Inside
this loop, we first evaluate the local basis functions, local,1 , local,2 , the derivatives
of the local basis functions and the functions p(x), q(x), r (x), f (x) at each quadra-
(k) (k)
ture point, before using these values to increment the entries of Alocal and blocal by
weighting the contributions according to the quadrature rule.
Returning to the main body of code, the contribution from the Neumann boundary
condition at x = 1 is added into b N +1 in line 30 using the value of b given by
Eq. (3.68), before the Dirichlet boundary condition at x = 0.5 is imposed in lines 33
35 using the value of u a given by Eq. (3.67). Finally, the linear system is solved in
line 38, before the finite element solution is plotted in lines 3941.
This Matlab function may be called by typing, for example,
U = Chap3_CalculateFemForBvp(linspace(0.5, 1, 101));

into a Matlab session to use a computational mesh with 101 nodes (and, therefore,
100 elements) with the nodes equally spaced between 0.5 and 1.
44 3 Linear Boundary Value Problems

Listing 3.1 Chap3_CalculateFemForBvp.m, a Matlab function for calculating the


 element solution of a boundary value problem of the form given in Sect. 3.1.
finite
1 function U = Chap3_CalculateFemForBvp (x)
2
3 % d e f i n e p ( x ) , q ( x ) , r ( x ) , f ( x ) , u_a , eta_b
4 p = @ ( x ) -x ;
5 q = @ ( x ) -(2* x +3);
6 r = @ ( x ) x +2;
7 f = @ ( x ) x ^3* exp (2* x );
8 u_a = exp (1);
9 e t a _ b = -5* exp (1)* exp (1);
10
11 % D e d u c e n u m b e r of e l e m e n t s
12 N = l e n g t h ( x ) -1;
13
14 % I n i t i a l i s e A and b to zero
15 A = z e r o s ( N +1 , N +1);
16 b = z e r o s ( N +1 , 1);
17
18 % Loop over e l e m e n t s c a l c u l a t i n g local c o n t r i b u t i o n s and
19 % i n s e r t i n g into linear system
20 for k = 1: N
21 [ A_local , b _ l o c a l ] = ...
22 C a l c u l a t e C o n t r i b u t i o n O n E l e m e n t ( x , k , p , q , r , f );
23
24 A ( k : k +1 , k : k +1) = A ( k : k +1 , k : k +1) + A _ l o c a l ;
25 b ( k : k +1) = b ( k : k +1) + b _ l o c a l ;
26 end
27
28 % Add c o n t r i b u t i o n s from N e u m a n n b o u n d a r y c o n d i t i o n s into
29 % linear system
30 b ( N +1) = b ( N +1) + e t a _ b ;
31
32 % Set D i r i c h l e t b o u n d a r y c o n d i t i o n s in row 1
33 A (1 ,:) = 0;
34 A (1 ,1) = 1;
35 b (1) = u_a ;
36
37 % Solve linear system and plot FE s o l u t i o n
38 U = A\b;
39 plot ( x , U , - o )
40 x l a b e l ( x )
41 y l a b e l ( U )
42
43
44
45 f u n c t i o n [ A_local , b _ l o c a l ] = ...
46 CalculateContributionOnElement (x , k , p , q , r , f)
47
48 % C a l c u l a t e l e n g t h of e l e m e n t
49 h = x ( k +1) - x ( k );
50
51 % Define quadrature scheme
52 M = 3;
53 Q u a d W e i g h t s = [5/18 , 4/9 , 5 / 1 8 ] ;
54 Q u a d P o i n t s = [0.5*(1 - sqrt (0.6)) , 0.5 , 0 . 5 * ( 1 + sqrt ( 0 . 6 ) ) ] ;
55
3.7 Computational Implementation 45

56 % I n i t i a l i s e local c o n t r i b u t i o n s to A and b to zero


57 A _ l o c a l = z e r o s (2 ,2);
58 b _ l o c a l = z e r o s (2 ,1);
59
60 % Loop over q u a d r a t u r e p o i n t s
61 for m = 1: M
62 X = Q u a d P o i n t s ( m );
63
64 % E v a l u a t e l o c a l basis f u n c t i o n s and their d e r i v a t i v e s
65 p h i _ l o c a l = [1 - X , X ];
66 p h i _ p r i m e _ l o c a l = [ -1 , 1];
67
68 % E v a l u a t e p , q , r , f at q u a d r a t u r e p o i n t s
69 p _ v a l = p ( x ( k )+ h * X );
70 q _ v a l = q ( x ( k )+ h * X );
71 r _ v a l = r ( x ( k )+ h * X );
72 f _ v a l = f ( x ( k )+ h * X );
73
74 % I n c r e m e n t e n t r i e s of A _ l o c a l and b _ l o c a l
75 % with s u i t a b l y w e i g h t e d f u n c t i o n e v a l u a t i o n s
76 for i =1:2
77 for j =1:2
78 A _ l o c a l (i , j ) = A _ l o c a l ( i , j ) + ...
79 h * Q u a d W e i g h t s ( m )*( ...
80 p _ v a l / h / h * p h i _ p r i m e _ l o c a l ( i )* ...
81 p h i _ p r i m e _ l o c a l ( j ) + ...
82 q _ v a l / h * p h i _ l o c a l ( i )* ...
83 p h i _ p r i m e _ l o c a l ( j ) + ...
84 r _ v a l * p h i _ l o c a l ( i )* p h i _ l o c a l ( j ));
85 end
86 b _ l o c a l ( i ) = b _ l o c a l ( i ) + h * Q u a d W e i g h t s ( m )* ...
87 f _ v a l * p h i _ l o c a l ( i );
88 end
89 end

3.8 Robin Boundary Conditions

In Sect. 3.1, we defined a general second-order differential equation, Eq. (3.1), that
was subject to one Dirichlet boundary condition, Eq. (3.2), and one natural Neumann
boundary condition, Eq. (3.3). Many boundary value problems can be written in the
form of Eq. (3.1), subject to some combination of Dirichlet and Neumann boundary
conditions, and so this can be thought of as a reasonably general boundary value
problem. There is, however, a third variety of boundary conditionknown as a
Robin boundary conditionthat relates both the function value and the derivative
of the function on the boundary.
We demonstrate how Robin boundary conditions may be handled by the finite
element method using a slight modification of the boundary value problem given in
Sect. 3.1. We use the differential equation given by Eq. (3.1) on the interval a < x < b
and the Dirichlet boundary condition at x = a given by Eq. (3.2). Rather than using
46 3 Linear Boundary Value Problems

the Neumann boundary condition given by Eq. (3.3), we specify a Robin boundary
condition at x = b. The differential equation and boundary conditions are then given
by
 
d du du
p(x) + q(x) + r (x)u = f (x), (3.69)
dx dx dx
u = ua , x = a, (3.70)
du
p(x) + u = b , x = b, (3.71)
dx

where p(x), q(x), r (x), f (x) are given functions, and u a , b , are given constants.

3.8.1 The Weak Formulation

As before, the first step when computing the finite element solution of Eqs. (3.69)
(3.71) is to write the problem in weak form. We have a Dirichlet boundary condition
at x = a, and so the space H01 (a, b) for this boundary value problem comprises
those functions v(x) H 1 (a, b) that also satisfy v(a) = 0. We then proceed as
in Sect. 3.2.2. We multiply the given differential equation by a test function v(x)
H01 (a, b) and integrate over the interval a < x < b. After integrating by parts, we
obtain
   b  b
du b du dv du
p v + p + q v + r uv dx = f v dx.
dx a a dx dx dx a

Using v(a) = 0 (a consequence of v(x) H01 (a, b)) and the Robin boundary condi-
tion, Eq. (3.71), the equation above may be written as
 b  b
du dv du
p + q v + r uv dx + u(b)v(b) = f v dx + b v(b). (3.72)
a dx dx dx a

We then define the weak solution of Eqs. (3.69)(3.71) by:

find u(x) HE1 (a, b) such that Eq. (3.72) holds for all v(x) H01 (a, b),

where as defined by Eq. (3.10), HE1 (a, b) is the set of u(x) H 1 (a, b) that also
satisfy the Dirichlet boundary condition given by Eq. (3.70).
3.8 Robin Boundary Conditions 47

3.8.2 The Finite Element Solution

Having derived the weak solution, we then specify the set S h , and the subsets of this
set S Eh and S0h , as in Sect. 3.3.2. We then define the finite element solution, U (x), of
Eqs. (3.69)(3.71) by

find U (x) S Eh such that


 b  b
dU di dU
p +q i + rU i dx + U (b)i (b) = f i dx + b i (b),
a dx dx dx a
(3.73)

for all i (x) S0h .

The only difference between Eq. (3.73) and the finite element solution given by
Eq. (3.35) is the extra term U (b)i (b) on the left-hand side of Eq. (3.73). This extra
term is straightforward to handle: using Eq. (3.18), we see that

1, i = N + 1,
i (b) =
0, i = N + 1,

and so this term will contribute only to row N + 1 of the linear system. Furthermore,
using Eq. (3.20),

U (b) = U (x N +1 ) = U N +1 .

The extra term in Eq. (3.73) therefore adds an extra term U N +1 to the left-hand side
of equation generated by using i = N +1 in Eq. (3.73), that is row N + 1 of the
linear system given by Eq. (3.41). This may be implemented very easily by adding
the quantity to the value of A N +1,N +1 defined earlier by Eq. (3.43).

3.9 A Bound on the Accuracy of the Solution

In this chapter, we have set out the theory necessary to apply the finite element method
to a general linear, second-order, boundary value problem. We have not yet, however,
said anything about whether the finite element solution is a good approximation to
the true solution, or how this approximation improves as the number of elements in
the mesh is increased. We now providewithout proofa theorem on the accuracy
of the finite element solution.

Theorem 3.1 Let p(x), q(x), r (x), f (x) be functions such that p(x), q(x), r (x),
f (x) and ddxp are continuous functions on the interval a < x < b. Suppose the
second-order differential equation, defined on a < x < b, and given by
48 3 Linear Boundary Value Problems
 
d du du
p(x) + q(x) + r (x)u = f (x),
dx dx dx

has a unique solution, subject to suitable given Dirichlet, Neumann or Robin bound-
ary conditions. Let U (x) be the finite element solution, calculated on a uniform mesh
of elements of length h, that is a linear approximation to the solution on each element.
Define the error, E(x), by

E(x) = U (x) u(x),

and the L 2 norm of the error by



 b
E L 2 = (E(x))2 dx.
a

There then exists a constant C > 0, independent of the mesh used, such that, as the
element size h 0, the error satisfies

E L 2 = Ch 2 .

The constant C that appears in Theorem 3.1 is not easy to obtain. If C is not
known, the reader may ask, quite reasonably, what the purpose of this error bound
is. There is a very compelling answer to this question. Suppose we calculate the
finite element solution on a mesh of elements with constant length h 1 . Suppose we
then compute a second finite element solution on a mesh where each element of the
original mesh is divided into two, so that the length of each element in the new mesh
is h 2 = h 1 /2. As the error in the L 2 norm depends on h 2 , we see that halving the
element length will reduce the L 2 norm of the error to a quarter of its original value.
As such, we can use the error bound to assess the relative error between these two
finite element solutions.
The error bound in Theorem 3.1 may also be used to confirm the efficiency of
the implementation of the finite element method when it is used to solve a model
problem with known solution, as was first introduced in Exercise 2.2 at the end of
Chap. 2. Taking the logarithm of both sides of the error bound in Theorem 3.1 gives

log E L 2 = log C + 2 log h. (3.74)

Noting that E L 2 may be calculated using



 N 
 xk+1
E L 2 = (U (x) u(x))2 dx, (3.75)
k=1 xk
3.9 A Bound on the Accuracy of the Solution 49

we may easily compute E L 2 for a given h, using quadrature if the integration is
difficult, or impossible, to evaluate analytically. See Exercise 2.2 at the end of Chap. 2
for more details on performing this computation. Plotting log E L 2 against log h
for a range of values of h, we see, from Eq. (3.74), that this graph should have slope
2 as h 0. If this slope is less than 2, then the implementation is not optimal.

3.10 Exercises

3.1 In this exercise, we will develop code that may be used to calculate the finite
element solution of the boundary value problem, given by the differential equation
 
d du du
4 +8 + 5u = (sin x + 8 cos x) , < x < ,
dx dx dx

subject to one natural Neumann boundary condition

du
4 = 4, x = ,
dx
and one Dirichlet boundary condition

u = 2, x = ,

where and are constants. This boundary value problem has solution
 
x ( + 1 2e2 ) x
u(x) = 2ex e sin + cos + sin x.
2 e 2

You may want to develop your code for calculating the finite element solution in
the subparts below based on the overview of the steps required given in Sect. 2.9,
and Listing 3.1.
The integrals required to assemble the global linear system for this example prob-
lem may all be evaluated analytically without too much difficulty. The reader may
decide for themselves whether to evaluate these integrals analytically, or to approx-
imate them numerically using Gaussian quadrature.
(a) Write down the weak formulation of the boundary value problem. Be sure to state
clearly how the Dirichlet boundary conditions are handled, and what conditions
are required on the test functions.
(b) Define the finite element solution and the linear system that determines the
coefficients of the basis functions in the finite element solution.
(c) Write code to calculate the finite element solution of the boundary value problem
in the case where = 0 and = 0. Note that some terms appearing in the linear
system do not need to be computed for these values of and .
50 3 Linear Boundary Value Problems

(d) Extend the code you wrote in part (c) so that it may be used to calculate the finite
element solution of the boundary value problem in the case where = 5 and
= 0.
(e) Extend the code you wrote in part (d) so that it may be used to calculate the finite
element solution of the boundary value problem in the case where = 5 and
= 1.
(f) Investigate the use of a nonuniform mesh with the code that you wrote for part
(e).

In parts (c)(f) above be sure to validate your finite element solution by comparison
with the true solution.

3.2 In this exercise, we will work with the differential equation


 
d 2 du
  du 2x 1
(1 + x) + 2 + x x2 + u=
dx dx dx 4
 
2x 1
(1 + x)2 + sin x x(1 + x) cos x, 0 < x < .
4

This differential equation has general solution


 
u(x) = 1 + x K 1 + K 2 ex + sin x,

for constants K 1 and K 2 that are determined from the boundary conditions. We will
now consider different selections of boundary conditions and derive the finite element
solution in each case. Remember that the weak formulation will change when the
boundary conditions are changed.
We suggest that the reader uses Gaussian quadrature to approximate the integrals
required to assemble the global linear system in this exercise.

(a) Compute the finite element approximation to the differential equation when u(x)
satisfies the Dirichlet boundary conditions

u(0) = 2, u( ) = 1 + .

In this case, the constants K 1 , K 2 that appear in the general solution are given
by

1 2e 1
K1 = , K2 = .
1 e 1 e

Check that your finite element solution is correct by comparison with the true
solution.
Calculate the finite element solution on several uniform meshes, and calculate
the L 2 norm of the error for each mesh. Confirm the result of Theorem 3.1,
3.10 Exercises 51

namely that the graph of log E L 2 against log h, where h is the element size,
has slope 2 as h 0.
(b) Compute the finite element approximation to the differential equation when u(x)
satisfies one Dirichlet boundary condition:

u = 0, x = 0,

and one natural Neumann boundary condition

du
(1 + x)2 = 0, x = .
dx
In this case, the constants K 1 , K 2 that appear in the general solution are given
by

2 1+ 2 1+
K1 = , K2 = .
1 + e (1 + 2 ) 1 + e (1 + 2 )

Check that your finite element solution is correct by comparison with the true
solution.
Calculate the finite element solution on several uniform meshes, and calculate
the L 2 norm of the error for each mesh. Confirm the result of Theorem 3.1, as in
part (a).

3.3 Calculate the finite element solution of the partial differential equation

d2 u
u = 0, 0 < x < 1,
dx 2
subject to one Robin boundary condition:

du
+ u = 2, x = 0,
dx
and one Dirichlet boundary condition:

u = e, x = 1.

Confirm your finite element solution is correct by comparison with the true solution,
u = ex .

3.4 A partial differential equation is defined, for x 2 + y 2 < 1, by


 
2u 2u
+ = 1,
x2 x2
52 3 Linear Boundary Value Problems

subject to the Dirichlet boundary conditions

u(x, y) = 0, x 2 + y 2 = 1.

This partial differential equation may be written in, cylindrical polar coordinates, as
the ordinary differential equation
 
1 d du
r = 1, (3.76)
r dr dr

subject to the boundary conditions

du
= 0, r = 0, (3.77)
dr
u = 0, r = 1, (3.78)

where r 2 = x 2 + y 2 . This ordinary differential equation has solution u(r ) =


(1 r 2 )/4, and so the solution of the partial differential equation is

1 
u(x, y) = 1 x 2 y2 .
4
By writing Eq. (3.76) in the form of Eq. (3.1), calculate the finite element solution
of Eqs. (3.76)(3.78). Verify that your solution is correct by comparison with the true
solution.

3.5 There is no guarantee that a solution exists to a general boundary value problem.
Furthermore, if a solution does exist, then it may not be unique. Should we attempt
to compute the finite element solution to a boundary value problem without a unique
solution, we would anticipate problems. In this exercise, we investigate this concept
using a simple example. The differential equation

d2 u
+ u = 0, 0 < x < ,
dx 2

has general solution u(x) = K 1 sin x + K 2 cos x, where K 1 , K 2 are constants deter-
mined from the boundary conditions.

(a) When u(x) satisfies the Dirichlet boundary conditions

u(0) = u( ) = 0,

the differential equation has the nonunique solution u(x) = K 1 sin x for any
constant K 1 . Attempt to calculate the finite element solution to this boundary
value problem. How does the solution vary with the number of elements N ?
3.10 Exercises 53

(b) When u(x) satisfies the Dirichlet boundary conditions

u(0) = 1, u( ) = 0,

the differential equation has no solution. Attempt to calculate the finite element
solution to this boundary value problem. How does the solution vary with the
number of elements N ?
Chapter 4
Higher Order Basis Functions

We claimed, in Chap. 1, that the finite element method is a very flexible technique for
calculating the numerical solution of differential equations. One justification for this
claim is that the finite element method allows the solution of a differential equation to
be approximated, on each element of the computational mesh, using a polynomial of
any degree chosen by the user. The material presented in earlier chapters of this book
has focused entirely on finite element solutions that are linear approximations on
each element. In this chapter, we extend this material to allow a general polynomial
approximation on each element.
The key to defining higher order global basis functions is to provide a mapping
between each element of the mesh and a canonical element, and to specify local
basis functions of the required degree on this canonical element. This approach was
introduced for linear basis functions in Chap. 3, and in this chapter, we will show
how it may be extended to higher order basis functions.

4.1 A Model Differential Equation

We illustrate the use of a higher degree of approximating polynomial on each element


using the same boundary value problem that was used in Chap. 3. That is, for a <
x < b,
 
d du du
p(x) + q(x) + r (x)u = f (x), (4.1)
dx dx dx

for given functions p(x), q(x), r (x) and f (x), where p(x) = 0 for a < x < b,
subject to a Dirichlet boundary condition at x = a:

u = ua , x = a, (4.2)
Springer International Publishing AG 2017 55
J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_4
56 4 Higher Order Basis Functions

for some given constant u a , and a natural Neumann boundary condition at x = b:

du
p(x) = b , x = b, (4.3)
dx
for a given constant b .
The first step when calculating a finite element solution of a differential equation
is to derive the weak formulation of the differential equation. This step is independent
of the basis functions used and will therefore be identical to that derived in Sect. 3.2.2,
re-stated below:

find u(x) HE1 (a, b) such that


 b  b
du dv du
p + q v + r uv dx = f v dx + b v(b), (4.4)
a dx dx dx a

for all v(x) H01 (a, b).

4.2 Quadratic Basis Functions

We now describe how to calculate the finite element solution of the model boundary
value problem, using a quadratic approximation to the solution on each element.

4.2.1 The Mesh

As in Chaps. 2 and 3, we partition the domain on which the differential equation is


defined, i.e. the interval a < x < b, into N elements. We do not make any assump-
tions on the size of the elements, allowing a nonuniform mesh to be used.
In earlier chapters, when calculating a finite element solution that was a linear
approximation to the solution on each element, we saw that a mesh comprising N
elements contained N + 1 nodes, with all nodes located on the boundaries of the
elements. When using a quadratic approximation to the solution on each element,
we will require, in addition to the nodes on the boundary of each element, an addi-
tional node inside each elementa total of N extra nodes. The nodes inside each
element are often referred to as interior nodes. Our mesh now contains 2N + 1 nodes,
x1 , x2 , . . . , x2N +1 . As before, we set the first and last node to lie on the boundary, so
that x1 = a, and x2N +1 = b, and insist that x j+1 > x j for j = 1, 2, . . . , 2N .
Using the conditions on the nodes set out above, element k occupies the interval
x2k1 x x2k+1 for k = 1, 2, . . . , N . We introduce one further condition on the
nodes: the node x2k is placed at the centre of element k for k = 1, 2, . . . , N , so that
4.2 Quadratic Basis Functions 57

x2k = 0.5(x2k1 + x2k+1 ). (4.5)

The nodes and elements for a suitable computational mesh are illustrated in Fig. 4.1.

4.2.2 The Definition of Quadratic Basis Functions

We now define quadratic basis functions on the mesh illustrated in Fig. 4.1. We follow
the same approach as in Sect. 3.3.2.2 and specify global basis functions through local
basis functions defined on the canonical element 0 X 1.
As element k occupies the interval x2k1 x x2k+1 , a suitable change of vari-
able that maps between element k and the canonical element is given by

x = x2k1 + (x2k+1 x2k1 )X, 0 X 1. (4.6)

Local basis functions on the canonical element are defined by


 
1
local,1 (X ) = 2 X (1 X ), (4.7)
2
local,2 (X ) = 4X (1 X ), (4.8)
 
1
local,3 (X ) = 2X X . (4.9)
2

These local basis functions are illustrated in Fig. 4.2. We now define the global basis
functions on element k by:

2k1 (X ) = local,1 (X ), 0 X 1, (4.10)


2k (X ) = local,2 (X ), 0 X 1, (4.11)
2k+1 (X ) = local,3 (X ), 0 X 1, (4.12)

Element 1 Element k Element N

x1 x2 x3 x4 x5 x6 x2k1 x2k x2k+1 x2N 1 x2N x2N +1

Fig. 4.1 The nodes and elements for a mesh that may be used to calculate a finite element solution
that is a quadratic approximation to the true solution on each element. Nodes on the boundary of
an element are denoted by a filled circle, and nodes in the interior of an element are denoted by an
open circle
58 4 Higher Order Basis Functions

local,1(X)

1.2 local,2(X)
local,3(X)
1

0.8

0.6

0.4

0.2

0.2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
X

Fig. 4.2 The local basis functions local,1 (X ), local,2 (X ) and local,3 (X ) defined by Eqs. (4.7)
(4.9)

j (X ) = 0, 0 X 1, j = 2k 1, j = 2k, j = 2k + 1.
(4.13)

The definition of global basis functions by Eqs. (4.6)(4.13) may be used on


any element k = 1, 2, . . . , N , in the mesh, fully specifying each of the global basis
functions 1 (x), 2 (x), . . . , 2N +1 (x). We now describe some of the features of
these global basis functions. Remember, from Eq. (4.5), that the interior node of
each element is placed at the midpoint of that element. Using the change of variable
given by Eq. (4.6), the local basis functions given by Eqs. (4.7)(4.9) may be written
in terms of the original variable x. On element k, we may then write the global basis
functions given by Eqs. (4.10)(4.13) as, for x2k1 x x2k+1 :

(x2k x)(x2k+1 x)
2k1 (x) = , (4.14)
(x2k x2k1 )(x2k+1 x2k1 )
(x x2k1 )(x2k+1 x)
2k (x) = , (4.15)
(x2k x2k1 )(x2k+1 x2k )
(x x2k1 )(x x2k )
2k+1 (x) = , (4.16)
(x2k+1 x2k1 )(x2k+1 x2k )
j (x) = 0, j = 2k 1, j = 2k, j = 2k + 1. (4.17)

The basis functions 2k1 (x), 2k (x), 2k+1 (x) are plotted in Fig. 4.3. We note
that the global basis functions fall into two categories. The first category contains
basis functions such as 2k1 (x) and 2k+1 (x) that are nonzero in two neighbouring
4.2 Quadratic Basis Functions 59

(x)
2k1
(x)
2k
1
2k+1
(x)

x 2k 3 x 2k 2 x 2k 1 x 2k x 2k+1 x 2k+2 x 2k+3


x
Fig. 4.3 The basis functions 2k1 (x), 2k (x) and 2k+1 (x) defined by Eqs. (4.6)(4.13)

elements. The second category is the basis functions such as 2k (x) that are nonzero
in only one element.
Using Eqs. (4.14)(4.17) we see that, on element k,

2k1 (x2k1 ) = 1, 2k1 (x2k ) = 0, 2k1 (x2k+1 ) = 0, (4.18)


2k (x2k1 ) = 0, 2k (x2k ) = 1, 2k (x2k+1 ) = 0, (4.19)
2k+1 (x2k1 ) = 0, 2k+1 (x2k ) = 0, 2k+1 (x2k+1 ) = 1, (4.20)

and, if j = 2k 1, j = 2k and j = 2k + 1,

j (x2k1 ) = 0, j (x2k ) = 0, j (x2k+1 ) = 0. (4.21)

As this is true for all elements, we may deduce that, for all basis functions j (x),
j = 1, 2, . . . , 2N + 1, we have:

1, i = j,
j (xi ) = (4.22)
0, i = j,
60 4 Higher Order Basis Functions

and so the quadratic global basis function j (x) takes the value 1 at the node where
x = x j , and the value zero at all other nodes.

4.2.3 The Finite Element Solution

In Chap. 3, we defined the set S h through the linear basis functions specified in
Sect. 3.3.2. We now modify the definition of this set to take account of the quadratic
basis functions specified in Sect. 4.2.2. The set S h is now defined to be those functions
V (x) that can be written in the form


2N +1
V (x) = V j j (x), (4.23)
j=1

where j (x), j = 1, 2, . . . , 2N + 1, are the quadratic basis functions that are given
by Eqs. (4.6)(4.13). The subsets S Eh and S0h of the set S h are then defined in a similar
manner to Sect. 3.3.3: the set S Eh contains functions of the form given by Eq. (4.23)
that satisfy all Dirichlet boundary conditions, and the set S0h contains functions of
the form given by Eq. (4.23) that take the value zero at all points where Dirichlet
boundary conditions are applied.
Having defined the sets S Eh and S0h , we may now define the finite element solution,
U (x), by the following modification of the weak solution given by Eq. (4.4):

find U (x) S Eh such that


 b  b
dU di dU
p +q i + rU i dx = f i dx + b i (b), (4.24)
a dx dx dx a

for all i (x) S0h .

4.2.4 The System of Algebraic Equations

We write our finite element solution as a linear sum of the basis functions defined by
Eqs. (4.6)(4.13):


2N +1
U (x) = U j j (x), (4.25)
j=1

and note that U (x), as written above, lies in the set S h defined in Sect. 4.2.3. Using
Eq. (4.22), we have
4.2 Quadratic Basis Functions 61

U (xi ) = Ui , i = 1, 2, . . . , 2N + 1, (4.26)

and so, as in earlier chapters, Ui , for i = 1, 2, . . . , 2N + 1, is the finite element


solution at the node where x = xi .
The finite element solution given by Eq. (4.25) contains 2N + 1 unknown quan-
tities U1 , U2 , . . . , U2N +1 , and so we will need 2N + 1 equations to determine these
values. As in Chaps. 2 and 3, these equations will arise from two sources: (i) demand-
ing that the finite element solution satisfies the Dirichlet boundary conditions and (ii)
substituting suitable basis functions i (x) S0h into the integral given by Eq. (4.24).
We now explain how both categories of equations may be generated.
We first derive an algebraic equation that ensures that the finite element solution
given by Eq. (4.25) satisfies all Dirichlet boundary conditions, thus satisfying the
condition that U (x) lies in the set S Eh . The model problem contains one Dirichlet
boundary condition, given by Eq. (4.2), which applies at the boundary x = a. This
boundary condition therefore applies at the node where x = x1 . Using Eq. (4.26),
we see that the finite element solution satisfies the Dirichlet boundary condition at
x = x1 if

U1 = u a , (4.27)

which is our first algebraic equation.


We now explain how a further 2N algebraic equations may be generated by sub-
stituting suitable basis functions i (x) S0h into the integral condition given by
Eq. (4.24). Noting that the Dirichlet boundary condition given by Eq. (4.2) is applied
at the node where x = x1 , and observing that i (x1 ) = 0 for i = 2, 3, . . . , 2N + 1,
we see that these 2N basis functions take the value zero where Dirichlet boundary
conditions are applied. These functions are therefore members of the set S0h and suit-
able candidates for use in Eq. (4.24). Substituting these basis functions into Eq. (4.24)
yields, after some manipulation, a total of 2N algebraic equations:


2N +1  b   b
d j di d j
p +q i + r j i dx U j = f i dx + b i (b),
j=1 a dx dx dx a

(4.28)

for i = 2, 3, . . . , 2N + 1.
When defining the mesh in Sect. 4.2.1, we set x2N +1 = b. Using Eq. (4.22), we
may write

i (b) = i (x2N +1 )

1, i = 2N + 1,
= (4.29)
0, i = 2N + 1,

which allows us to evaluate the final term in Eq. (4.28).


62 4 Higher Order Basis Functions

Equations (4.27) and (4.28) specify the required 2N + 1 equations for the 2N + 1
unknown U j , j = 1, 2, . . . , 2N + 1, that appear in the expression for U (x) given by
Eq. (4.25). These equations may be written as the linear system

AU = b, (4.30)

where, for j = 1, 2, . . . , 2N + 1,

1, j = 1,
A1, j = (4.31)
0, j = 1,
 b
di d j d j
Ai, j = p + qi + r i j dx, i = 2, 3, . . . , 2N + 1, (4.32)
a dx dx dx

and

b1 = u a , (4.33)
 b
bi = f i dx, i = 2, 3, . . . , 2N , (4.34)
a
 b
b2N +1 = f 2N +1 dx + b . (4.35)
a

4.2.5 Assembling the Algebraic Equations

We will assemble the system of algebraic equations given by Eq. (4.30) in the same
spirit as in Sect. 3.4, when linear basis functions were used. We first calculate entries
of the form given by Eqs. (4.32), (4.34) and (4.35) that require the evaluation of
integrals of known functions over the whole domain a < x < b. These integrals
are evaluated by decomposing them into the sum of contributions from individual
elements. Remembering that element k occupies the region x2k1 x x2k+1 , we
may write Eq. (4.32) as, for i = 2, 3, . . . , 2N + 1, j = 1, 2, . . . , 2N + 1:

N 
 x2k+1
di d j d j
Ai, j = p + qi + r i j dx, (4.36)
k=1 x2k1 dx dx dx

and Eqs. (4.34) and (4.35) as

N 
 x2k+1
bi = f i dx, i = 2, 3, . . . , 2N , (4.37)
k=1 x2k1

N 
 x2k+1
b2N +1 = f 2N +1 dx + b . (4.38)
k=1 x2k1
4.2 Quadratic Basis Functions 63

Having calculated these entries, we complete the assembly of the linear system by
setting entries defined by Eqs. (4.31) and (4.33) that are given by explicit expressions.
We now describe the assembly of both categories of entry in more detail.

4.2.5.1 The Contributions from Integrals

We now explain how to evaluate the contributions to Eqs. (4.36)(4.38) from indi-
vidual elements. When evaluating local contributions from individual elements in
Sect. 3.4.1, when linear basis functions were used, we noted that only two basis func-
tions were nonzero on each element. Nonzero contributions from element k to the
(k)
global matrix A and global vector b were then stored in a local matrix Alocal of size
(k)
2 2, and a local vector blocal with two entries. We take a similar approach here.
The contributions from element k to Eq. (4.36) are given by, for i = 2, 3, . . . ,
2N + 1, j = 1, 2, . . . , 2N + 1:
 x2k+1
di d j d j
p + qi + r i j dx.
x2k1 dx dx dx

The only basis functions that are nonzero on element k are the three basis functions
2k1 , 2k and 2k+1 . The integral above is therefore zero, unless both i and j take
one of the values 2k 1, 2k or 2k + 1. We may therefore store the nonzero local
(k)
contributions to Eq. (4.36) from element k in the 3 3 matrix Alocal . The relationship
between the indices of the entries in this local matrix, and the indices of the entries
in the global matrix A that they contribute to, is given in Table 4.1.
Similarly, the contributions from element k to Eqs. (4.37) and (4.38) are given by,
for i = 2, 3, . . . , 2N + 1:

N 
 x2k+1
f i dx.
k=1 x2k1

This integral is zero unless i takes one of the values 2k 1, 2k or 2k + 1. We may


therefore store the nonzero local contributions to Eqs. (4.37) and (4.38) from element
(k)
k in the vector blocal with three entries. The relationship between the indices of the

Table 4.1 The relationship between the location of the entries of A(k) (k)
local and blocal , and the location
of the entries of A and b that they contribute to, when quadratic basis functions are used
Local index Global index
1 2k 1
2 2k
3 2k + 1
64 4 Higher Order Basis Functions

entries in this local vector, and the indices of the entries in the global vector b that
they contribute to, is given in Table 4.1.
(k) (k)
The entries of Alocal and blocal are given below.
 x2k+1
(k) d2k1 d2k1 d2k1
Alocal, 1,1 = p + q2k1 + r 2k1 2k1 dx,
x2k1 dx dx dx
 x2k+1
(k) d2k1 d2k d2k
Alocal, 1,2 = p + q2k1 + r 2k1 2k dx,
x2k1 dx dx dx
 x2k+1
(k) d2k1 d2k+1 d2k+1
Alocal, 1,3 = p + q2k1 + r 2k1 2k+1 dx,
x dx dx dx
 2k1
x2k+1
(k) d2k d2k1 d2k1
Alocal, 2,1 = p + q2k + r 2k 2k1 dx,
x2k1 dx dx dx
 x2k+1
(k) d2k d2k d2k
Alocal, 2,2 = p + q2k + r 2k 2k dx,
x2k1 dx dx dx
 x2k+1
(k) d2k d2k+1 d2k+1
Alocal, 2,3 = p + q2k + r 2k 2k+1 dx,
x2k1 dx dx dx
 x2k+1
(k) d2k+1 d2k1 d2k1
Alocal, 3,1 = p + q2k+1 + r 2k+1 2k1 dx,
x dx dx dx
 2k1
x2k+1
(k) d2k+1 d2k d2k
Alocal, 3,2 = p + q2k+1 + r 2k+1 2k dx,
x2k1 dx dx dx
 x2k+1
(k) d2k+1 d2k+1 d2k+1
Alocal, 3,3 = p + q2k+1 + r 2k+1 2k+1 dx,
x dx dx dx
 2k1
x2k+1
(k)
blocal, 1 = f 2k1 dx,
x2k1
 x2k+1
(k)
blocal, 2 = f 2k dx,
x2k1
 x2k+1
(k)
blocal, 3 = f 2k+1 dx.
x2k1

As with the integrals over each element that arose in Chap. 3 when linear basis
functions were used, there is no guarantee that the integrals above may be evaluated
analytically. Even if analytic evaluation is possible, it may require much tedious,
error-prone algebra. As such, it is often necessary, or desirable, to approximate these
integrals using numerical quadrature, as described in Sect. 3.5. When approximating
these integrals over element k using quadrature, we will use the change of variable
given by Eq. (4.6), which we now write as

x = x2k1 + h X, 0 X 1, (4.39)
4.2 Quadratic Basis Functions 65

where h = x2k+1 x2k1 . Using the change of variable given by Eq. (4.39), and the
(k)
definition of global basis functions given by Eqs. (4.6)(4.13), the entries of Alocal
(k)
and blocal may be written as
 1
(k) 1 dlocal, i dlocal, j
Alocal, i, j = h 2
p(x2k1 + h X ) +
0 h dX dX
1 dlocal, j
q(x2k1 + h X )local, i (X ) +
h dX
r (x2k1 + h X )local, i (X )local, j (X ) dX, i, j = 1, 2, 3, (4.40)
 1
(k)
blocal, i =h f (x2k1 + h X )local, i (X ) dX, i = 1, 2, 3. (4.41)
0

The integration required is now over the interval 0 X 1, allowing us to approxi-


mate these integrals using the quadrature techniques described in Sect. 3.5. Note the
similarity of Eqs. (4.40) and (4.41) with Eqs. (3.59) and (3.60). The only differences
are the range of the indices i and j, and the definition of local basis functions.
The entries of A and b that are given by Eqs. (4.32), (4.34) and (4.35) may now be
calculated by looping over all elements, calculating the nonzero local contributions
(k)
from each element given by Alocal and b(k)
local , and then adding these contributions to
the correct entries of A and b as given in Table 4.1. Finally, we complete the assembly
of b2N +1 by adding the final term on the right hand side of Eq. (4.35) to this entry
of b.

4.2.5.2 Completing the Assembly of the Linear System

The final task when assembling the system of algebraic equations given by Eq. (4.30)
is to set the entries that are given by Eqs. (4.31) and (4.33). We explained in Sect. 2.8.2
that, when computing the entries of the linear system that are given by equations
such as Eqs. (4.32), (4.34) and (4.35) using the method described in Sect. 4.2.5.1,
incorrect entries would be entered into the rows of the linear system where equations
such as Eqs. (4.31) and (4.33) apply. We therefore have to ensure that these entries
are overwritten by the correct entries.

4.2.6 Computational Implementation

We now provide an exemplar Matlab implementation of the material presented


in this chapter, using the same boundary value problem that was used in Chap. 3
which we re-state here. We calculate a finite element solution, using a quadratic
approximation on each element, of the differential equation
66 4 Higher Order Basis Functions
 
d du du
x (2x + 3) + (x + 2)u = x 3 e2x , 0.5 < x < 1, (4.42)
dx dx dx

together with a Dirichlet boundary condition at x = 0.5:

u = e, x = 0.5, (4.43)

and a natural Neumann boundary condition at x = 1:

du
x = 5e2 , x = 1. (4.44)
dx
This boundary value problem has solution
   
u(x) = ex K 1 + K 2 x 3 + e2x x 2 2x + 2 ,

where
 
32 e
K 1 = 3e 3e + ,
31 4
 
8 e
K2 = 3e + .
31 4

This boundary value problem may be related to the model problem defined by
Eqs. (4.1)(4.3) by writing

p(x) = x,
q(x) = (2x + 3),
r (x) = x + 2,
f (x) = x 3 e2x ,
u a = e,
b = 5e2 .

A Matlab implementation is given in Listing 4.1. This listing uses the same
pattern as in earlier chapters, where variable names follow the notation used in this
chapter.
The Matlab function requires an array x_ele_bound as input. This array
should contain only the nodes on the boundary of elements, and the entries should
be listed in ascending order. Thus, if N elements are used, the array should be of
length N + 1. The first entry should take the value 0.5, i.e. the value of x at the left
hand boundary, and the last entry should take the value 1, i.e. the value of x at the
right hand boundary. The function then returns a vector x that contains all nodes in
the mesh, and a vector U that contains the finite element solution at each node. The
function may be called by typing, for example,
4.2 Quadratic Basis Functions 67

x_ele_bound = linspace(0.5,1,101);
[x, U] = Chap4_CalculateQuadraticFemForBvp(x_ele_bound);

into a Matlab session. This will calculate the finite element solution on a mesh with
100 equally sized elements.
The functions p(x), q(x), r (x), f (x) and the values u a , b are defined in lines 5
10 of the listing. Noting that the vector x_ele_bound contains N + 1 nodes, where
N is the number of elements, we set the number of elements in line 13. There will be
2N + 1 nodes in the mesh, and so we allocate the appropriate memory for the vector
of nodes x in line 16. The nodes x2k1 , k = 1, 2, . . . , N + 1, are the entries of the
vector x_ele_boundthese are set in line 19. The nodes x2k , k = 1, 2, . . . , N ,
are the midpoints of the elementsthese are set in line 22. We then initialise the
entries of the global matrix A and vector b to zero in lines 25 and 26, before looping
over all elements in lines 3037 to first calculate the local contributions to A and b,
and then increment A and b appropriately.
The local contributions to both A and b from each element are calculated using
the function CalculateContributionOnElement given by lines 58102 of
the listing. This function first calculates the length of the interval in line 62 before
setting the number of Gaussian quadrature points, the quadrature points and the
quadrature weights in lines 6567. The entries of the local contributions to A and
b are initialised to zero in lines 7071. We then loop over all Gaussian quadrature
points in lines 74102. At each Gauss point, we calculate the value of all functions
appearing in Eqs. (4.40) and (4.41) in lines 7885. We then loop over all entries of
the nonzero local contributions to the linear system, incrementing each entry with
the appropriate weighting from the Gaussian quadrature scheme.
Returning to the main function, we handle the Neumann boundary conditions in
line 41, and the Dirichlet boundary conditions using lines 4446. Finally, we solve
the linear system in line 49, before plotting the solution using lines 5254. Note that
the solution plotted is not exactly the finite element solutionline 52 of the code
simply joins successive points in the mesh using a straight line, rather than plotting
the quadratic approximation to the solution on each element. We leave plotting the
finite element solution as a quadratic approximation on each element as an exercise
for the reader.

Listing 4.1 Chap4_CalculateQuadraticFemForBvp.m, a Matlab function for


calculating the finite element solution of a boundary value problem of the form given in
Sect. 4.1.
1 f u n c t i o n [x , U ] = ...
2 Chap4_CalculateQuadraticFemForBvp ( x_ele_bound )
3
4 % d e f i n e p ( x ) , q ( x ) , r ( x ) , f ( x ) , u_a , eta_b
5 p = @(x) -x;
6 q = @ ( x ) -(2* x +3);
7 r = @ ( x ) x +2;
8 f = @ ( x ) x ^3* exp (2* x );
9 u_a = exp (1);
10 eta_b = -5* exp (1)* exp (1);
11
12 % D e d u c e n u m b e r of e l e m e n t s
68 4 Higher Order Basis Functions

13 N = l e n g t h ( x _ e l e _ b o u n d ) -1;
14
15 % A l l o c a t e m e m o r y for nodes
16 x = zeros (2* N +1 , 1);
17
18 % Define nodes on edge of e l e m e n t s
19 x ( 1 : 2 : 2 * N +1) = x _ e l e _ b o u n d ;
20
21 % Define nodes at c e n t r e of e l e m e n t s
22 x ( 2 : 2 : 2 * N ) = 0 . 5 * ( x ( 1 : 2 : 2 * N -1) + x ( 3 : 2 : 2 * N + 1 ) ) ;
23
24 % I n i t i a l i s e A and b to zero
25 A = zeros (2* N +1 , 2* N +1);
26 b = zeros (2* N +1 , 1);
27
28 % Loop over e l e m e n t s c a l c u l a t i n g local c o n t r i b u t i o n s and
29 % i n s e r t i n g into linear s y s t e m
30 for k = 1: N
31 [ A_local , b _ l o c a l ] = ...
32 C a l c u l a t e C o n t r i b u t i o n O n E l e m e n t ( x , k , p , q , r , f );
33
34 A (2* k -1:2* k +1 , 2* k -1:2* k +1) = ...
35 A (2* k -1:2* k +1 , 2* k -1:2* k +1) + A _ l o c a l ;
36 b (2* k -1:2* k +1) = b (2* k -1:2* k +1) + b _ l o c a l ;
37 end
38
39 % Add c o n t r i b u t i o n s from N e u m a n n b o u n d a r y c o n d i t i o n s into
40 % linear system
41 b (2* N +1) = b (2* N +1) + eta_b ;
42
43 % Set D i r i c h l e t b o u n d a r y c o n d i t i o n s in row 1
44 A (1 ,:) = 0;
45 A (1 ,1) = 1;
46 b (1) = u_a ;
47
48 % Solve linear system
49 U = A\b;
50
51 % Plot finite e l e m e n t s o l u t i o n
52 plot ( x , U , - o )
53 xlabel ( x )
54 ylabel ( U )
55
56
57
58 f u n c t i o n [ A_local , b _ l o c a l ] = ...
59 C a l c u l a t e C o n t r i b u t i o n O n E l e m e n t (x , k , p , q , r , f )
60
61 % Calculate element length
62 h = x (2* k +1) - x (2* k -1);
63
64 % D e f i n e q u a d r a t u r e rule
65 M = 3;
66 Q u a d W e i g h t s = [5/18 , 4/9 , 5 / 1 8 ] ;
67 Q u a d P o i n t s = [0.5*(1 - sqrt (0.6)) , 0.5 , 0 . 5 * ( 1 + sqrt ( 0 . 6 ) ) ] ;
68
69 % I n i t i a l i s e local c o n t r i b u t i o n s to A and b to zero
70 A _ l o c a l = zeros (3 ,3);
71 b _ l o c a l = zeros (3 ,1);
72
73 % Loop over q u a d r a t u r e p o i n t s
74 for m = 1: M
75 X = Q u a d P o i n t s ( m );
4.2 Quadratic Basis Functions 69

76
77 % E v a l u a t e local basis f u n c t i o n s and their d e r i v a t i v e s
78 p h i _ l o c a l = [2*(1 - X )*(0.5 - X ) , 4* X *(1 - X ) , 2* X *( X - 0 . 5 ) ] ;
79 p h i _ p r i m e _ l o c a l = [4* X -3 , 4 -8* X , 4* X -1];
80
81 % E v a l u a t e p , q , r , f at q u a d r a t u r e p o i n t s
82 p_val = p ( x (2* k -1)+ h * X );
83 q_val = q ( x (2* k -1)+ h * X );
84 r_val = r ( x (2* k -1)+ h * X );
85 f_val = f ( x (2* k -1)+ h * X );
86
87 % I n c r e m e n t e n t r i e s of A _ l o c a l and b _ l o c a l
88 % with s u i t a b l y w e i g h t e d f u n c t i o n e v a l u a t i o n s
89 for i =1:3
90 for j =1:3
91 A _ l o c a l (i , j ) = A _ l o c a l (i , j ) + ...
92 h * Q u a d W e i g h t s ( m )*( ...
93 p_val / h / h * p h i _ p r i m e _ l o c a l ( i )* ...
94 p h i _ p r i m e _ l o c a l ( j ) + ...
95 q_val / h * p h i _ l o c a l ( i )* ...
96 p h i _ p r i m e _ l o c a l ( j ) + ...
97 r_val * p h i _ l o c a l ( i )* p h i _ l o c a l ( j ));
98 end
99 b _ l o c a l ( i ) = b _ l o c a l ( i ) + h * Q u a d W e i g h t s ( m )* ...
100 f_val * p h i _ l o c a l ( i );
101 end
102 end

4.2.7 A Note on Using Quadrature for Higher


Order Basis Functions

We have seen that the assembly of the system of algebraic equations that deter-
mines the finite element solution requires the evaluation of many integrals over the
whole computational domain. Throughout this book, we evaluate the integrals by
first calculating the nonzero contributions from each element, before adding these
local contributions into the global matrix A and global vector b. These integrals may
not, in general, be evaluated analytically and are usually computed using Gaussian
quadrature. It should be noted that increasing the order of the approximating poly-
nomial on each element increases the overall degree of the function that is being
integrated. As a consequence, more quadrature points may be needed to evaluate
these integrals sufficiently accurately.

4.3 Cubic Basis Functions

In Chap. 3, we explained how to calculate the finite element solution of a differential


equation, using a linear approximation to the solution on each element. In Sect. 4.2,
we developed the finite element method further, making a quadratic approximation
70 4 Higher Order Basis Functions

to the solution on each element. In this section, we explain, albeit in less detail, how
to calculate a finite element solution that is a cubic approximation to the solution on
each element. The key ingredients required that we do not already have are a suitable
computational mesh, and cubic basis functions defined on this mesh. We again use
the boundary value problem given by Eqs. (4.1)(4.3) when explaining how cubic
basis functions may be used.

4.3.1 The Mesh

We again partition the computational domain a < x < b into N elements. A cubic
approximation to the solution on each element requires that every element contains
four nodes. Two of these nodes will be the end-points of these elements. The other
two nodesthe interior nodeswill be placed inside the element so that the four
nodes are uniformly spaced across the element. There will be a total of N + 1 nodes
at the end-points of the N elements, and a further 2N nodes in the interior of the
elements, giving a total of 3N + 1 nodes.
The nodes are numbered as follows. We demand that: (i) x1 = a; (ii) x3N +1 = b;
(iii) the nodes are listed in ascending order, so that x j+1 > x j for j = 1, 2, . . . , 3N ;
and (iv) the nodes contained in each element are uniformly spaced across the ele-
ment. Using this convention, element k, where k = 1, 2, . . . , N , occupies the region
x3k2 x x3k+1 , and the elements span the whole of the computational domain
a < x < b. Insisting that the interior nodes of element k, where k = 1, 2, . . . , N , are
uniformly spaced across the element implies that:

1
x3k1 = (2x3k2 + x3k+1 ) , (4.45)
3
1
x3k = (x3k2 + 2x3k+1 ) . (4.46)
3
An example mesh, that may be used to calculate a finite element solution that is
a cubic approximation to the solution on each element, is illustrated in Fig. 4.4.

Element 1 Element k Element N

x1 x2 x3 x4 x5 x3k2 x3k1 x3k x3k+1 x3N2 x3N1 x3N x3N+1

Fig. 4.4 The nodes and elements of a finite element mesh that may be used to calculate a finite
element solution that is a cubic approximation on each element. Nodes on the boundary of an
element are denoted by a filled circle, and nodes in the interior of an element are denoted by an
open circle
4.3 Cubic Basis Functions 71

4.3.2 The Definition of Cubic Basis Functions

The quadratic basis functions used in Sect. 4.2 were defined in terms of local basis
functions defined on the canonical element 0 X 1. This approach may be
adapted to allow cubic basis functions to be defined. We first need a mapping between
element k of the mesh and the canonical element 0 X 1. As element k, where
k = 1, 2, . . . , N , occupies the region x3k2 x x3k+1 , we may map between this
element and the canonical element using the change of variable

x = x3k2 + (x3k+1 x3k2 )X, 0 X 1. (4.47)

Local basis functions are then given by


  
9 1 2
local,1 (X ) = X X (1 X ), (4.48)
2 3 3
 
27 2
local,2 (X ) = X X (1 X ), (4.49)
2 3
 
27 1
local,3 (X ) = X X (1 X ), (4.50)
2 3
  
9 1 2
local,4 (X ) = X X X . (4.51)
2 3 3

These local basis functions are illustrated in Fig. 4.5. Global basis functions j (x),
j = 1, 2, . . . , 3N + 1, may now be defined using these local basis functions. On
element k, we define the global basis functions in terms of the local basis functions
by:

3k2 (X ) = local,1 (X ), 0 X 1, (4.52)


3k1 (X ) = local,2 (X ), 0 X 1, (4.53)
3k (X ) = local,3 (X ), 0 X 1, (4.54)
3k+1 (X ) = local,4 (X ), 0 X 1, (4.55)
j (X ) = 0, 0 X 1, j = 3k 2, 3k 1, 3k, 3k + 1.
(4.56)

The basis functions 3k2 (x), 3k1 (x), 3k (x), 3k+1 (x) are shown in Fig. 4.6.
Using the global basis functions given by Eqs. (4.47)(4.56), and remembering
the location of interior nodes given by Eqs. (4.45) and (4.46), we may deduce the
following properties of these global basis functions on element k:
72 4 Higher Order Basis Functions

local,1(X)
1.6 local,2(X)
1.4 local,3(X)
local,4(X)
1.2

0.8

0.6

0.4

0.2

0.2

0.4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
X

Fig. 4.5 The local basis functions local,1 (X ), local,2 (X ), local,3 (X ) and local,4 (X ) defined by
Eqs. (4.48)(4.51)

(x)
3k2
(x)
3k1
1 (x)
3k
(x)
3k+1

x3k5 x3k4 x3k3 x3k2 x3k1 x3k x3k+1 x3k+2 x3k+3 x3k+4
x

Fig. 4.6 The basis functions 3k2 (x), 3k1 (x), 3k (x) and 3k+1 (x) defined by Eqs. (4.47)
(4.56)
4.3 Cubic Basis Functions 73

3k2 (x3k2 ) = 1, 3k2 (x3k1 ) = 0, 3k2 (x3k ) = 0, 3k2 (x3k+1 ) = 0,


3k1 (x3k2 ) = 0, 3k1 (x3k1 ) = 1, 3k1 (x3k ) = 0, 3k1 (x3k+1 ) = 0,
3k (x3k2 ) = 0, 3k (x3k1 ) = 0, 3k (x3k ) = 1, 3k (x3k+1 ) = 0,
3k+1 (x3k2 ) = 0, 3k+1 (x3k1 ) = 0, 3k+1 (x3k ) = 0, 3k+1 (x3k+1 ) = 1,

and, for j = 3k 2, 3k 1, 3k, 3k + 1,

j (x3k2 ) = 0, j (x3k1 ) = 0, j (x3k ) = 0, j (x3k+1 ) = 0.

These properties of basis functions hold on all elements. Hence, these basis functions
satisfy the usual property that, for j = 1, 2, . . . , 3N + 1,

1, i = j,
j (xi ) = (4.57)
0, i = j.

4.3.3 The Finite Element Solution

We now explain how the cubic basis functions, defined in Sect. 4.3.2, may be used
to calculate the finite element solution of the boundary value problem given by
Eqs. (4.1)(4.3). The set S h is now defined to be those functions V (x) that can be
written as


3N +1
V (x) = V j j (x),
j=1

where j (x), j = 1, 2, . . . , 3N + 1, are defined by Eqs. (4.47)(4.56), with the sub-


sets S Eh and S0h of the set S h defined as before. The finite element solution U (x) is
defined, as in earlier examples, by

find U (x) S Eh such that


 b  b
dU di dU
p +q i + rU i dx = f i dx + b i (b), (4.58)
a dx dx dx a

for all i (x) S0h .

Writing the finite element solution as the member of S h given by


3N +1
U (x) = U j j (x), (4.59)
j=1
74 4 Higher Order Basis Functions

the specification of the finite element solution by Eq. (4.58) allows us to derive a sys-
tem of algebraic equations that determines the unknown values U1 , U2 , . . . , U3N +1 .
As usual, these equations are drawn from two sources: (i) equations that ensure that
the finite element solution is in the set S Eh , i.e. we insist that U (x) satisfies the Dirich-
let boundary conditions and (ii) the substitution of suitable test functions i (x) S0h
into Eq. (4.58).
There is one Dirichlet boundary condition, given by Eq. (4.2), and it is applied
at the node where x = x1 . Using Eqs. (4.57) and (4.59), we see that this equation is
satisfied if

U1 = u a . (4.60)

Noting that i (x) S0h , for i = 2, 3, . . . , 3N + 1, we obtain a further 3N alge-


braic equations by substituting these test functions into Eq. (4.58). Combined with
Eq. (4.60), we now have 3N + 1 equations, as would be expected to determine
3N + 1 unknown values U1 , U2 , . . . , U3N +1 . Using similar manipulations to that
used for quadratic basis functions in Sect. 4.2.4, we may write these equations as

AU = b, (4.61)

where, for j = 1, 2, . . . , 3N + 1,

1, j = 1,
A1, j = (4.62)
0, j = 1,
 b
di d j d j
Ai, j = p + qi + r i j dx, i = 2, 3, . . . , 3N + 1, (4.63)
a dx dx dx

and

b1 = u a , (4.64)
 b
bi = f i dx, i = 2, 3, . . . , 3N , (4.65)
a
 b
b3N +1 = f 2N +1 dx + b . (4.66)
a

4.3.4 Assembling the Algebraic Equations

We now give a very brief account of how the linear system given by Eq. (4.61) may
be assembled. We use the same strategy as in previous examples, and first compute
the entries, given by Eqs. (4.63), (4.65) and (4.66), that are defined by integrals over
the whole domain. Having computed these entries, we then complete the assembly
of A and b by setting the entries given by Eqs. (4.62) and (4.64).
4.3 Cubic Basis Functions 75

(k) (k)
Table 4.2 The relation between the location of the entries in Alocal and blocal , and the location of
the entries of A and b that they contribute to, when cubic basis functions are used
Local index Global index
1 3k 2
2 3k 1
3 3k
4 3k + 1

As in earlier examples, the entries of the linear system given by Eqs. (4.63), (4.65)
and (4.66) may be calculated efficiently by decomposing them into the sum of the
nonzero contributions from integrals over individual elements. We note that, on
element k, there are only four nonzero basis functions: 3k2 (x), 3k1 (x), 3k (x),
3k+1 (x). Using the relation between local and global indices given in Table 4.2, we
may store the nonzero contributions from element k to Eqs. (4.63), (4.65) and (4.66)
(k) (k)
in a 4 4 local matrix Alocal , and a local vector blocal with four entries. These local
contributions are given in terms of integrals over the canonical element by
 1
(k) 1 dlocal, i dlocal, j
Alocal, i, j =h 2
p(x3k2 + h X ) +
0 h dX dX
1 dlocal, j
q((x3k2 + h X )local, i (x) +
h dX
r ((x3k2 + h X )local, i (X )local, j (X ) dX, i, j = 1, 2, 3, 4, (4.67)
 1
(k)
blocal, i =h f (x3k2 + h X )local, i (X ) dX, i = 1, 2, 3, 4, (4.68)
0

where h = x3k+1 x3k2 , and the local basis functions are defined by Eqs. (4.47)
(4.56).
Entries of the linear system given by Eqs. (4.63), (4.65) and (4.66) may therefore
be calculated by looping over all elements, calculating the nonzero local contri-
butions from each element given by Eqs. (4.67) and (4.68), and adding these local
contributions into the global matrix A and global vector b. We then add the final
term required to assemble Eq. (4.66), before setting the entries given by Eqs. (4.62)
and (4.64).

4.4 General Basis Functions

The material presented in Sect. 4.2 on quadratic basis functions, and in Sect. 4.3 on
cubic basis functions, may easily be generalised to a polynomial approximation of
an arbitrary degree. We now give a very brief overview of how this may be done.
76 4 Higher Order Basis Functions

4.4.1 The Mesh

Suppose, we use a polynomial approximation to the solution on each element that is


of degree , where 1, and partition the computational domain a < x < b into
N elements. A total of + 1 nodes will be required on each element to provide a
polynomial approximation of degree . On each element, there will be one node at
either end of the element. We therefore need an extra 1 interior nodes on each
element. This gives a total of N + 1 nodes in the mesh.
We label the nodes so that they are ordered in ascending order and cover the
whole domain. There are several consequences to these conditions. First, x1 = a and
x N +1 = b. Secondly, as the nodes are listed in ascending order we have x j+1 > x j
for j = 1, 2, . . . , N . Finally, element k, where k = 1, 2, . . . , N , occupies the region
x(k1)+1 x xk+1 .
We now define the 1 interior nodes of element k, where k = 1, 2, . . . , N , to
be uniformly spaced across the element. The location of these nodes is then given
by:

1 
x(k1)+ j+1 = ( j)x(k1)+1 + j xk+1 , j = 1, 2, . . . , 1.

4.4.2 Basis Functions

Global basis functions are again defined by: (i) defining a transformation between
element k and the canonical element 0 X 1; (ii) defining local basis functions
on the canonical element; and (iii) defining the global basis functions in terms of the
transformation and local basis functions.
Element k, where k = 1, 2, . . . , N , occupies the region x(k1)+1 x xk+1 .
A transformation between element k and the canonical element 0 X 1 is given
by
 
x = x(k1)+1 + xk+1 x(k1)+1 X, 0 X 1. (4.69)

Local basis functions on the canonical element are given by

i= j (X X i )
local, j (X ) =  , j = 1, 2, . . . , + 1, (4.70)
i= j X j X i

where
i 1
Xi = , i = 1, 2, . . . , + 1,

and the expression i= j (X X i ) is defined by
4.4 General Basis Functions 77
  
i= j (X X i ) = (X X 1 ) (X X 2 ) . . . X X j1 X X j+1 . . . (X X +1 ) .

It then follows that, for j = 1, 2, . . . , + 1,



1, i = j,
local, j (X i ) = (4.71)
0, i = j.

Global basis functions on element k are then defined by

(k1)+ j (X ) = local, j (X ), j = 1, 2, . . . , + 1, (4.72)


j (X ) = 0, otherwise. (4.73)

Using this definition of global basis functions, we may use Eq. (4.71) to deduce that,
for j = 1, 2, . . . , N + 1:

1, i = j,
j (xi ) =
0, i = j.

4.4.3 The Finite Element Solution

The set S h is now defined to be those functions V (x) that can be written as


N +1
V (x) = V j j (x),
j=1

where j (x), j = 1, 2, . . . , N + 1 are defined by Eqs. (4.69)(4.73), with the sub-


sets S Eh and S0h of the set S h defined as before. The finite element solution is then
defined by

find U (x) S Eh such that


 b  b
dU di dU
p +q i + rU i dx = f i dx + b i (b), (4.74)
a dx dx dx a

for all i (x) S0h .

Writing the finite element solution as the member of the set S h given by


N +1
U (x) = U j j (x),
j=1
78 4 Higher Order Basis Functions

a system of algebraic equations may be derived, in a similar manner to earlier exam-


ples, for the unknown values U1 , U2 , . . . , U N +1 . These equations will again arise
from two sources: equations that ensure that the finite element solution satisfies
the Dirichlet boundary conditions, and equations that arise from substituting a suit-
able test function i (x) into Eq. (4.74). These algebraic equations may be efficiently
assembled by first evaluating the entries that are given by integrals over the whole
domain; these entries are conveniently calculated by summing the contributions from
individual elements. Having done this, the contributions from both Neumann and
Dirichlet boundary conditions must be handled appropriately to complete the assem-
bly of the linear system.

4.5 Convergence of the Finite Element Solution

We conclude this chapter by presenting a bound on the L 2 norm of the error of the
finite element solution when a general polynomial approximation is made to the
solution on each element.
Theorem 4.1 Let p(x), q(x), r (x), f (x) be functions such that p(x), q(x), r (x),
f (x) and ddxp are continuous functions on the interval a < x < b. Suppose the second
order differential equation, defined on a < x < b, and given by
 
d du du
p(x) + q(x) + r (x)u = f (x),
dx dx dx

has a unique solution, subject to suitable given Dirichlet, Neumann or Robin bound-
ary conditions. Let U (x) be the finite element solution, calculated on a uniform
mesh of elements of length h, using an approximating polynomial of degree to the
solution on each element. Define the error, E(x), by

E(x) = U (x) u(x),

and the L 2 norm of the error by



 b
E L 2 = (E(x))2 dx.
a

There then exists a constant C > 0, independent of the mesh, such that, as the element
size h 0, the error satisfies

E L 2 = Ch +1 .

We see, from the theorem above, that if we increase the degree of the approxi-
mating polynomial by one, then the exponent of h in the error bound is increased by
4.5 Convergence of the Finite Element Solution 79

one. We also note that Theorem 3.1 is a special case of the theorem above, using the
value = 1.

4.6 Exercises

The exercises below are all based on the same boundary value problem as Exercise 3.1
from Chap. 3, with parameters = 5 and = 1, that is, the differential equation
 
d du du
4 +8 + 5u = sin x + 8 cos x, < x < ,
dx dx dx

subject to one Neumann boundary condition

du
4 = 20, x =
dx
and one Dirichlet boundary condition

u = 2, x = .

This boundary value problem has true solution


 
x 2(3 e2 ) x
u(x) = 2ex e sin + cos + sin x.
2 e 2

A computational implementation of the finite element method, using a linear


approximation to the solution on each element, was developed in Exercise 3.1.

4.1 In this exercise, we will develop a computational implementation of the finite


element method using a quadratic approximation to the solution on each element.

(a) Write down the weak formulation of the boundary value problem. State clearly
the sets that the weak solution and test functions lie in.
(b) Develop a computational implementation to calculate the finite element solution
of the boundary value problem that is a quadratic approximation to the solution on
each element. You may like to base this implementation on Listing 4.1. Confirm
that your implementation is correct by comparing the finite element solution
with the true solution.
(c) Write down an expression for the L 2 error of the finite element solution. Extend
your computational implementation so that it also outputs the L 2 error of the
finite element solution. Use your implementation to confirm the theoretical error
bound given by Theorem 4.1. If you are unsure how to do this, more details are
given in Exercise 2.2 from Chap. 2, and in Sect. 3.9.
80 4 Higher Order Basis Functions

4.2 In this exercise, we will develop a computational implementation of the finite


element method, using a cubic approximation to the solution on each element.
(a) Repeat Exercise 4.1 for a finite element solution that uses a cubic approximation
to the solution on each element.
(b) Use your implementation with M = 1, 2, 3, 4 quadrature points. Does this affect
the order of convergence of the solution?

4.3 An overview of the use of basis functions of a general polynomial degree to


calculate a finite element solution of a differential equation was given in Sect. 4.4.
Using this description, develop a finite element implementation that uses a quartic
(i.e. a degree four) polynomial approximation to the solution on each element.
Chapter 5
Nonlinear Boundary Value Problems

The differential equations investigated in earlier chapters have all been linear differ-
ential equations. The finite element discretisation of these equations yielded systems
of linear algebraic equations that may be solved using established, robust and reliable
linear algebra techniques. In this chapter, we describe the application of the finite
element method to nonlinear boundary value problems.
The key difference when applying the finite element method to nonlinear differ-
ential equations, rather than linear differential equations, is that we must solve a
system of nonlinear algebraic equations. Finding the solution of a system of non-
linear algebraic equations is, in general, more challenging than for linear systems.
An extensive discussion of the existence and uniqueness of the solution of a general
system of nonlinear algebraic equations is beyond the scope of this book. We do,
however, provide an introduction to the computation of the solution in Section A.2
of Appendix A that will be sufficient for most finite element applications.
We introduce the finite element method for nonlinear differential equations
through the use of a simple example. We then present a discussion of how this
implementation may be extended for more general nonlinear differential equations.
Throughout this chapter, we calculate finite element solutions that are a linear approx-
imation to the solution on each element. We leave the use of higher order basis
functions, described in Chap. 4, as an exercise for the reader.

5.1 A First Example

The implementation of the finite element method for nonlinear problems may be
demonstrated using the example nonlinear boundary value problem, defined by
 
d du
(1 + u 2 ) + 2(1 + x 4 ) + 8u 2 = 0, 1 < x < 2, (5.1)
dx dx

Springer International Publishing AG 2017 81


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_5
82 5 Nonlinear Boundary Value Problems

subject to one Dirichlet boundary condition and one natural Neumann boundary
condition:

u = 1, x = 1, (5.2)
du
(1 + u 2 ) = 68, x = 2. (5.3)
dx

The solution of this boundary value problem is u = x 2 .


As in earlier chapters, we have chosen to apply one Dirichlet boundary condi-
tion and one Neumann boundary condition in our model problem. This allows us
to illustrate how both categories of boundary condition are handled. The material
presented here may easily be adapted to any suitable combination of these boundary
conditions.

5.1.1 The Weak Formulation

As with linear differential equations, we begin by writing the boundary value problem
given by Eqs. (5.1)(5.3) in weak form using the Sobolev space, and subsets of
the Sobolev space, defined in Sect. 3.2.1. We multiply the differential equation by a
function v(x) H01 (1, 2) and integrate the resulting product over the computational
domain 1 < x < 2. After an application of integration by parts, we obtain
   2
du 2 du dv
(1 + u 2 ) v + (1 + u 2 ) + 2(1 + x 4 )v + 8u 2 v dx = 0. (5.4)
dx 1 1 dx dx

Our boundary value problem specifies one Dirichlet boundary condition, Eq. (5.2),
that applies at x = 1. As v(x) H01 (1, 2), we have v(x) = 0 at any value of
x where a Dirichlet boundary condition is applied, and so v(1) = 0. Using this
condition on v(1), and the Neumann boundary condition given by Eq. (5.3), we
may write Eq. (5.4) as
 2
du dv
(1 + u 2 ) + 2(1 + x 4 )v + 8u 2 v dx 68v(2) = 0.
1 dx dx

So far we have used the differential equation, Eq. (5.1), and the Neumann bound-
ary condition, Eq. (5.3). We have not yet used the Dirichlet boundary condition, other
than to ensure that test functions v(x) lie in the appropriate set of functions. We com-
plete the weak formulation by insisting that the weak solution satisfies the Dirichlet
boundary condition, given by Eq. (5.2). The weak solution of Eqs. (5.1)(5.3) is then
given by:
find u(x) H E1 (1, 2) such that
5.1 A First Example 83
 2 du dv
(1 + u 2 ) + 2(1 + x 4 )v + 8u 2 v dx 68v(2) = 0, (5.5)
1 dx dx

for all functions v(x) H01 (1, 2).

5.1.2 The Mesh, Basis Functions and Finite


Element Solution

We will calculate a finite element solution that is a linear approximation to the solution
of the boundary value problem on each element. The mesh and basis functions
described in Chap. 3 are suitable for this purpose. We therefore use the mesh of N
elements and N + 1 nodes described in Sect. 3.3.1, and the basis functions j (x),
j = 1, 2, . . . , N + 1, described in Sect. 3.3.2.2. Note that we use the definition of
the global basis functions in terms of local basis functions, defined on the canonical
element 0 X 1, and given by Eqs. (3.2.1)(3.2.6). This will aid the integration
over individual elements that is required when setting up the system of algebraic
equations later in this chapter.
The set S h takes the same definition as in Sect. 3.3.3. This set contains all functions
V (x) that can be written in the form

N +1

V (x) = V j j (x),
j=1

where j (x), j = 1, 2, . . . , N + 1 are defined by Eqs. (3.2.1)(3.2.6). Further, as in


Sect. 3.3.3, the set S Eh is the subset of S h that includes only functions that satisfy the
Dirichlet boundary condition, Eq. (5.2), and the set S0h is the subset of S h that includes
only functions that are zero where Dirichlet boundary conditions are applied.
Having defined the sets S Eh and S0h , and using the weak formulation given by
Eq. (5.5), we may define the finite element solution of Eqs. (5.1)(5.3) by:
find U (x) S Eh such that
 2 dU di
(1 + U 2 ) + 2(1 + x 4 )i + 8U 2 i dx 68i (2) = 0, (5.6)
1 dx dx

for all functions i (x) S0h .

5.1.3 The Algebraic Equations

We write the finite element solution as the member of S h given by

N +1

U (x) = U j j (x), (5.7)
j=1
84 5 Nonlinear Boundary Value Problems

where j (x), j = 1, 2, . . . , N + 1, are defined by Eqs. (3.21)(3.26). We will now


assemble N + 1 equations to allow us to determine the N + 1 unknown values
U1 , U2 , . . . , U N +1 . As with the systems of algebraic equations that were derived
from linear differential equations in earlier chapters, these equations will com-
prise: (i) equations that ensure that U (x) satisfies the Dirichlet boundary conditions,
i.e. U (x) S Eh ; and (ii) equations that arise from substituting a suitable test function
i (x) into Eq. (5.6).
The first condition on U (x) given in Eq. (5.6) is that U (x) S Eh . As U (x) is
specified by Eq. (5.7), it already lies in the set S h . To ensure that U (x) S Eh , we
demand that U (x) satisfies all Dirichlet boundary conditions. We have one Dirichlet
boundary condition, given by Eq. (5.2), which is applied at the node x1 = 1. Using
Eq. (3.37), this boundary condition is satisfied provided that

U1 = 1. (5.8)

The basis functions i (x), i = 2, 3, . . . , N +1, all satisfy the condition i (1) =
0 and therefore lie in the set S0h . These basis functions may be substituted into Eq. (5.6)
to deduce that, for i = 2, 3, . . . , N + 1:
 2
dU di
(1 + U 2 ) + 2(1 + x 4 )i + 8U 2 i dx 68i (2) = 0.
1 dx dx
(5.9)

Noting that x N +1 = 2, on using Eq. (3.20) we see that



0, i = 2, 3, . . . , N ,
i (2) =
1, i = N + 1.

We may therefore write Eq. (5.9) as


 2
dU di
(1 + U 2 ) + 2(1 + x 4 )i + 8U 2 i dx = 0, i = 2, 3, . . . , N , (5.10)
1 dx dx

and
 2
dU d N +1
(1 + U 2 ) + 2(1 + x 4 ) N +1 + 8U 2 N +1 dx 68 = 0. (5.11)
1 dx dx

Combining Eqs. (5.8), (5.10) and (5.11), we have N + 1 algebraic equations for the
N + 1 unknown values U1 , U2 , . . . , U N +1 , as required.
5.1 A First Example 85

5.1.4 Assembling the Algebraic Equations

We now explain how the system of algebraic equations, given by Eqs. (5.8), (5.10) and
(5.11), is assembled in practice. These equations contain the same features as those
seen in earlier chapters: (i) contributions from integrals over the whole domain (in
Eqs. (5.10) and (5.11)); (ii) a contribution from the Neumann boundary condition (in
Eq. (5.11)) that includes only a known explicit term; and (iii) an algebraic equation
to satisfy the Dirichlet boundary condition where all terms may be written down
explicitly (Eq. (5.8)).
As in earlier chapters, we decompose the integrals that appear in Eqs. (5.10) and
(5.11) into the sum of the contributions from each element. We write Eq. (5.10) as,
for i = 2, 3, . . . , N ,

N 
 xk+1
dU di
(1 + U 2 ) + 2(1 + x 4 )i + 8U 2 i dx = 0. (5.12)
k=1 xk dx dx

Contributions to the integral in Eq. (5.11) may be written in a similar manner. The
contribution to this sum from element k, i.e. the integral over xk x xk+1 , may
be calculated by mapping this element to the canonical element 0 X 1 using
the change of variable

x = xk + h X, 0 X 1, (5.13)

where h = xk+1 xk . Remembering, from Eqs. (3.22) and (3.23), that the local basis
functions are given by

local,1 (X ) = 1 X, (5.14)
local,2 (X ) = X, (5.15)

the finite element solution on element k is

U (X ) = Uk local,1 (X ) + Uk+1 local,2 (X ). (5.16)

We see from Eq. (5.12) that the term U 2 appears in the integral over each element.
On element k, this quantity is given by

U 2 = Uk2 local,1
2
(X ) + 2Uk Uk+1 local,1 (X )local,2 (X ) + Uk+1
2
local,2
2
(X ). (5.17)

It is clear from this expression for U 2 that the integral over each element required
to compute Eq. (5.12) is a nonlinear function of the values U1 , U2 , . . . , U N +1 . As
a consequence, it is not possible to write the system of algebraic equations as the
linear system AU = b as we did in Chaps. 2, 3 and 4. Instead, we write Eqs. (5.8),
(5.10) and (5.11) as the nonlinear system
86 5 Nonlinear Boundary Value Problems

R(U) = 0, (5.18)

where the vector Rcommonly known as the residual vectorhas entries given by
R1 (U) = U1 1, (5.19)
N  x k+1 dU di
Ri (U) = (1 + U 2 ) + 2(1 + x 4 )i + 8U 2 i dx, i = 2, 3, . . . , N ,
xk dx dx
k=1
(5.20)
N  x k+1
 dU d N +1
R N +1 (U) = (1 + U 2 ) + 2(1 + x 4 ) N +1 + 8U 2 N +1 dx 68.
dx dx
k=1 x k
(5.21)

An elementary discussion of methods for solving systems of nonlinear algebraic


equations, such as Eq. (5.18), is given in Appendix A.2. The easiest techniques to
use when solving these equations, for example, the function fsolve provided by
Matlab, require only specification of the function R(V) for a given V, and an initial
guess U0 to the solution U. We now explain how the function R(U) may be evaluated,
allowing the nonlinear system of equations given by Eq. (5.18) to be solved using a
suitable technique, such as the function fsolve provided by Matlab. We assemble
these equations, as in earlier chapters, by first evaluating the integrals over the whole
domain that appear in Eqs. (5.20) and (5.21). We then add in the extra contribution to
Eq. (5.21) that arises from the Neumann boundary condition, before setting Eq. (5.19)
to satisfy the Dirichlet boundary conditions.

5.1.4.1 Evaluating the Integrals on Each Element

Equations (5.20) and (5.21) reveal that, on element k, we have to calculate a contri-
bution to the residual of the form
 xk+1
dU di
(1 + U 2 ) + 2(1 + x 4 )i + 8U 2 i dx, (5.22)
xk dx dx

where i takes the values 2, 3, . . . , N + 1. As in Chaps. 2 and 3 we observe that,


on element k, the only basis functions that are nonzero are k and k+1 . The local
contribution to the residual from element k is then nonzero only for entries generated
with i = k and i = k + 1. As a consequence, we need only calculate the contribution
from this element to Eq. (5.22) using i = k and i = k + 1, which we denote by
(k) (k)
Rlocal,1 (U) and Rlocal,2 (U), respectively. These local contributions are given by
 xk+1
(k) dU dk
Rlocal,1 (U) = (1 + U 2 )+ 2(1 + x 4 )k + 8U 2 k dx, (5.23)
xk dx dx
 xk+1
(k) dU dk+1
Rlocal,2 (U) = (1 + U 2 ) + 2(1 + x 4 )k+1 + 8U 2 k+1 dx.
xk dx dx
(5.24)
5.1 A First Example 87

The local residuals, given by Eqs. (5.23) and (5.24), may easily be evaluated
by mapping element k to the canonical element 0 X 1, using the change of
variable given by Eq. (5.13). On element k, the basis functions k and k+1 are
given by Eqs. (5.14) and (5.15), and so the local contributions to the residual may be
written
 1
(k) 1 dU dlocal,1
Rlocal,1 (U) = h 2
(1 + U 2 ) + 2(1 + (xk + h X )4 )local,1 +
0 h dX dX
8U 2 local,1 dX, (5.25)
 1
(k) 1 dU d local,2
Rlocal,2 (U) = h 2
(1 + U 2 ) + 2(1 + (xk + h X )4 )local,2 +
0 h dX dX
8U 2 local,2 dX, (5.26)

where U 2 is given by Eq. (5.17), and

dU dlocal,1 dlocal,2
= Uk + Uk+1 . (5.27)
dX dX dX
The integrals given by Eqs. (5.25) and (5.26) may now be evaluated, either ana-
lytically or using quadrature. We then add these local contributions into the global
(k)
residual by incrementing the appropriate entry of R. The local contribution Rlocal,1
(k)
from element k was the integral generated from Eq. (5.22) with i = k: hence, Rlocal,1
should be added to entry Rk of the global residual. Similarly, the local contribution
(k)
Rlocal,2 from element k was the integral generated from Eq. (5.22) with i = k + 1
and should be added to entry Rk+1 of the global residual.
The integral contributions to R(U) may therefore be computed by looping over
all elements, calculating all nonzero local contributions as described above, and then
adding these local contributions into the global residual vector.

5.1.4.2 Setting the Entries Defined Explicitly

To complete specification of R(U), we need to: (i) add the final term into Eq. (5.21)
to satisfy the Neumann boundary condition; and (ii) specify R1 , given by Eq. (5.19),
to satisfy the Dirichlet boundary conditions. We now give an example computational
implementation of the material presented in this section.

5.1.5 Computational Implementation

In Listing 5.1, we provide a computational implementation of the material discussed


in this chapter for calculating the finite element solution of Eqs. (5.1)(5.3). As with
88 5 Nonlinear Boundary Value Problems

Matlab implementations in earlier chapters, the variable names correspond to the


notation used when presenting the material.
The listing begins by requiring the user to provide an appropriate mesh x, and
initial guess to the solution at each node in the mesh U0 . The vector x that specifies
the mesh should satisfy the properties listed in Sect. 3.3.1: (i) if there are N elements
in the mesh, then this vector should be of length N + 1; (ii) x1 = 1; (iii) x N +1 = 2;
and (iv) the entries of the vector should be listed in ascending order. The in-built
Matlab function fsolve is then called on line 5. This function finds the finite
element solution U, starting from the initial guess U0 , such that all components
of the vector-valued function CalculateResidual are zero, as is required by
Eq. (5.18)see the Matlab documentation for more details. The solution U is
returned as the output of the Matlab function.
The function CalculateResidual begins at line 15 of the listing. The func-
tion first deduces the number of elements (line 18) and initialises the variable
Residual to be a vector of the correct size, with all entries set to zero (line 21).
We then loop over all elements in lines 2528, calculating the nonzero local con-
tributions to the residual from each element, before incrementing the appropriate
entries of the global residual with these local contributions. Finally, this function
adds the contribution to entry N + 1 of the global vector that is required to satisfy
the Neumann boundary condition at x = 2 (line 31) and sets the first entry of this
vector to satisfy the Dirichlet boundary condition at x = 1 (line 34).
The local contributions to the residual on each element are calculated using the
function CalculateResidualOnElement that starts on line 39 of the listing.
We begin this function by calculating the element length (line 42), and defining
the numerical quadrature scheme used (lines 4547), before initialising the local
contribution to the residual to zero (line 50). We then loop over the quadrature
points, calculating the contributions to the local residuals (given by Eqs. (5.25) and
(5.26)) from each of these pointswe first evaluate the quantities that appear in these
equations (lines 6065), before adding suitably weighted function evaluations into
the local contributions to the residual (lines 6875).
Having calculated U, we then plot the solution (lines 810).
Suppose we want to calculate the finite element solution of Eqs. (5.1)(5.3),
using a uniform mesh with 100 elements, and an initial guess to the solution that is
zero at each node. The Matlab implementation given in Listing 5.1 may be called
by typing
x = linspace(-1,2,101);
U0 = zeros(101,1);
U = Chap5_CalculateFemForNonlinearBVP(x, U0);

into a Matlab session.


Note that we did not put much effort into specifying our initial guess to the solution,
simply setting this guess to be zero at each node. In this case, the Matlab routine
used to solve the system of nonlinear equations found the solution from this initial
guess. Readers should be aware, however, that for some problems a more accurate
initial guess to the solution may be required.
5.1 A First Example 89

Listing 5.1 Chap5_CalculateFemForNonlinearBVP.m, a Matlab function for


calculating the finite element solution of Eqs. (5.1)(5.3), specifying only the residual of the

nonlinear system.
1 % S p e c i f y mesh x and i n i t i a l g u e s s U0
2 f u n c t i o n U = C h a p 5 _ C a l c u l a t e F e m F o r N o n l i n e a r B V P (x , U0 )
3
4 % Use M a t l a b r o u t i n e to solve n o n l i n e a r a l g e b r a i c s y s t e m
5 U = f s o l v e ( @ C a l c u l a t e R e s i d u a l , U0 , [] , x );
6
7 % Plot s o l u t i o n
8 plot (x , U , - )
9 x l a b e l ( x )
10 y l a b e l ( U )
11
12
13
14 % F u n c t i o n for c a l c u l a t i n g g l o b a l r e s i d u a l
15 f u n c t i o n R e s i d u a l = C a l c u l a t e R e s i d u a l (U , x )
16
17 % D e d u c e n u m b e r of e l e m e n t s
18 N = l e n g t h ( x ) - 1;
19
20 % I n i t i a l i s e r e s i d u a l to zero
21 R e s i d u a l = zeros ( N +1 , 1);
22
23 % Loop over elements , c o m p u t i n g local c o n t r i b u t i o n to
24 % r e s i d u a l and i n c r e m e n t i n g g l o b a l r e s i d u a l
25 for k =1: N
26 L o c a l R e s i d u a l = C a l c u l a t e R e s i d u a l O n E l e m e n t (U , x , k );
27 R e s i d u a l ( k : k +1) = R e s i d u a l ( k : k +1) + L o c a l R e s i d u a l ;
28 end
29
30 % H a n d l e N e u m a n n b o u n d a r y c o n d i t i o n at x =2
31 R e s i d u a l ( N +1) = R e s i d u a l ( N +1) - 68;
32
33 % H a n d l e D i r i c h l e t b o u n d a r y c o n d i t i o n at x = -1
34 R e s i d u a l (1) = U (1) - 1;
35
36
37
38 % F u n c t i o n for c a l c u l a t i n g local r e s i d u a l
39 f u n c t i o n L o c a l R e s i d u a l = C a l c u l a t e R e s i d u a l O n E l e m e n t (U , x , k )
40
41 % Set e l e m e n t l e n g t h h
42 h = x ( k +1) - x ( k );
43
44 % D e f i n e q u a d r a t u r e rule
45 M = 3;
46 Q u a d W e i g h t s = [5/18 , 4/9 , 5 / 1 8 ] ;
47 Q u a d P o i n t s = [0.5*(1 - sqrt (0.6)) , 0.5 , 0 . 5 * ( 1 + sqrt ( 0 . 6 ) ) ] ;
48
49 % I n i t i a l i s e local r e s i d u a l to zero
50 L o c a l R e s i d u a l = zeros (2 ,1);
51
52 % Loop over q u a d r a t u r e p o i n t s to e v a l u a t e i n t e g r a t i o n
53 % using G a u s s i a n q u a d r a t u r e
54 for m =1: M
55 % Set local c o o r d i n a t e X
56 X = Q u a d P o i n t s ( m );
90 5 Nonlinear Boundary Value Problems

57
58 % C a l c u l a t e local values of basis functions ,
59 % their derivatives , x , U and the d e r i v a t i v e of U
60 p h i _ l o c a l = [1 - X , X ];
61 p h i _ p r i m e _ l o c a l = [ -1 , 1];
62 x_local = x(k) + h*X;
63 L o c a l U = U ( k )* p h i _ l o c a l (1) + U ( k +1)* p h i _ l o c a l (2);
64 L o c a l U P r i m e = U ( k )* p h i _ p r i m e _ l o c a l (1) + ...
65 U ( k +1)* p h i _ p r i m e _ l o c a l (2);
66
67 % E v a l u a t e i n t e g r a n d and add into sum
68 for i =1:2
69 L o c a l R e s i d u a l ( i ) = L o c a l R e s i d u a l ( i ) + ...
70 h * Q u a d W e i g h t s ( m )* ...
71 (1/ h / h * ( 1 + L o c a l U * L o c a l U )* ...
72 L o c a l U P r i m e * p h i _ p r i m e _ l o c a l ( i ) + ...
73 2 * ( 1 + x _ l o c a l ^4)* p h i _ l o c a l ( i ) + ...
74 8* L o c a l U * L o c a l U * p h i _ l o c a l ( i ));
75 end
76 end

5.1.6 Calculation of the Jacobian Matrix

In Sect. 5.1.5, we provided a computational implementation for calculating the finite


element solution of Eqs. (5.1)(5.3). We used the Matlab function fsolve to
solve the system of nonlinear equations given by Eq. (5.18), using specification only
of the residual vector R. Some numerical methods for solving nonlinear systems of
algebraic equations require specification not just of the residual vector, but also the
Jacobian matrix, denoted by J , with entries given by

Ri
Ji, j (U) = , i, j = 1, 2, . . . , N + 1. (5.28)
U j

Even if the numerical method being used does not require the Jacobian matrix to
be specified, it may provide the option to specify this matrix; the Matlab function
fsolve (that was used in Listing 5.1) allows the user to do this. Supplying the
Jacobian matrix will usually improve the efficiency and the reliability of the root-
finding algorithm.
We now explain how entries of the Jacobian matrix, defined by Eq. (5.28), may
be calculated. Clearly from Eq. (5.19), the first row of the Jacobian matrix is given
by, for j = 1, 2, . . . , N + 1,

1, j = 1,
J1, j = (5.29)
0, j = 1.
5.1 A First Example 91

We now consider rows 2, 3, . . . , N + 1 of the Jacobian matrix. Using Eqs. (5.20)


and (5.21), we see that, for i = 2, 3, . . . , N + 1, the entries of the Jacobian matrix
can be expressed as the sum of the contributions from each element:

N  xk+1
dU di
Ji, j = (1 + U 2 ) + 2(1 + x 4 )i +8U 2 i dx,
k=1
U j x k
dx dx
j = 1, 2, . . . , N + 1,
(5.30)

where we have interchanged the order of summation and partial differentiation.


As the reader would expect, the entries of Eq. (5.30) are assembled by calcu-
lating the contribution from individual elements. The contribution to Ji, j , where
i = 2, 3, . . . , N + 1, and j = 1, 2, . . . , N + 1, from integrating over element k is
given by

xk+1
dU di
(1 + U 2 ) + 2(1 + x 4 )i + 8U 2 i dx. (5.31)
U j xk dx dx

Remembering that k (x) and k+1 (x) are the only basis functions that are nonzero on
element k, the integrand above will be zero unless i = k or i = k + 1. We therefore
need only calculate the contributions to this integral using i = k and i = k + 1, as
all other entries will be zero. Recalling the definition of the local contributions to the
(k) (k)
residual on element k, denoted by Rlocal,1 (U), Rlocal,2 (U) and defined by Eqs. (5.23)
and (5.24), we see that the nonzero contributions to Eq. (5.31) when i = k are, for
j = 1, 2, . . . , N + 1,
(k)
Rlocal,1
, (5.32)
U j

and, when i = k + 1, the contributions to Eq. (5.31) are, for j = 1, 2, . . . , N + 1,


(k)
Rlocal,2
. (5.33)
U j

Further, on element k, the finite element solution U (x) may be written

U (x) = Uk k (x) + Uk+1 k+1 (x), (5.34)

and so the local contributions from element k to the Jacobian given by Eq. (5.30)
depend only on the entries Uk and Uk+1 of the vector U. As a consequence, the
expressions given by Eqs. (5.32) and (5.33) are zero unless j = k or j = k + 1.
We now see that the local contribution from element k to the global Jacobian matrix,
given by Eq. (5.30), is nonzero for only four entries of J : Jk,k , Jk,k+1 , Jk+1,k and
92 5 Nonlinear Boundary Value Problems

Jk+1,k+1 . As such, only these entries need be calculated. They may be stored in the
(k)
2 2 matrix denoted by Jlocal , with entries given by
(k) (k)
Rlocal,1 Rlocal,1
(k) U U k+1
Jlocal = k
(k) (k) , (5.35)
Rlocal,2 Rlocal,2
Uk Uk+1

(k) (k) (k)


where Jlocal,1,1 contributes to Jk,k , Jlocal,1,2 contributes to Jk,k+1 , Jlocal,2,1 contributes
(k)
to Jk+1,k , and Jlocal,2,2 contributes to Jk+1,k+1 .
The partial derivatives that appear in Eq. (5.35) are straightforward to evaluate by
partial differentiation of Eqs. (5.25) and (5.26). Using Eqs. (5.27) and (5.34), we see
that when element k is mapped to the canonical interval 0 X 1

U U
= local,1 (X ), = local,2 (X ),
Uk Uk+1
   
dU dlocal,1 dU dlocal,2
= , = .
Uk dX dX Uk+1 dX dX

Hence, using the chain rule,


(k)
(k) Rlocal,1
Jlocal,1,1 =
Uk
 1
1 dU dlocal,1
=h (1 + U 2 ) + 2(1 + (xk + h X )4 )local,1 +
Uk 0 h 2 dX dX
8U 2 local,1 dX,
 1    
1 U dU dU dlocal,1
=h 2U + (1 + U ) 2
+
0 h 2 U k dX U k dX dX
U
16U local,1 dX
Uk
 1  
1  dU 2 dlocal,1 dlocal,1
=h 2
2U local,1 + (1 + U ) +
0 h dX dX dX
16U local,1 local,1 dX.

More generally, for i = 1, 2, and j = 1, 2:


  
(k)
1
1  dU dlocal, j dlocal,i
Jlocal,i, j = h 2U local, j + (1 + U 2 ) +
0 h2 dX dX dX
16U local, j local,i dX. (5.36)

These integrals may easily be evaluated using quadrature.


5.1 A First Example 93

The global Jacobian may therefore be computed by looping over all elements,
calculating the local nonzero contributions on each element, before adding these
local contributions into the global matrix J . Having done this, we set the entries
corresponding to rows of the residual that enforce Dirichlet boundary conditions,
that is, the entries given by Eq. (5.29).

5.1.6.1 Computational Implementation

In Listing 5.1, we presented a computational implementation for calculating the


finite element solution of Eqs. (5.1)(5.3), solving the resulting nonlinear system
of algebraic equations given by Eq. (5.18) by specifying only the residual vector.
In Listing 5.2, we extend this computational implementation, solving the nonlinear
system of algebraic equations by specification of both the residual vector and Jacobian
matrix. We assume that the reader is familiar with the implementation given in
Listing 5.1 and describe only the modifications made in the listing below.
The mesh and initial guess to the solution should be set as described in Sect. 5.1.5.
Specification of only the residual vector is the default for the Matlab function
fsolve, and so we set the option that allows fsolve to use the Jacobian matrix
in line 8, and pass this option to fsolve in line 10.
We write a function CalculateResidualAndJacobian in lines 2046 of
the listing that computes both the global residual vector and global Jacobian matrix.
This function initialises both of these to zero (lines 2728), before looping over
all elements in lines 3238, calculating the nonzero local contributions and adding
these contributions into the correct locations in the global residual vector and global
Jacobian matrix. Finally, the boundary conditions are handled by lines 4146.
The function CalculateResidualAndJacobianOnElement (lines
52102) calculates the local contribution to both the residual vector and Jacobian
matrix on each element, using Gaussian quadrature. This function follows the pattern
of the function CalculateResidualOnElement from Listing 5.1, but with the
addition of calculating the entries of the local Jacobian matrix given by Eq. (5.36) in
lines 9199 of the listing by using Gaussian quadrature.
This Matlab function may be called by typing, as in Sect. 5.1.5,
x = linspace(-1,2,101);
U0 = zeros(101,1);
U = Chap5_CalculateFemForNonlinearBVP_Jacobian(x, U0);

into a Matlab session. We encourage the reader to execute both Listings 5.1 and
5.2 for meshes containing different numbers of elements, and to record the execution
time for both functions. The reader will observe that specification of the Jacobian
matrix allows the finite element solution to be computed much more efficientlysee
Exercise 5.1.
94 5 Nonlinear Boundary Value Problems

Listing 5.2 Chap5_CalculateFemForNonlinearBVP_Jacobian.m, a Matlab


function for calculating the finite element solution of Eqs. (5.1)(5.3), specifying both the

residual vector and Jacobian matrix of the nonlinear system.
1 % S p e c i f y mesh x and i n i t i a l g u e s s U0
2 f u n c t i o n U = ...
3 C h a p 5 _ C a l c u l a t e F e m F o r N o n l i n e a r B V P _ J a c o b i a n (x , U0 )
4
5 % Use M a t l a b r o u t i n e to solve n o n l i n e a r a l g e b r a i c s y s t e m
6
7 % S p e c i f y that the J a c o b i a n will be s p e c i f i e d by the user
8 o p t i o n s = o p t i m o p t i o n s ( f s o l v e , J a c o b i a n , on );
9
10 U = f s o l v e ( @ C a l c u l a t e R e s i d u a l A n d J a c o b i a n , U0 , options , x );
11
12 % Plot s o l u t i o n
13 plot (x , U , - )
14 x l a b e l ( x )
15 y l a b e l ( U )
16
17
18
19 % F u n c t i o n for c a l c u l a t i n g g l o b a l r e s i d u a l and J a c o b i a n
20 f u n c t i o n [ Residual , J a c o b i a n ] = ...
21 CalculateResidualAndJacobian (U , x)
22
23 % D e d u c e n u m b e r of e l e m e n t s
24 N = l e n g t h ( x ) - 1;
25
26 % I n i t i a l i s e r e s i d u a l and J a c o b i a n to zero
27 R e s i d u a l = zeros ( N +1 , 1);
28 J a c o b i a n = zeros ( N +1 , N +1);
29
30 % Loop over elements , c o m p u t i n g local c o n t r i b u t i o n to
31 % r e s i d u a l and i n c r e m e n t i n g g l o b a l r e s i d u a l and J a c o b i a n
32 for k =1: N
33 [ L o c a l R e s i d u a l , L o c a l J a c o b i a n ] = ...
34 C a l c u l a t e R e s i d u a l A n d J a c o b i a n O n E l e m e n t ( U , x , k );
35 R e s i d u a l ( k : k +1) = R e s i d u a l ( k : k +1) + L o c a l R e s i d u a l ;
36 J a c o b i a n ( k : k +1 , k : k +1) = J a c o b i a n ( k : k +1 , k : k +1) + ...
37 LocalJacobian ;
38 end
39
40 % H a n d l e N e u m a n n b o u n d a r y c o n d i t i o n at x =2
41 R e s i d u a l ( N +1) = R e s i d u a l ( N +1) - 68;
42
43 % H a n d l e D i r i c h l e t b o u n d a r y c o n d i t i o n at x = -1
44 R e s i d u a l (1) = U (1) - 1;
45 J a c o b i a n (1 ,:) = 0;
46 J a c o b i a n (1 ,1) = 1;
47
48
49
50 % F u n c t i o n for c a l c u l a t i n g local c o n t r i b u t i o n to
51 % r e s i d u a l and J a c o b i a n
52 f u n c t i o n [ L o c a l R e s i d u a l , L o c a l J a c o b i a n ] = ...
53 CalculateResidualAndJacobianOnElement (U , x , k)
54
55 % Set e l e m e n t l e n g t h h
56 h = x ( k +1) - x ( k );
5.1 A First Example 95

57
58 % D e f i n e q u a d r a t u r e rule
59 M = 3;
60 Q u a d W e i g h t s = [5/18 , 4/9 , 5 / 1 8 ] ;
61 Q u a d P o i n t s = [0.5*(1 - sqrt (0.6)) , 0.5 , 0 . 5 * ( 1 + sqrt ( 0 . 6 ) ) ] ;
62
63 % I n i t i a l i s e local r e s i d u a l and J a c o b i a n to zero
64 L o c a l R e s i d u a l = zeros (2 ,1);
65 L o c a l J a c o b i a n = zeros (2 ,2);
66
67 % Loop over q u a d r a t u r e p o i n t s to e v a l u a t e i n t e g r a l
68 % using G a u s s i a n q u a d r a t u r e
69 for m =1: M
70 % Set local c o o r d i n a t e X
71 X = Q u a d P o i n t s ( m );
72
73 % C a l c u l a t e local values of basis functions ,
74 % their derivatives , x , U and the d e r i v a t i v e of U
75 p h i _ l o c a l = [1 - X , X ];
76 p h i _ p r i m e _ l o c a l = [ -1 , 1];
77 x_local = x(k) + h*X;
78 L o c a l U = U ( k )* p h i _ l o c a l (1) + U ( k +1)* p h i _ l o c a l (2);
79 L o c a l U P r i m e = U ( k )* p h i _ p r i m e _ l o c a l (1) + ...
80 U ( k +1)* p h i _ p r i m e _ l o c a l (2);
81
82 % E v a l u a t e i n t e g r a n d and add into sum
83 for i =1:2
84 L o c a l R e s i d u a l ( i ) = L o c a l R e s i d u a l ( i ) + ...
85 h * Q u a d W e i g h t s ( m )* ...
86 (1/ h / h * ( 1 + L o c a l U * L o c a l U )* ...
87 L o c a l U P r i m e * p h i _ p r i m e _ l o c a l ( i ) + ...
88 2 * ( 1 + x _ l o c a l ^4)* p h i _ l o c a l ( i ) + ...
89 8* L o c a l U * L o c a l U * p h i _ l o c a l ( i ));
90
91 for j =1:2
92 L o c a l J a c o b i a n (i , j ) = L o c a l J a c o b i a n ( i , j ) + ...
93 h * Q u a d W e i g h t s ( m )* ...
94 (1/ h / h * ( 2 * L o c a l U * p h i _ l o c a l ( j )* ...
95 L o c a l U P r i m e + ...
96 (1+ L o c a l U * L o c a l U )* p h i _ p r i m e _ l o c a l ( j ))* ...
97 p h i _ p r i m e _ l o c a l ( i ) + ...
98 16* L o c a l U * p h i _ l o c a l ( j )* p h i _ l o c a l ( i ));
99 end
100
101 end
102 end
96 5 Nonlinear Boundary Value Problems

5.1.7 Numerical Approximation of the Jacobian Matrix

In Sect. 5.1.6.1, we claimed that a nonlinear system of algebraic equations may be


solved far more efficiently by specifying, in addition to the residual vector, the Jaco-
bian matrix. This claim may be verified by completing Exercise 5.1 at the end of this
chapter. Calculating the Jacobian matrix does, however, require extra programming
to be undertaken. The example boundary value problem used in this chapter, defined
by Eqs. (5.1)(5.3), contains only terms that allow local contributions to the Jacobian
matrix, given by Eq. (5.36), to be explicitly calculated fairly easily. For more com-
plex differential equations, it may be very difficult to evaluate these contributions.
Even if the contributions are calculated, some verification of the calculation may be
useful. In both cases, a numerical approximation to the partial derivatives given by
Eq. (5.28), calculated using the finite difference method, may be useful. We now
explain how these partial derivatives may be estimated efficiently.
In Listing 5.1the implementation that does not specify the Jacobian matrix
we wrote a function CalculateResidual that calculated the global residual. It
may be tempting to calculate the global Jacobian matrix by using this function to
estimate the partial derivatives needed, by using a finite difference approximation.
To calculate the required partial derivatives with respect to U j , we could use this
function to calculate both R(U) and R(U + e j ), where e j is a vector of the same
size as U, with entry e j = 1, and all other entries taking the value zero. The partial
derivatives with respect to U j , i.e. the entries of column j of the Jacobian matrix,
may then approximated by

Ri (U + e j ) Ri (U)
Ji, j ,

where takes a sufficiently small value for the approximation to the derivative to
be accurate. Performing this for all columns j will indeed generate a numerical
approximation to the Jacobian matrix. This approach is, however, very inefficient.
To evaluate the partial derivatives in this way, we would first need to compute the
original residual. We would then need to compute N + 1 perturbed residuals to
approximate the partial derivatives with respect to each U j , j = 1, 2, . . . , N + 1,
as demonstrated above. This would require the function CalculateResidual to
be evaluated a total of N + 2 times.
A more efficient approach to approximating the Jacobian matrix is to calculate
an approximation to the partial derivatives that appear in the local contribution to
the Jacobian matrix from an individual element, given by Eq. (5.35). These partial
derivatives may easily be approximated by the finite difference method, using the
function CalculateResidualOnElement given in Listing 5.1. Taking this
approach, we may assemble the Jacobian matrix as described in Sect. 5.1.6, using a
numerical approximation to Eq. (5.35). We then supply both the residual vector and
Jacobian matrix to the Matlab function fsolve as in Listing 5.2.
5.2 A General Nonlinear Boundary Value Problem 97

5.2 A General Nonlinear Boundary Value Problem

We now discuss how the material that has been used to calculate the finite element
solution of the example nonlinear differential equation, given by Eqs. (5.1)(5.3),
may be applied to more general nonlinear boundary value problems. The most general
second-order, nonlinear, differential equation satisfied by a function u(x) on the
interval a < x < b is of the form
 
du d2 u
f x, u, , 2 = 0, a < x < b,
dx dx
 
du d2 u
for some given function f x, u, dx , dx 2 , combined with suitable boundary condi-
tions. Throughout this book, the first step when calculating the finite element solution
of a differential equation has always been to write the differential equation in weak
form. To do this, we have multiplied the differential equation by a suitable test
function and used integration by parts to eliminate the dependence on the second
derivative of u(x). Writing the equation in the general form above does not always
allow us to integrate by parts. Hence, we restrict ourselves to differential equations
that may be written in the form
    
d du du
p x, u, + r x, u, = 0, a < x < b, (5.37)
dx dx dx
du
 du

for specified functions p x, u, dx , r x, u, dx . Although this is not the most general
possible differential equation, most practical applications can be written in this form.
We require suitable boundary conditions to completely specify the problem. As with
the example given earlier in this chapter, we shall impose one Dirichlet boundary
condition and one natural Neumann boundary condition:

u = ua , x = a, (5.38)
 
du
p x, u, = pb , x = b. (5.39)
dx

We now give an overview of how the finite element solution of Eq. (5.37), subject to
the boundary conditions given by Eqs. (5.38) and (5.39), may be calculated.

5.2.1 The Weak Formulation

The weak formulation is obtained in the usual way: we multiply the differential
equation, given by Eq. (5.37), by a function v(x) H01 (a, b), and integrate this
product over the interval a < x < b:
98 5 Nonlinear Boundary Value Problems
 b      
d du du
p x, u, + r x, u, v(x) dx = 0.
a dx dx dx

Integrating by parts then gives


   b  b    
du du dv du
p x, u, v(x) + p x, u, + r x, u, v(x) dx = 0.
dx a a dx dx dx
(5.40)

As v(x) H01 (a, b), we have v(a) = 0. The Neumann boundary condition given by
Eq. (5.39), together with v(a) = 0, allows us to write Eq. (5.40) as
 b    
du dv du
p x, u, + r x, u, v(x) dx pb v(b) = 0. (5.41)
a dx dx dx

The weak solution of Eqs. (5.37)(5.39) is then defined by:

find u(x) HE1 (a, b) such that


 b    
du dv du
p x, u, + r x, u, v(x) dx pb v(b) = 0, (5.42)
a dx dx dx

for all v(x) H01 (a, b).

5.2.2 The Nonlinear System of Algebraic Equations

We use the same mesh, basis functions and sets S h , S Eh , S0h that were used for the
example problem that we considered earlier in this chapter. The finite element solu-
tion, U (x), may then be written as

N +1

U (x) = U j j (x).
j=1

The coefficients of the basis functions in the expression above, U1 , U2 , . . . , N N +1 ,


are then determined by specifying the finite element solution by:

find U (x) S Eh such that


 b    
dU di dU
p x, U, + r x, U, i (x) dx pb i (b) = 0, (5.43)
a dx dx dx

for all i (x) S0h .


5.2 A General Nonlinear Boundary Value Problem 99

As U (x) S Eh , the finite element solution must satisfy any Dirichlet bound-
ary conditions that have been specified. We have one Dirichlet boundary condition,
specified at the node where x = x1 , and given by Eq. (5.38). Remembering that
U (x1 ) = U1 , we may deduce our first algebraic equation:

U1 = u a . (5.44)

The functions i (x), i = 2, 3, . . . , N + 1, all satisfy i (a) = 0 and are therefore


members of the set S0h . We may then substitute these basis functions into Eq. (5.43)
to deduce that
 b    
dU di dU
p x, U, + r x, U, i (x) pb i (b) dx = 0,
a dx dx dx
i = 2, 3, . . . , N + 1.
(5.45)

The final term on the left-hand side of Eq. (5.45) may be simplified by remembering
that

i (b) = i (x N +1 )

1, i = N + 1,
=
0, i = N + 1.

The algebraic equations given by Eqs. (5.44) and (5.45) may now be written in
the form

R(U) = 0,

where the entries of R are given by

R 1 = U1 u a ,
N  xk+1    
dU di du
Ri = p x, U, + r x, u, i (x) dx, i = 2, 3, . . . , N ,
xk dx dx dx
k=1
N 
 xk+1    
dU d N +1 du
R N +1 = p x, U, + r x, u, N +1 (x) dx pb .
dx dx dx
k=1 xk

These residuals may then be calculated, as described for the example differen-
tial equation in Sect. 5.1.4. The Jacobian matrix may, if required, be calculated as
described in Sect. 5.1.6.
100 5 Nonlinear Boundary Value Problems

5.3 Exercises

5.1 Listing 5.1 contains a computational implementation for calculating the finite
element solution of the boundary value problem given by Eqs. (5.1)(5.3), specifying
only the residual vector given by Eq. (5.18). Listing 5.2 also solves this boundary
value problem, specifying both the residual vector and the Jacobian matrix given
by Eq. (5.28). Execute the functions given by both Listings 5.1 and 5.2 for differ-
ent numbers of elements: try, for example, N = 50, 100, 150, 200, . . .. Record the
execution time for both functions for all values of N that you use. How much more
efficient is it to specify the global Jacobian matrix in addition to the global residual
vector?

5.2 Using Listings 5.1 and 5.2, write a computational implementation that calculates
the finite element solution of the boundary value problem given by Eqs. (5.1)(5.3),
using a numerical approximation to the Jacobian matrix as described in Sect. 5.1.7.

5.3 In this exercise, we will work with the differential equation


 
d du
u + 2u 2 = 0, 0 < x < 1,
dx dx

subject to the Dirichlet boundary conditions

u = 1, at x = 0,
u = e, at x = 1.

This equation has solution u(x) = ex .

a. Write down the weak formulation of the given differential equation. State clearly
the definition of the sets HE1 (0, 1) and H01 (0, 1) that you will require.
b. Calculate a finite element solution to the given differential equation, using a
linear approximation to the solution on each element. You may want to start
by using the function Matlab fsolve, specifying only the residual vector
as in Listing 5.1. When you are confident that this implementation is working
correctly, develop your code so that it also specifies the Jacobian matrix, either
calculated analytically or approximated numerically.
c. Repeat part (b) using a quadratic approximation to the solution on each element.

5.4 In this exercise, we will work with the differential equation


  2 
d du du
e u
+ (1 u 2 )eu cos x 2eu u = 0, 0<x< ,
dx dx dx 2

subject to a Neumann boundary condition at x = 0, and a Dirichlet boundary con-


dition at x = 2 :
5.3 Exercises 101
 2
du
e u
= 1, at x = 0,
dx

u = 1, at x = .
2
This equation has solution u(x) = sin x.
Repeat the subparts of Exercise 5.3 for this differential equation.
Chapter 6
Systems of Ordinary Differential Equations

So far we have only considered scalar boundary value problems, that is, a single
differential equation that contains one unknown function u(x). We now generalise
the finite element method so that it may be applied to coupled systems of differ-
ential equations. We illustrate the basic principles using a simple example, before
explaining how these ideas may be generalised for application to more complex
systems.

6.1 A Model Problem

A model system of differential equations is, for 0 < x < ,

d2 u 1 d2 u 2
3 u 1 + 8u 2 = 20 sin 2x, (6.1)
dx 2 dx 2
d2 u 2
2 + 2u 1 4u 2 = 2 sin x. (6.2)
dx
As with scalar equations, we require suitable boundary conditions to be specified.
In general, we expect one boundary condition for each equation at x = 0, and one
boundary condition for each equation at x = . As with scalar differential equations,
these boundary conditions may be any combination of Dirichlet, Neumann and Robin
boundary conditions, subject to constraints on the existence and uniqueness of the
solution. The boundary conditions that we use to illustrate the application of the finite
element method to the model system of equations above are, at x = 0:

du 1 du 2
3 = 7, (6.3)
dx dx
u 2 = 0, (6.4)
Springer International Publishing AG 2017 103
J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_6
104 6 Systems of Ordinary Differential Equations

and at x = , we impose

u 1 = 0, (6.5)
du 2
= 2. (6.6)
dx
We shall see later that the Neumann boundary condition given by Eq. (6.3) is a natural
boundary condition for Eq. (6.1), and the Neumann boundary condition given by
Eq. (6.6) is a natural boundary condition for Eq. (6.2). The solution of this model
problem is

u 1 (x) = sin x, u 2 (x) = sin 2x.

6.2 The Weak Formulation

The method for deriving the weak formulation in earlier chapters has been to multiply
the differential equation by a suitable test function and to integrate this product over
the region on which the differential equation is defined. This is easily generalised for
systems of equations such as Eqs. (6.1) and (6.2). We multiply the first differential
equation, Eq. (6.1), by a suitable test function v1 (x), and the second differential
equation, Eq. (6.2), by a suitable test function v2 (x). We must, however, choose
suitable test functions.
We have already claimed that Eq. (6.3) is a natural Neumann boundary condition
at x = 0 for the first differential equation, Eq. (6.1). Further, Eq. (6.5) may be used
as a Dirichlet boundary condition for Eq. (6.1) at x = . We then view Eq. (6.1),
subject to the boundary conditions given by Eqs. (6.3) and (6.5), as a boundary value
problem that satisfies a Neumann boundary condition at x = 0, and a Dirichlet
boundary condition at x = . We therefore specify v1 to be a member of the set
H 1 (0, ) that satisfies the condition

v1 ( ) = 0, (6.7)

so that v1 (x) = 0 at values of x where Dirichlet boundary conditions are applied to


the first differential equation, given by Eq. (6.1).
Similarly, we will solve the second differential equation, Eq. (6.2), subject to the
Dirichlet boundary condition given by Eq. (6.4) at x = 0, and the Neumann boundary
condition given by Eq. (6.6) at x = . Using these boundary conditions, we see that
an appropriate test function v2 (x) belongs to the set H 1 (0, ) and also satisfies

v2 (0) = 0. (6.8)

We now multiply Eq. (6.1) by the test function v1 (x) defined above and integrate
the resulting product over 0 < x < . After integrating by parts, we obtain
6.2 The Weak Formulation 105
    
du 1 du 2 du 1 dv1 du 2 dv1
+3 v1 + +3 u 1 v1 + 8u 2 v1 dx =
dx dx 0 dx dx dx dx
0

20v1 sin 2x dx.
0

Applying the Neumann boundary condition given by Eq. (6.3) at x = 0, and the
condition on v1 given by Eq. (6.7) allows us to write the equation above as
 
du 1 dv1 du 2 dv1
+3 u 1 v1 + 8u 2 v1 dx = 20v1 sin 2x dx 7v1 (0).
0 dx dx dx dx 0

In the same spirit, we: (i) multiply Eq. (6.2) by the test function v2 (x); (ii) integrate
the resulting product over 0 < x < ; (iii) integrate by parts; (iv) apply the Neumann
boundary condition given by Eq. (6.6) at x = ; and (v) apply the condition on v2 (x)
given by Eq. (6.8). We then obtain
 
du 2 dv2
+ 2u 1 v2 4u 2 v2 dx = 2v2 sin x dx + 2v2 ( ).
0 dx dx 0

All that is now required to complete the weak formulation of the problem is to
demand that u 1 (x) satisfies the Dirichlet boundary condition given by Eq. (6.5) and
that u 2 (x) satisfies the Dirichlet boundary condition given by Eq. (6.4). We may then
write the weak formulation of Eqs. (6.1) and (6.2), subject to the boundary conditions
given by Eqs. (6.3)(6.6) as:
find u 1 (x), u 2 (x) HE1 (0, ) such that
 
du 1 dv1 du 2 dv1
+3 u 1 v1 + 8u 2 v1 dx = 20v1 sin 2x dx 7v1 (0), (6.9)
0 dx dx dx dx
 0
du 2 dv2
+ 2u 1 v2 4u 2 v2 dx = 2v2 sin x dx + 2v2 ( ). (6.10)
0 dx dx 0

for all v1 (x), v2 (x) H01 (0, ).

6.3 The Mesh and Basis Functions

We will calculate a linear approximation to both u 1 (x) and u 2 (x) on each element.
As such, we will use the mesh of N elements described in Sect. 3.3.1, and the linear
basis functions described in Sect. 3.3.2.
106 6 Systems of Ordinary Differential Equations

6.4 The Finite Element Solution

Before we define the finite element solution, we need to specify the set S h , and the
subsets S Eh and S0h of this set. The set S h is defined to be the set of all functions
V (1) (x), V (2) (x) that may be written as linear sums of the basis functions:

N +1

V (1)
(x) = V j(1) j (x),
j=1
N +1

V (2) (x) = V j(2) j (x).
j=1

The set S Eh is, as usual, defined to be the subset of S h that contains functions that
satisfy the Dirichlet boundary conditions. On using Eqs. (6.4) and (6.5), we see that
these are the functions V (1) (x) and V (2) (x) that satisfy

V (1) ( ) = 0, V (2) (0) = 0.

The set S0h is the subset of S h that contains functions that take the value zero where
Dirichlet boundary conditions are applied. As our model problem is the special case
where the Dirichlet boundary conditions, given by Eqs. (6.4) and (6.5), set the solution
to zero, the set S0h is identical to the set S Eh .
We now define the finite element solution by
Find U (1) (x), U (2) (x) S Eh such that

dU (1) dV (1) dU (2) dV (1)
+3 U (1) V (1) + 8U (2) V (1) dx =
dx dx dx dx
0

20V (1) sin 2x dx 7V (1) (0),
0
(6.11)


dU (2) dV (2)
+ 2U (1) V (2) 4U (2) V (2) dx =
dx dx
0

2V (2) sin x dx + 2V (2) ( ). (6.12)
0

for all V (1) (x), V (2) (x) S0h .


Note the small change in the convention for naming the finite element solutions
it might be thought that these finite element solutions would be named U1 (x) and
U2 (X ). We use superscripts, rather than subscripts, for these finite element solutions
so that, for example, U (1) is not confused with U1 , the first entry of the vector U.
6.5 The Algebraic Equations 107

6.5 The Algebraic Equations

We write the finite element solutions, U (1) (x) and U (2) (x), as members of the set S h
given by

N +1

U (1)
(x) = U (1)
j j (x), (6.13)
j=1
N +1

U (2) (x) = U (2)
j j (x), (6.14)
j=1

where U (1) (2)


j , U j , j = 1, 2, . . . , N + 1, are to be determined. As usual, the system of
algebraic equations contains both: (i) equations that ensure that U (1) (x) and U (2) (x)
satisfy the Dirichlet boundary conditions and are therefore members of the set S Eh ; and
(ii) equations that arise through substituting suitable test functions into Eqs. (6.11)
and (6.12).
The Dirichlet boundary conditions, given by Eqs. (6.4) and (6.5), require that

U (1) ( ) = 0, U (2) (0) = 0.

We note that x N +1 = and x1 = 0. From the definition of the finite element solutions
given by Eqs. (6.13) and (6.14), we see that these boundary conditions are satisfied
if

U N(1)+1 = 0, (6.15)
U1(2) = 0. (6.16)

Eqs. (6.15) and (6.16) then specify our first two algebraic equations.
Test functions V (1) (x) for Eq. (6.11) must satisfy V (1) (x N +1 ) = 0 so that they lie
in the space S0h . The N basis functions i (x), i = 1, 2, . . . , N , satisfy this condition
and are therefore suitable candidates. Substituting V (1) (x) = i (x), i = 1, 2, . . . , N ,
into Eq. (6.11), together with the finite element solutions given by Eqs. (6.13) and
(6.14), yields, after some manipulation,

N +1 
 
di d j
i j dx U (1)
j +
j=1 0 dx dx
N +1 
  
di d j
3 + 8i j dx U (2)
j = 20i sin 2x dx 7i (x1 ).
j=1 0 dx dx 0

This may be written


108 6 Systems of Ordinary Differential Equations

N +1
 N +1

Ai(11) (1)
j Uj + Ai(12) (2) (1)
j U j = bi , i = 1, 2, . . . , N , (6.17)
j=1 j=1

where for i = 1, 2, . . . , N , j = 1, 2, . . . , N + 1,

di d j
Ai(11)
j = i j dx, (6.18)
dx dx
0
di d j
Ai(12)
j = 3 + 8i j dx, (6.19)
0 dx dx

and

b1(1) = 201 sin 2x dx 7, (6.20)
0
bi(1) = 20i sin 2x dx, i = 2, 3, . . . , N . (6.21)
0

We now combine Eqs. (6.15) and (6.17) into the single matrix equation

A(11) U(1) + A(12) U(2) = b(1) , (6.22)

where the entries of A(11) , A(12) , b(1) are given by Eqs. (6.18)(6.21), together with

1, j = N + 1,
A(11)
N +1, j = (6.23)
0, j = N + 1,
A(12)
N +1, j = 0, j = 1, 2, . . . , N + 1, (6.24)
b(1)
N +1 = 0. (6.25)

We now consider the integral given by Eq. (6.12). Suitable test functions will
satisfy V (2) (x1 ) = 0, and this condition is satisfied by the N basis functions i (x),
i = 2, 3, . . . , N + 1. Substituting these basis functions, and Eqs. (6.13) and (6.14),
into Eq. (6.12) we obtain, in a similar fashion to above:

N +1
 N +1

Ai(21) (1)
j Uj + Ai(22) (2) (2)
j U j = bi , i = 2, 3, . . . , N + 1, (6.26)
j=1 j=1

where for i = 2, 3, . . . , N + 1, j = 1, 2, . . . , N + 1,

Ai(21)
j = 2i j dx, (6.27)
0
di d j
Ai(22)
j = 4i j dx, (6.28)
0 dx dx
6.5 The Algebraic Equations 109

and

bi(2) = 2i sin x dx, i = 2, 3, . . . , N , (6.29)
0
b(2)
N +1 = 2 N +1 sin x dx + 2. (6.30)
0

As before, Eqs. (6.16) and (6.26) may be written as the single equation

A(21) U(1) + A(22) U(2) = b(2) , (6.31)

where the entries of the A(21) , A(22) , b(2) are given by Eqs. (6.27)(6.30), together
with

A(21)
1, j = 0, j = 1, 2, . . . , N + 1, (6.32)

1, j = 1,
A(22) = (6.33)
1, j 0, j = 1,
b1(2) = 0. (6.34)

Finally, we may combine Eqs. (6.22) and (6.31) into the single matrix equation

AU = b, (6.35)

where
     
A(11) A(12) U(1) b(1)
A= , U= , b= . (6.36)
A(21) A(22) U(2) b(2)

The values U1(1) , U2(1) , . . . , U N(1)+1 and U1(2) , U2(2) , . . . , U N(2)+1 that specify the finite
element solution for U (1) (x) and U (2) (x) given by Eqs. (6.13) and (6.14) may then
be obtained by solving the linear system given by Eq. (6.35).

6.6 Assembling the Algebraic Equations

We will assemble the global matrix A and global vector b that appear in the linear
system Eq. (6.35) by assembling the submatrices and subvectors that A and b are
comprised of, as shown in Eq. (6.36).
We start by assembling the matrix A(11) that is defined by Eqs. (6.18) and (6.23).
We see that these entries take the same form as those calculated in earlier chapters:
most entries (those defined by Eq. (6.18)) take the form of integrals of products
of basis functions and their derivatives over the whole domain; other entries (those
defined by Eq. (6.23)) take the form of very simple, explicit numerical values. We
110 6 Systems of Ordinary Differential Equations

may therefore assemble A(11) using the techniques developed in earlier chapters. To
do this, we first evaluate the entries that are defined by integrals, i.e. those given
by Eq. (6.18). These are assembled by decomposing these integrals into the sum of
integrals over individual elements. We loop over all elements, calculating only the
nonzero local contributions to the entries of A(11) from each element, before adding
these local contributions into A(11) . Having done this, we then complete the assembly
of this matrix by setting the entries defined by Eq. (6.23).
The other matrices A(12) , A(21) , A(22) may be assembled using the same approach
to that described above for assembling A(11) . Finally, the vectors b(1) and b(2) may
also be assembled in a similar manner: we do, however, have an additional term in
Eqs. (6.20) and (6.30) to add into these vectors that arise from the Neumann boundary
conditions.

6.7 Computational Implementation

We now develop an example Matlab implementation, given in Listing 6.1, of the


material presented in this chapter. The function written requires that the mesh is
specified by a user-supplied vector x. We require that this mesh is consistent with
the mesh used in this chapter. As such, if there are N elements in the mesh, then
the vector x must be of length N + 1 with: (i) the first entry equal to zero, (ii) the
last entry equal to and (iii) the entries of the vector ordered in ascending order.
This ensures that the vector x is a suitable partitioning of the domain 0 < x < .
The function returns the vectors corresponding to U(1) and U(2) . This function can
be called by typing, for example,
x = linspace(0, pi, 101);
[U1, U2] = Chap6_CalculateFemForSystems(x);

into a Matlab session.


As already noted, the vector x will be of length N + 1, where N is the number
of elements. The length of x is used to deduce the number of elements in line 4 of
the listing. The matrices A(11) , A(12) , A(21) , A(22) , and the vectors b(1) , b(2) are then
initialised to zero, and to be of the correct size, in lines 712.
In lines 1628, we loop over all elements, calculating the local contributions to
A(11) , A(12) , A(21) , A(22) , b(1) , b(2) , and then incrementing these global matrices and
vectors appropriately.
The function used to calculate the local entries is given in lines 66101. In this
function, we first calculate the length of each element in line 71. We calculate the
local contributions to A(11) , A(12) , A(21) , A(22) analytically in lines 7479. We then
define a quadrature rule in lines 8284 that we use to calculate the entries of b(1) ,
b(2) in lines 92101.
Returning to the main body of code, we handle the Neumann boundary conditions
in lines 31 and 34 and the Dirichlet boundary conditions in lines 3746. We then
form the global matrixA in line 49, and the global vector b in line 50, before solving
6.7 Computational Implementation 111

the equation in line 53. The vectors U1 and U2 are extracted from the solution of the
linear system in lines 56 and 57. Finally, in lines 6062, we plot the finite element
solution given by both U (1) (x) and U (2) (x).

Listing 6.1 Chap6_CalculateFemForSystems.m, a Matlab function for calculat-


ing the finite element solution of the system of differential equations given by Eqs. (6.1)(6.6).
1 f u n c t i o n [ U1 , U2 ] = C h a p 6 _ C a l c u l a t e F e m F o r S y s t e m s ( x )
2
3 % D e d u c e the n u m b e r of e l e m e n t s
4 N = l e n g t h ( x ) - 1;
5
6 % I n i t i a l i s e A_11 , A_12 , A_21 , A_22 , b_1 , b_2 to zero
7 A_11 = zeros ( N +1 , N +1);
8 A_12 = zeros ( N +1 , N +1);
9 A_21 = zeros ( N +1 , N +1);
10 A_22 = zeros ( N +1 , N +1);
11 b_1 = zeros ( N +1 , 1);
12 b_2 = zeros ( N +1 , 1);
13
14 % Loop over elements , c a l c u l a t i n g n o n z e r o local
15 % c o n t r i b u t i o n s and i n s e r t i n g them into linear system
16 for k =1: N
17 [ A_local_11 , A_local_12 , A_local_21 , ...
18 A_local_22 , b_local_1 , b _ l o c a l _ 2 ] = ...
19 C a l c u l a t e C o n t r i b u t i o n O n E l e m e n t ( x , k );
20
21 A_11 ( k : k +1 , k : k +1) = A_11 ( k : k +1 , k : k +1) + A_local_11 ;
22 A_12 ( k : k +1 , k : k +1) = A_12 ( k : k +1 , k : k +1) + A_local_12 ;
23 A_21 ( k : k +1 , k : k +1) = A_21 ( k : k +1 , k : k +1) + A_local_21 ;
24 A_22 ( k : k +1 , k : k +1) = A_22 ( k : k +1 , k : k +1) + A_local_22 ;
25
26 b_1 ( k : k +1) = b_1 ( k : k +1) + b _ l o c a l _ 1 ;
27 b_2 ( k : k +1) = b_2 ( k : k +1) + b _ l o c a l _ 2 ;
28 end
29
30 % H a n d l e N e u m a n n b o u n d a r y c o n d i t i o n at x =0
31 b_1 (1) = b_1 (1) - 7;
32
33 % H a n d l e N e u m a n n b o u n d a r y c o n d i t i o n at x = pi
34 b_2 ( N +1) = b_2 ( N +1) + 2;
35
36 % H a n d l e D i r i c h l e t b o u n d a r y c o n d i t i o n at x = pi
37 A_11 ( N +1 ,:) = 0;
38 A_11 ( N +1 , N +1) = 1;
39 A_12 ( N +1 ,:) = 0;
40 b_1 ( N +1) = 0;
41
42 % H a n d l e D i r i c h l e t b o u n d a r y c o n d i t i o n at x =0
43 A_21 (1 ,:) = 0;
44 A_22 (1 ,:) = 0;
45 A_22 (1 ,1) = 1;
46 b_2 (1) = 0;
47
48 % Form global m a t r i x A and v e c t o r b
49 A = [ A_11 , A_12 ; A_21 , A_22 ];
50 b = [ b_1 ; b_2 ];
51
52 % Solve linear system
53 U = A\b;
54
55 % D e c o m p o s e the s o l u t i o n U into U_1 and U_2
112 6 Systems of Ordinary Differential Equations

56 U1 = U (1: N +1);
57 U2 = U ( N +2:2* N +2);
58
59 % Plot the s o l u t i o n
60 plot ( x , U1 , r - ,x , U2 , k - )
61 xlabel ( x )
62 l e g e n d ( U_1 , U_2 )
63
64
65
66 f u n c t i o n [ A_local_11 , A_local_12 , A_local_21 , ...
67 A_local_22 , b_local_1 , b _ l o c a l _ 2 ] = ...
68 C a l c u l a t e C o n t r i b u t i o n O n E l e m e n t (x , k )
69
70 % C a l c u l a t e l e n g t h of e l e m e n t
71 h = x ( k +1) - x ( k );
72
73 % C a l c u l a t e a n a l y t i c a l c o n t r i b u t i o n s to A
74 A _ l o c a l _ 1 1 = [1/ h , -1/ h ; -1/ h , 1/ h ] - [ h /3 , h /6; h /6 , h /3];
75 A _ l o c a l _ 1 2 = 3*[1/ h , -1/ h ; -1/ h , 1/ h ] + ...
76 8*[ h /3 , h /6; h /6 , h /3];
77 A _ l o c a l _ 2 1 = 2*[ h /3 , h /6; h /6 , h /3];
78 A _ l o c a l _ 2 2 = [1/ h , -1/ h ; -1/ h , 1/ h ] - ...
79 4*[ h /3 , h /6; h /6 , h /3];
80
81 % D e f i n e q u a d r a t u r e rule
82 M = 3;
83 Q u a d W e i g h t s = [5/18 , 4/9 , 5 / 1 8 ] ;
84 Q u a d P o i n t s = [0.5*(1 - sqrt (0.6)) , 0.5 , 0 . 5 * ( 1 + sqrt ( 0 . 6 ) ) ] ;
85
86 % I n i t i a l i s e local c o n t r i b u t i o n s to b to zero
87 b _ l o c a l _ 1 = zeros (2 ,1);
88 b _ l o c a l _ 2 = zeros (2 ,1);
89
90 % Loop over q u a d r a t u r e p o i n t s to e v a l u a t e local
91 % c o n t r i b u t i o n s to b
92 for m = 1: M
93 X = Q u a d P o i n t s ( m );
94 p h i _ l o c a l = [1 - X , X ];
95 for i =1:2
96 b _ l o c a l _ 1 ( i ) = b _ l o c a l _ 1 ( i ) + h * Q u a d W e i g h t s ( m )* ...
97 20* sin (2*( x ( k )+ h * X ))* p h i _ l o c a l ( i );
98 b _ l o c a l _ 2 ( i ) = b _ l o c a l _ 2 ( i ) + h * Q u a d W e i g h t s ( m )* ...
99 2* sin ( x ( k )+ h * X )* p h i _ l o c a l ( i );
100 end
101 end

6.8 More General Linear Problems

We now give a brief summary of how to calculate the finite element solution of a
system of Neq ordinary differential equations. Suppose the unknown functions are
u 1 (x), u 2 (x), . . . , u Neq (x). Using vector notation, we may write
6.8 More General Linear Problems 113


du 1 d2 u 1
u 1 (x) dx
dx 2

u 2 (x) du d2 u d2 u 2
du 2

u(x) = ,
.. = .

,
dx
= . .dx 2

. dx .. dx 2 ..

u Neq (x) du Neq d2 u Neq
dx dx 2

A general linear, second-order system of differential equations may then be written,


for a < x < b, as
 
d du du
P(x) + Q(x) + R(x)u = f(x), (6.37)
dx dx dx

where P(x), Q(x), R(x) are matrices of size Neq Neq , and f is a vector of length Neq .
Alternatively, we may write Eq. (6.37) in component form as, for m = 1, 2, . . . , Neq ,
a < x < b,

   
du n 
Neq Neq Neq
d du n
Pmn (x) + Q mn (x) + Rmn (x)u n = f m (x). (6.38)
n=1
dx dx n=1
dx n=1

We assume that each equation has one boundary condition prescribed at x = a, and
one boundary condition prescribed at x = b.

6.8.1 The Weak Formulation

The boundary conditions for the system of differential equations given by Eq. (6.38)
allow us to define the Sobolev spaces HE1 (a, b) and H01 (a, b) for this system of equa-
tions. Multiplying each component m = 1, 2, . . . , Neq of the differential equation
by a suitable test function vm (x), integrating the resulting product over the domain
a < x < b and integrating the first term by parts, we may then write the weak
formulation of the differential equation as:
find u 1 (x), u 2 (x), . . . , u Neq (x) HE1 (a, b) such that, for m = 1, 2, . . . , Neq :

 Neq 
b 
du n dvm du n
Pmn + Q mn vm + Rmn u n vm dx =
a n=1
dx dx dx
 b
f m vm dx + h m (vm (a), vm (b)),
a

for all vm (x) H01 (a, b),


where h m (vm (a), vm (b)) are the contributions from any Neumann boundary condi-
tions satisfied by component m of the differential equation.
114 6 Systems of Ordinary Differential Equations

6.8.2 The Finite Element Solution

After specifying a suitable mesh, and defining basis functions on this mesh, we
define S h to be the set of vector-valued functions, with Neq components, where each
component may be written as a linear sum of these basis functions, that is,


N node

V (m)
(x) = V j(m) j (x), m = 1, 2, . . . , Neq , (6.39)
j=1

where Nnode is the number of nodes in the mesh. The sets S Eh and S0h are then subsets
of S h , with definitions that depend on the boundary conditions used. Having defined
the sets S Eh and S0h , we then define the finite element solution by:
find U (1) (x), U (2) (x), . . . , U (Neq ) (x) S Eh such that, for m = 1, 2, . . . , Neq :

 Neq 
b 
dU (n) dV (m) dU (n) (m) (n) (m)
Pmn + Q mn V + Rmn U V dx =
a n=1
dx dx dx
 b
f m V (m) dx + h m (V (m) (a), V (m) (b)),
a
(6.40)

for all V (m) (x) S0h .


We now write the finite element solution as a member of S h :


N node

U (m) (x) = U (m)


j j (x), m = 1, 2, . . . , Neq . (6.41)
j=1

As usual, for each differential equation, a system of Nnode algebraic equations may
be derived by: (i) ensuring that U (m) (x) S Eh ; and (ii) substituting V (m) = i (x)
into Eq. (6.40) for suitable test functions i (x) S0h . Using a similar approach to
that described in Sect. 6.5, this results in a linear system that may be written in the
form AU = b where
(11)
A A(12) . . . A(1,Neq )
A(21) A(22) . . . A(2,Neq )

A= . .. .. .. ,
.. . . .
A(Neq ,1) A(Neq ,2) . . . A(Neq ,,Neq )

U(1)
U(2)

U = . ,
..
U(Neq )
6.8 More General Linear Problems 115

b(1)

b(2)

b= ,
..
.
b(Neq )

We then solve the linear system AU = b to evaluate the coefficients in the finite
element solution given by Eq. (6.41).

6.9 Nonlinear Problems

We conclude this chapter with a brief summary of how the material in Sects. 6.16.7
may be adapted for nonlinear boundary value problems. Suppose we are solving the
nonlinear system defined by, for a < x < b,
    
d du 1 du 2 du 1 du 2
p1 x, u 1 , u 2 , , + r1 x, u 1 , u 2 , , = 0,
dx dx dx dx dx
    
d du 1 du 2 du 1 du 2
p2 x, u 1 , u 2 , , + r2 x, u 1 , u 2 , , = 0,
dx dx dx dx dx

subject to suitable prescribed boundary conditions. Using the usual arguments, we


find that the weak formulation is given by:

find u 1 (x), u 2 (x) HE1 (a, b) such that


 b    
du 1 du 2 dv1 du 1 du 2
p1 x, u 1 , u 2 , , + r1 x, u 1 , u 2 , , v1 (x) dx+
a dx dx dx dx dx
h 1 (v1 (a), v1 (b)) = 0,

 b    
du 1 du 2 dv2 du 1 du 2
p2 x, u 1 , u 2 , , + r2 x, u 1 , u 2 , , v2 (x) dx+
a dx dx dx dx dx
h 2 (v2 (a), v2 (b)) = 0,

for all v1 (x), v2 (x) H01 (a, b),

where HE1 (a, b) and H01 (a, b) are defined using the boundary conditions for each
equation, and h 1 (v1 (a), v1 (b)) and h 2 (v2 (a), v2 (b)) represent the contributions, if
any, from Neumann boundary conditions.
Defining the computational mesh allows us to specify suitable basis functions
and, in turn, the sets S Eh and S0h . We then write the finite element solutions as the
members of S h given by:
116 6 Systems of Ordinary Differential Equations


N node 
N node

U (1) (x) = U (1)


j j (x), U (2) (x) = U (2)
j j (x),
j=1 j=1

where Nnode is the number of nodes in the mesh. The finite element solution is then
defined by:

find U (1) (x), U (2) (x) S Eh such that


  
b
(1) dU (1) dU (2) dV (1)
(2)
p1 x, U , U , , +
a dx dx dx
 (1) 
(1) (2) dU dU (2)
r1 x, U , U , , V (1) (x) dx+
dx dx
h 1 (V (1) (a), V (1) (b)) = 0,
(6.42)

  
b
dU (1) dU (2) dV (2)
p2 x, U (1) , U (2) , , +
a dx dx dx
 (1) 
(1) (2) dU dU (2)
r2 x, U , U , , V (2) (x) dx+
dx dx
h 2 (V (2) (a), V (2) (b)) = 0,
(6.43)

for all V (1) (x), V (2) (x) S0h .

Substituting i (x) into Eq. (6.42) for all permissible i (x), when combined with
enforcing U (1) (x) S Eh , will result in Nnode algebraic equations, which we write as
 
R(1) U1(1) , U2(1) , . . . , U N(1)node , U1(2) , U2(2) , . . . , U N(2)node = 0. (6.44)

Similarly, substituting i (x) into Eq. (6.43) for all permissible i (x) S0h , and
enforcing U (2) (x) S Eh , will result in a further Nnode algebraic equations,
 
R(2) U1(1) , U2(1) , . . . , U N(1)node , U1(2) , U2(2) , . . . , U N(2)node = 0. (6.45)

The residuals R(1) and R(2) may be calculated in the same way as the residuals for
the scalar value nonlinear differential equations discussed in Chap. 5.
The two systems of nonlinear algebraic equations defined by Eqs. (6.44) and
(6.45) may then be combined into a single system of nonlinear algebraic equations
given by:
6.9 Nonlinear Problems 117
 (1) 
R
R = 0, where R= .
R(1)

Provided the Jacobian matrix is not required, this residual vector may be used to
determine U1(1) , U2(1) , . . . , U N(1)node , U1(2) , U2(2) , . . . , U N(2)node . If the Jacobian is required,
it may be written in the form
 
J (11) J (12)
J= ,
J (21) J (22)

where the entries of the submatrices of J are given by, for i = 1, 2, . . . , Nnode ,
j = 1, 2, . . . , Nnode ,

Ri(1) Ri(1)
Ji,(11)
j = (1)
, Ji,(12)
j = ,
U j U (2)
j

Ri(2) Ri(2)
Ji,(21)
j = (1)
, Ji,(22)
j = .
U j U (2)
j

The entries of these submatrices may be calculated in the same way as the Jacobian
matrix for scalar value nonlinear differential equations, described in Chap. 5.

6.10 Exercises

1 A boundary value problem for a system of two differential equations is defined


by, for 0 < x < 1,

d2 u 1 d2 u 2
2 2
+ 2u 1 + 5u 2 = 2e2x ,
dx dx 2
d2 u 1 d2 u 2
3 + 12u 2 = ex ,
dx 2 dx 2
subject to Dirichlet boundary conditions at x = 0 given by

u 1 (0) = 1,
u 2 (0) = 2,

and Neumann boundary conditions at x = 1 given by

du 1 du 2
2 + = 2e + 4e2 ,
dx dx
du 1 du 2
3 = e 12e2 .
dx dx
118 6 Systems of Ordinary Differential Equations

The first Neumann boundary condition is a natural boundary condition for the first dif-
ferential equation, and the second Neumann boundary condition is a natural boundary
condition for the second differential equation.
This boundary value problem has analytic solution u 1 (x) = ex , u 2 (x) = 2e2x .

(a) In this chapter, we wrote down a model boundary value problem in Sect. 6.1 that
was similar to the system above. We then explained how to solve this system,
providing a computational implementation in Sect. 6.7. Using a similar pattern
to that presented for this model problem, define: (i) the weak formulation, (ii)
the finite element solution that is a linear approximation to the solution on each
element and (iii) the algebraic system of equations satisfied by the finite element
solution. Then, develop a computational implementation for setting up and solv-
ing the system of algebraic equations and confirm that your solution agrees with
the analytic solution.
(b) Develop the material presented in this chapter to allow computation of a finite
element solution to the boundary value problem above, using a quadratic approx-
imation to the solution on each element.
Chapter 7
Linear Elliptic Partial Differential Equations

In earlier chapters, we described how to apply the finite element method to ordinary
differential equations. For the remainder of this book, we will focus on extending
this technique for application to partial differential equations. As with ordinary dif-
ferential equations, we begin with a simple example to illustrate the key features. We
then discuss more complex differential equations in later chapters.
The material in this chapter requires some knowledge of vector calculus and inte-
gration over two-dimensional regions. We assume that the reader has some familiar-
ity with this topic, including: (i) the definition of the gradient operator ; (ii) basic
identities that use the gradient operator; (iii) use of the divergence theorem; and
(iv) surface and line integrals. A very brief summary of some of the vector calculus
required is given in Appendix B.

7.1 A First Model Problem

Our initial, simple model problem is the partial differential equation


 
2u 2u
+ 2 = 1, 0 < x < 1, 0 < y < 1, (7.1)
x 2 y

subject to Dirichlet boundary conditions on the whole boundary, given by

u(x, 0) = 0, 0 < x < 1, (7.2)


u(1, y) = 0, 0 < y < 1, (7.3)
u(x, 1) = 0, 0 < x < 1, (7.4)
u(0, y) = 0, 0 < y < 1. (7.5)

Our strategy when applying the finite element method to this model problem
will be the same as that used when applying the finite element method to ordinary
Springer International Publishing AG 2017 119
J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_7
120 7 Linear Elliptic Partial Differential Equations

differential equations. A summary of this strategy was written down in Sect. 2.9. We
encourage the reader to revisit this summary before reading the remainder of this
chapter.

7.2 The Weak Formulation

As we have seen throughout this book, the first step when using the finite element
method is to derive the weak formulation of the differential equation. As with ordinary
differential equations, this is done by first multiplying the given differential equation
by a function in an appropriate Sobolev subspace. Before we may do this for the
model problem given in Sect. 7.1, we must define Sobolev spaces for functions of
two variables that generalise the definitions given for functions of one variable in
Sect. 3.2.1.

7.2.1 Sobolev Spaces in Two Dimensions

Let be a subset of the (x, y)-plane. In Sect. 3.2.1, we defined the concept of
square integrable functions for functions of one variable. This definition may easily
be extended to functions of two variables. A function v(x, y) that is defined on the
region is said to be square integrable on this region if the integral

(v(x, y))2 d A

exists. The set of square integrable functions on this region, denoted by L 2 (), is
then defined by
  
L 2 () = functions v(x, y) such that the integral (v(x, y))2 d A exists .

The Sobolev space of order 1 for functions defined on the region , denoted by
H 1 (), is now defined to be those functions v(x, y) that (i) are defined and continu-
ous on ; (ii) lie in the set L 2 (); and (iii) have first partial derivatives with respect
to both x and y that lie in L 2 (). Hence,

H () = functions v(x, y) such that v(x, y) is continuous, and the integrals
1

   2   2 
v v
(v(x, y))2 d A, d A, and d A exist .
x y
(7.6)
7.2 The Weak Formulation 121

We may also define the subsets HE1 () and H01 () of H 1 () that mirror those
defined for functions of one variable in Sect. 3.2.1. Suppose a differential equation,
together with suitable boundary conditions, has been specified. Using these boundary
conditions, we define these two subsets by:

HE1 () = functions v(x, y) in H 1 () such that v(x, y) satisfies all Dirichlet

boundary conditions , (7.7)

H01 () = functions v(x, y) in H 1 () such that v(x, y) = 0 at all points (x, y)

where Dirichlet boundary conditions are specified . (7.8)

7.2.2 Deriving the Weak Formulation

The example differential equation, Eq. (7.1), is defined on the region 0 < x < 1,
0 < y < 1, which we will denote by . This differential equation may be written in
terms of the gradient operator as

(u) = 1, (x, y) .

Multiplying this equation by any function v(x, y) H01 () gives

v (u) = v, (x, y) .

After a little vector calculusthe use of Eq. (B.2) from Appendix Bthis may be
written

(vu) + (v) (u) = v, (x, y) .

We now integrate this equation over the region and apply the divergence theorem
to the first term on the left-hand side. Denoting the boundary of by yields
  
v (u) n ds + (v) (u) d A = v d A, (7.9)

where n denotes the normal vector to the boundary that points out of the region
and is of unit length. Note that, at this stage, some authors use the notation

u
= (u) n.
n
122 7 Linear Elliptic Partial Differential Equations

Remembering that v(x, y) H01 (), the function v(x, y) will take the value zero
at all points on the boundary of where Dirichlet boundary conditions are applied.
The boundary conditions for our model problem, Eqs. (7.2)(7.5), prescribe Dirichlet
boundary conditions on the whole boundary . Consequently, v(x, y) = 0 for every
point on . The first integral on the left-hand side of Eq. (7.9) will therefore be
zero, and we may write
 
(v) (u) d A = v d A. (7.10)

We are now ready to define the weak solution of our example differential equa-
tion. As with ordinary differential equations, the weak solution will satisfy both the
Dirichlet boundary conditions and the integral condition given by Eq. (7.10), for all
functions v(x, y) H01 (). More precisely, the weak solution of Eqs. (7.1)(7.5) is
given by
find u(x, y) H E1 () such that
 
(v) (u) d A = v d A, (7.11)

for all v(x, y) H01 ().

7.3 The Mesh and Basis Functions

Having derived the weak formulation of the differential equation, the next task is to
partition the region on which the differential equation is specified into elements and
to define the basis functions on these elements. In this chapter, we will partition
into a mesh of triangular elements. An alternative approach would be to partition
into quadrilateral elements; we will discuss this approach in Chap. 9. Both strategies
for partitioning the domain have their merits, which are beyond the scope of this
book. For most problems, however, either approach is suitable. We will consider only
linear basis functions in this chapter; higher order basis functions will be introduced
in Chap. 10.

7.3.1 A Mesh of Triangular Elements

Partitioning a general two-dimensional domain into a mesh of elementsknown as


mesh generationis a nontrivial task. However, our model problem is defined on
the square 0 < x < 1, 0 < y < 1, and we will see that this region may be partitioned
into triangular elements very easily. For more complex regions, established mesh
generation packages exist. These will be discussed in Sect. 7.10.
7.3 The Mesh and Basis Functions 123

The domain for the model problem, i.e. the square 0 < x < 1, 0 < y < 1, may
be partitioned into triangular elements by first partitioning the region into equally
sized rectangles so that there are N x rectangles in the x-direction and N y rectangles
in the y-direction. This is illustrated for N x = 2 and N y = 3 in Fig. 7.1a. We then
partition each rectangle into two triangles by drawing a line across one diagonal
of the rectangle, as illustrated in Fig. 7.1b. The elements of the mesh are then the
triangles generated, and the nodes are the vertices of the elements.
We now number both the nodes and the elements of the mesh shown in Fig. 7.1b.
When numbering nodes, we start with the node at the bottom left-hand corner of
the mesh and number the nodes consecutively moving in the positive x-direction.
We then move up to the next row in the y-direction and repeat this process, starting
from the left-hand end of this row. We proceed through the mesh in this fashion until
all nodes have been numbered. This numbering convention is illustrated in Fig. 7.2a
when N x = 2 and N y = 3. A similar process is used to number the elementsthis
is illustrated in Fig. 7.2b when N x = 2 and N y = 3.
When implementing the finite element method later in this chapter, we will find
it useful to have access to an array that contains the nodes of each element listed in
an anticlockwise direction. This array is known as the connectivity array, and the

(a) (b)
1 1

0.8 0.8

0.6 0.6
y

0.4 0.4

0.2 0.2

0 0

0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1


x x

Fig. 7.1 a Partitioning of into rectangles. b Partitioning of into triangles

(a) (b)
10 11 12
1 1

0.8 0.8 10 12
7 8 9 9 11
0.6 0.6
y

6 8
0.4 4 5 6 0.4 5 7

0.2 0.2 2 4
1 2 3 1 3
0 0
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1
x x

Fig. 7.2 a Labelling of the nodes in the mesh. b Labelling of the elements in the mesh
124 7 Linear Elliptic Partial Differential Equations

variable name traditionally used for this array in computer programs is lnods,1
and we follow this convention here. Suppose our mesh contains Nele elements. In
matrix notation, lnodsk,i , where k = 1, 2, . . . , Nele and i = 1, 2, 3, contains node i
of element k. A suitable matrix lnods for the mesh shown in Fig. 7.2 is given by

1 2 4
5 4 2

2 3 5

6 5 3

4 5 7

8 7 5
lnods =
5
.
6 8
9 8 6

7 8 10

11 10 8

8 9 11
12 11 9

Note that the ordering of the nodes in each row of the connectivity array is not
uniquewe require only that the nodes of each element are listed in an anticlockwise
order. The first row of the matrix lnods above could, for example, be stored in the
order 4, 1, 2 or 2, 4, 1. The reason for insisting that the elements are ordered in an
anticlockwise direction will become apparent when we define basis functions in the
next section. It is also important to note that the order in which the elements are listed
in the matrix lnods is not important. As such, we may interchange any rows of this
matrix.
When assembling the system of algebraic equations that determine the finite
element solution, we will require the value of x and y at each node. Suppose we have
Nnode nodes in the mesh. The values of x and y at node i, where i = 1, 2, . . . , Nnode ,
are denoted by xi and yi .
We now make some general comments on the partitioning of a domain into tri-
angular elements. The partitioning of illustrated in Fig. 7.2 has a key property
that is required by any finite element mesh in more than one spatial dimensionany
point in that is a node of one element is also a node of any other element that
it is contained in. For example, the partitioning of shown in Fig. 7.3 is not an
admissible partitioning, as a point existsdenoted by an open circlethat is a node
of one element, but lies on the edge of another element without being a node of that
element. This node is known as a hanging node.

1 This variable name is a consequence of the design of early programming languages and their
conventions for naming variables. Early versions of Fortran, the most common language of choice
for scientific computing until fairly recently, used the convention that variables representing integers
should have a variable name that starts with one of the letters i, j, k, l, m, n, or upper-case letters
corresponding to these letters. Furthermore, variable names were restricted to a maximum of six
characters.
7.3 The Mesh and Basis Functions 125

Fig. 7.3 An example of a


hanging node 1

0.8

0.6

y
0.4

0.2

0
0 0.2 0.4 0.6 0.8 1
x

There is a further consideration to be aware of when using a mesh in two, or


higher, dimensions. Long, thin elements should be avoided, as these elements can
degrade the accuracy of the solution. There are many metrics for quantifying this
property for an individual element, for example the minimum interior angle of an
element, the maximum interior angle of an element, or the ratio of the length of the
longest edge to the length of the shortest edge. It should be noted, however, that the
loss of accuracy usually only becomes apparent in very extreme casesfor example
when the minimum interior angle is less than around 10 .

7.3.2 Linear Basis Functions

In this chapter, we will calculate a finite element solution of the model problem that is
a linear approximation to the true solution on each element. We now identify suitable
basis functions for the mesh described in Sect. 7.3.1. We first define a canonical
triangular element in (X, Y )-coordinates to be the triangle that occupies the region
X 0, Y 0, 1 X Y 0, as shown in Fig. 7.4. The global basis functions are
then defined through (i) specification of local linear basis functions on this canonical
triangular element; and (ii) a linear transformation that maps between an element
of the mesh in (x, y)-coordinates and the canonical triangular element in (X, Y )-
coordinates.
Suppose the nodes of element k in a mesh of triangular elements, ordered in
an anticlockwise direction, are the nodes k1 , k2 and k3 . For the mesh described
in Sect. 7.3.1, these nodes will be the entries in row k of the matrix lnods, and the
values of x and y at these nodes are given by xk1 , xk2 , xk3 and yk1 , yk2 , yk3 . A mapping
between this triangular element, in (x, y)-coordinates, and the canonical triangular
element described above, in (X, Y )-coordinates, is given by

x = (1 X Y )xk1 + X xk2 + Y xk3 , (7.12)


y = (1 X Y )yk1 + X yk2 + Y yk3 . (7.13)
126 7 Linear Elliptic Partial Differential Equations

A general element in (x, y) coordinates The canonical element in (X,Y ) coordinates


1
(xk3 , yk3 )
0.8

0.6

Y
y

0.4

0.2
(xk2 , yk2 )
(xk1 , yk1 ) 0

0 0.2 0.4 0.6 0.8 1


x X

Fig. 7.4 Mapping between a general element in (x, y)-coordinates and the canonical element in
(X, Y )-coordinates

This mapping is illustrated in Fig. 7.4. It is straightforward to verify that the trans-
formation given by Eqs. (7.12) and (7.13) correctly maps the canonical element in
(X, Y )-coordinates to element k in (x, y)-coordinates. The vertices of the canonical
triangle, listed in an anticlockwise order, are the point X = 0, Y = 0, the point
X = 1, Y = 0 and the point X = 0, Y = 1. These three points are clearly mapped,
respectively, to nodes k1 , k2 and k3 of element k by Eqs. (7.12) and (7.13). Further,
as the map is a linear function of X and Y , straight lines (such as the edges of a
triangle) in the (X, Y )-coordinate system will be mapped to straight lines in the
(x, y)-coordinate system.
It is now apparent why we insisted that the nodes of each element are listed in an
anticlockwise order in the array lnods. If they were not, then the mapping between
element k and the canonical element given by Eqs. (7.12) and (7.13) would include
a reflection, which is not the intention of this transformation.
Local linear basis functions local,1 (X, Y ), local,2 (X, Y ), local,3 (X, Y ) are defined
on the canonical element by

local,1 (X, Y ) = 1 X Y, (7.14)


local,2 (X, Y ) = X, (7.15)
local,3 (X, Y ) = Y. (7.16)

These local basis functions are illustrated in Fig. 7.5 and have the property that they
take the value 1 at one node of the canonical triangle, and 0 at all other nodes:

local,1 (0, 0) = 1, local,1 (1, 0) = 0, local,1 (0, 1) = 0,


local,2 (0, 0) = 0, local,2 (1, 0) = 1, local,2 (0, 1) = 0,
local,3 (0, 0) = 0, local,3 (1, 0) = 0, local,3 (0, 1) = 1.

If there are Nnode nodes in the mesh, there will be a total of Nnode global basis func-
tions j , j = 1, 2, . . . , Nnode . We now define these global basis functions in terms
7.3 The Mesh and Basis Functions 127

local,1 (X,Y ) local,2 (X,Y ) local,3 (X,Y )

1 1 1

0.5 0.5 0.5


1 1 1
0 0.5 0 0.5 0 0.5
0 0.5 0 Y 0 0.5 0 Y 0 0.5 0 Y
1 1 1
X X X

Fig. 7.5 Local basis functions local,1 (X, Y ), local,2 (X, Y ) and local,3 (X, Y ) defined by
Eqs. (7.14)(7.16)

Fig. 7.6 An example basis 1


function

0.5

0
1

0.5

y 0 0.5 1
0 x

of the local basis functions on each element. On each element k = 1, 2, . . . , Nele ,


we define

k1 (X, Y ) = local,1 (X, Y ), (7.17)


k2 (X, Y ) = local,2 (X, Y ), (7.18)
k3 (X, Y ) = local,3 (X, Y ), (7.19)
j (X, Y ) = 0, j  = k1 , j  = k2 , j  = k3 . (7.20)

The global basis functions defined above may be written, if desired, as functions
of x and y on each element, by inverting the transformation given by Eqs. (7.12) and
(7.13), giving X and Y as functions of x and y. We shall see, however, that we do not
need to do this to assemble the system of algebraic equations satisfied by the finite
element solution. We do, however, illustrate an example global basis function as a
function of x and y in Fig. 7.6.
We now derive a useful property of the global basis functions defined by
Eqs. (7.12)(7.20). Remember that the node of the canonical triangle located at
X = 0, Y = 0 is mapped to node k1 of element k by Eqs. (7.12) and (7.13). We
then see that, on element k,
128 7 Linear Elliptic Partial Differential Equations

k1 (x = xk1 , y = yk1 ) = k1 (X = 0, Y = 0)
= local,1 (0, 0)
= 1,
k2 (x = xk1 , y = yk1 ) = k2 (X = 0, Y = 0)
= local,2 (0, 0)
= 0,
k3 (x = xk1 , y = yk1 ) = k3 (X = 0, Y = 0)
= local,3 (0, 0)
= 0,
j (x = xk1 , x = yk1 ) = 0, j  = k1 , j  = k2 , j  = k3 ,

where we have emphasised above whether we are working in the (x, y)-coordinate
system, or the (X, Y )-coordinate system. Similar consideration of the nodes k2 and
k3 allows us to write

k1 (xk2 , yk2 ) = 0, k1 (xk3 , yk3 ) = 0,


k2 (xk2 , yk2 ) = 1, k2 (xk3 , yk3 ) = 0,
k3 (xk2 , yk2 ) = 0, k3 (xk3 , yk3 ) = 1,
j (xk2 , yk2 ) = 0, j (xk3 , yk3 ) = 0, j  = k1 , j  = k2 , j  = k3 .

Hence, on any element k, the global basis functions j , j = 1, 2, . . . , Nnode , satisfy


the usual condition that

1, i = j,
j (xi ) = (7.21)
0, i = j.

As the property given by Eq. (7.21) holds on every element, we may deduce that it
holds for the whole domain.

7.4 Sets of Functions

We now define the sets of functions S h , S Eh , S0h that will be used when specifying
the finite element solution. These mirror the sets that were specified for functions of
one variable in Sect. 3.3.3. In this chapter, we will calculate a finite element solution
that is a linear approximation to the solution of the differential equation on each
triangular element. An appropriate set S h is
7.4 Sets of Functions 129


N node

S h = functions V (x, y) given by V (x, y) = V j j (x, y) where j (x, y),


j=1

j = 1, 2, . . . , Nnode , are defined by Eqs. (7.12)(7.20) . (7.22)

Subsets of S h , analogous to Eqs. (3.33) and (3.34), are defined as follows:



S Eh= functions V (x, y) in S h such that V (x, y) satisfies all Dirichlet boundary

conditions , (7.23)

S0h = functions V (x, y) in S h such that V (x, y) = 0 at all points (x, y) where

Dirichlet boundary conditions are specified . (7.24)

7.5 The Finite Element Solution

We now write the finite element solution as a linear sum of the global basis functions
defined by Eqs. (7.12)(7.20):


N node

U (x, y) = U j j (x, y), (7.25)


j=1

and note that U (x, y) is a member of the set S h . On using Eqs. (7.21) and Eqs. (7.25)
we see that, for i = 1, 2, . . . , Nnode ,


N node

U (xi , yi ) = U j j (xi , yi )
j=1
= Ui , (7.26)

and so Ui is, as usual, the value taken by the finite element solution at node i of the
mesh.
The coefficients of the basis functions U1 , U2 , . . . , U Nnode that appear in Eq. (7.25)
are determined by demanding that U (x, y) satisfies the following modification of
the weak formulation, Eq. (7.11):

find U (x, y) S Eh () such that


130 7 Linear Elliptic Partial Differential Equations
 
(i ) (U ) d A = i d A, (7.27)

for all i (x, y) S0h ().

7.6 The System of Algebraic Equations

The finite element solution given by Eq. (7.25) contains Nnode unknown quantities
U1 , U2 , . . . , U Nnode . We must therefore use the definition of the finite element solution,
given by Eq. (7.27), to identify Nnode algebraic equations to determine these values.
These equations are, as usual, drawn from two sources: (i) equations that ensure
that the finite element solution satisfies the Dirichlet boundary conditions; and (ii)
equations that arise from substituting suitable test functions i (x, y) S0h () into
the integral given by Eq. (7.27). We now discuss these two categories of equations
separately.

7.6.1 Satisfying the Dirichlet Boundary Conditions

We noted earlier that U (x, y), as given by Eq. (7.25), lies in the set S h . One of the
conditions imposed on the finite element solution is that it lies in the set S Eh that
is a subset of S h . To ensure that U (x, y) lies in S Eh , we therefore need to ensure
that it satisfies the Dirichlet boundary conditions. In our model problem, Dirichlet
boundary conditions are applied on the whole of the boundary. Suppose node i lies
on boundary, . Using Eq. (7.26), we see that the Dirichlet boundary conditions,
Eqs. (7.2)(7.5), are satisfied provided that

Ui = 0, (xi , yi ) , (7.28)

which provides our first category of algebraic equations.


Further, using Eq. (7.21) we see that i (xi , yi ) = 1. Hence, the basis function
i (x, y) is not zero at all points on the boundary, where Dirichlet boundary conditions
are applied. As a consequence, i (x, y) is not a member of the set S0h and may not
be used as a test function in the weak formulation given by Eq. (7.27).

7.6.2 Using Suitable Test Functions

Suppose that node i does not lie on the boundary, . Then it follows that i (x, y)
S0h (). Noting that Eq. (7.25) allows us to write
7.6 The System of Algebraic Equations 131


N node

U (x, y) = U j j (x, y),


j=1

we may substitute i (x, y) into Eq. (7.27) to obtain, after a little manipulation,


N   
node
 
(i ) j d A U j = i d A,
j=1

which may be written as


N node

Ai, j U j = bi ,
j=1

where

 
Ai, j = (i ) j d A, j = 1, 2, . . . , Nnode , (7.29)


bi = i d A. (7.30)

7.6.3 The Linear System

We may now write the system of algebraic equations satisfied by U1 , U2 , . . . , U Nnode


as the linear system

AU = b. (7.31)

If node i lies on the boundary, then Eq. (7.28) applies. The entries of A and b in row
i are then given by

1, j = i,
Ai, j = (7.32)
0, j = i,
bi = 0. (7.33)

If node i does not lie on the boundary, then the entries of A and b in row i are given
by Eqs. (7.29) and (7.30).
The system of algebraic equations derived above takes the same form as those
derived in earlier chapters. Some entries of the matrix A and vector b take the form of
integrals over the whole domain, while other entries may be written down explicitly.
132 7 Linear Elliptic Partial Differential Equations

7.7 Assembling the System of Algebraic Equations

As in earlier chapters, we will assemble the linear system given by Eq. (7.31) by
first evaluating the entries of A and b that are given by integrals, that is, Eqs. (7.29)
and (7.30). Having done this, we will then set the entries that are given explicitly
by Eqs. (7.32) and (7.33). We now describe how these two categories of entries are
handled in practice.

7.7.1 Assembling the Entries Defined by Integrals

We write the entries of the linear system that are defined by integrals, Eqs. (7.29) and
(7.30), as the sum of integrals over individual elements: when node i does not lie on
the boundary, we have

Nele 
 
Ai, j = (i ) j d A, j = 1, 2, . . . , Nnode , (7.34)
k=1 ek

Nele 

bi = i d A, (7.35)
k=1 ek

where Nele is the number of elements in the mesh, and ek is the region occupied
by element k. We now explain how the integrals over individual elements may be
evaluated.
Suppose the nodes of element k are the nodes k1 , k2 and k3 , listed in an anticlock-
wise order. Using the definition of the global basis functions, given by Eqs. (7.12)
(7.20), we see that the only global basis functions that are not identically zero on
this element are k1 , k2 and k3 . Hence, using the definition of the entries of A
given by Eq. (7.34), the only nonzero contributions from this element to the global
matrix A are to the entries Ai, j where i = k1 , k2 , k3 and j = k1 , k2 , k3 . We therefore
calculate only these nonzero local contributions, and store them in the 3 3 matrix
(k) (k)
Alocal , where Alocal,i, j contributes to Aki ,k j for i = 1, 2, 3, j = 1, 2, 3. Similarly, the
contributions from this element to the entries of b given by Eq. (7.35) are zero unless
i = k1 , k2 , k3 . We again calculate only these nonzero contributions, which we store
(k) (k)
in the local vector blocal that contains three entries, where blocal,i contributes to bki for
i = 1, 2, 3.
(k) (k)
The entries of Alocal and blocal may be calculated using the transformation between
element k and the canonical element given by Eqs. (7.12) and (7.13), and writing the
global basis functions in terms of the local basis functions given by Eqs. (7.17)
(k)
(7.19). We derive the entries of Alocal first. For i = 1, 2, 3, j = 1, 2, 3, we may
write
7.7 Assembling the System of Algebraic Equations 133

(k)    
Alocal, i, j = ki k j d A
ek

ki k j ki k j
= + dA
ek x x y y
  
local,i local, j local,i local, j
= + det(F) d A X , (7.36)
 x x y y

where  , combined with d A X , denotes integration over the canonical element, and
F is the Jacobian matrix of the transformation defined by Eqs. (7.12) and (7.13),
which may be written as
 
x x
X Y
F= y y
X Y
 
x k2 x k1 x k3 x k1
= . (7.37)
yk2 yk1 yk3 yk1

The determinant of F, which appears in Eq. (7.36), takes a constant value on element
k, given by
     
det(F) = xk2 xk1 yk3 yk1 xk3 xk1 yk2 yk1 . (7.38)

The integrand in Eq. (7.36) contains partial derivatives of local basis functions with
respect to the coordinates x and y. As the local basis functions are written in terms
of the coordinates X and Y , we use the chain rule to evaluate the quantities required.
The chain rule may be written in matrix form as
local,1 local,1 local,1 local,1
x y X Y
X X

local,2 local,2 local,2 local,2 x y
= X
x y Y Y Y
local,3 local,3 local,3 local,3 x y
x y X Y

1 1  X X

x y
= 1 0 Y Y
, (7.39)
0 1 x y

where the partial derivatives of the local basis functions with respect to X and Y
follow from their definitions, Eqs. (7.14)(7.16). It can be shown that
X X
  1
x x
x y X Y
Y Y
= y y
x y X Y
1
=F ,

and so we may write, on using Eq. (7.37),


134 7 Linear Elliptic Partial Differential Equations
X X
  1
x y x k2 x k1 x k3 x k1
Y Y
=
x y
yk2 yk1 yk3 yk1
 
1 yk3 yk1 xk1 xk3
= , (7.40)
det(F) yk1 yk2 xk2 xk1

where we have used an explicit expression for the inverse of a 22 matrix. Combining
Eqs. (7.39) and (7.40) allows us to evaluate the partial derivatives that appear in
Eq. (7.36). Noting that the Jacobian is given by Eq. (7.38), we now have explicit
expressions for all terms in the integrand in Eq. (7.36). Finally, we see that all terms in
the integrand are constant, and so the integral may be evaluated by simply multiplying
this constant integrand by the area of the canonical trianglei.e. the value 0.5to
give, for i = 1, 2, 3, j = 1, 2, 3:
 
(k) 1 local,i local, j local,i local, j
Alocal, i, j = det(F) + . (7.41)
2 x x y y

(k)
The entries of blocal may be calculated in a similar fashion. We first note that, for
i = 1, 2, 3, integration of the local basis functions over the canonical element may
be written
  1  1Y 
local,i (X, Y ) d A X = local,i (X, Y ) dX dY
 Y =0 X =0
1
= .
6
(k)
Using the definition of blocal given by Eq. (7.35), and the expression for the determi-
nant of F given by Eq. (7.38), we may write, for i = 1, 2, 3:

(k)
blocal,i = k i d A
ek

= local,i (X, Y ) det(F) d A X

1
= det(F). (7.42)
6
We may now assemble the entries of A and b that are given by Eqs. (7.34) and
(7.35) by looping over all elements, and calculating only the nonzero contributions
from each element, given by Eqs. (7.41) and (7.42). These local contributions from
each element are then added to the appropriate entry of the global matrix A and global
vector b; the relationship between the indices of the entries in the global linear system
and the indices of the entries of local contributions is given in Table 7.1.
7.7 Assembling the System of Algebraic Equations 135

Table 7.1 The relationship Local index Global index


between the location of the
entries in the local and global 1 k1 = lnods(k,1)
matrices and vectors 2 k2 = lnods(k,2)
3 k3 = lnods(k,3)

7.7.2 Setting the Entries Defined Explicitly

When looping over all elements, as described in Sect. 7.7.1, we will add contributions
into the global matrix A and global vector b from every node of every element. This
will include not only the rows where the entries of A and b are given by Eqs. (7.29)
and (7.30), but also the rows where A and b are given by Eqs. (7.32) and (7.33). We
therefore set these entries correctly by using the explicit values given by Eqs. (7.32)
and (7.33).

7.8 Evaluating the Solution at a Given Point

Suppose we want to evaluate the finite element solution at some point (x, y), i.e. we
want to evaluate U (x, y) for given x and y. The first task is to identify the element
that this point lies in. This may be done by first writing the transformation between
element k and the canonical element, Eqs. (7.12) and (7.13), as

(x xk1 )(yk3 yk1 ) (y yk1 )(xk3 xk1 )


X = , (7.43)
(xk2 xk1 )(yk3 yk1 ) (yk2 yk1 )(xk3 xk1 )
(x xk1 )(yk2 yk1 ) (y yk1 )(xk2 xk1 )
Y = . (7.44)
(xk3 xk1 )(yk2 yk1 ) (yk3 yk1 )(xk2 xk1 )

If the point (x, y) is mapped by this transformation to a point inside the canonical
trianglethat is, X 0, Y 0, and X + Y 1then (x, y) lies inside element k.
Having identified the element k that contains the point (x, y), suppose that the nodes
of this element are k1 , k2 , k3 . Using the definition of local basis functions given by
Eqs. (7.14)(7.16), and the values of X , Y , given by Eqs. (7.43) and (7.44), we may
deduce that

U (x, y) = Uk1 local,1 (X, Y ) + Uk2 local,2 (X, Y ) + Uk3 local,3 (X, Y ).
136 7 Linear Elliptic Partial Differential Equations

7.9 Computational Implementation

We now provide a computational implementation of the material presented in this


chapter. In Sect. 7.3.1, we partitioned the domain 0 < x < 1, 0 < y < 1 into tri-
angular elements by first partitioning this region into equally sized rectangles, with
N x rectangles in the x-direction, and N y rectangles in the y-direction. Listing 7.1
contains a Matlab function that accepts N x and N y as input and returns the coordi-
nates of the nodes x, y, the connectivity array lnods and the finite element solution
at each node U.
In line 4, a function is called as GenerateMesh. This function generates the
arrays x, y and lnods that are discussed in the text. It also generates a further array,
dir_nodes, that contains a list of all nodes on the Dirichlet boundary. The body
of the function, given in lines 4280, populates the arrays x, y and lnods using
the conventions given in Sect. 7.3.1. Note that the entries of x and y are allocated
uniformly across the intervals 0 x 1 and 0 y 1these lines of code need
to be modified if a different size of rectangular domain is used. The entries of the
array dir_nodes are ordered starting from the point x = 0, y = 0 and proceeding
around the boundary in an anticlockwise direction. The ordering of the nodes in the
array dir_nodes, however, has no effect on the finite element solution calculated.
For a mesh with N x elements in the x-direction and N y elements in the y-direction,
there will be a total of (N x + 1)(N y + 1) nodes in the mesh, and the linear system
will therefore contain (N x + 1)(N y + 1) equations. In lines 78, we initialise the
global matrix A and global vector b to be of the correct size, with all entries equal
to zero. Note that we define A to be a sparse matrixthis has the effect that only
the nonzero entries of A are stored. As each row of A contains very few nonzero
entries, this results in a significant reduction in computational memory requirement,
particularly when the number of rows in the matrix is large. A discussion of sparse
matrices is given in Appendix A.
There will be 2N x N y elements in the mesh defined in Sect. 7.3.1. In lines 1219,
we loop over these elements, calculating the local contributions to A and b, before
incrementing the appropriate entries of A and b. The local entries are calculated using
the function CalculateContributionOnElement that occupies lines 85
117. In this function, the location of the nodes of the element is given in lines 89
94. The determinant of the transformation between the element and the canonical
element is given in line 98, before the partial derivatives of the basis functions,
with respect to first the (X, Y )-coordinate system and then the (x, y)-coordinate
system, are calculated in lines 102104, using the expression given by Eq. (7.39).
These partial derivatives are used to calculate the local contribution to A (given by
Eq. (7.41)) in lines 107114, before assigning the local contributions to b (given by
Eq. (7.42)) in line 117.
Having calculated the contributions to A and b that arise from integrals, we then
set entries that enforce the Dirichlet boundary conditions in lines 2226. Note that we
set to zero all entries of the rows of A that correspond to Dirichlet boundary conditions
in line 22 as some of these entries may have been set to nonzero values when looping
7.9 Computational Implementation 137

over all elementssee Sect. 7.7.2 for more details. We then solve the linear system. In
Appendix A, we propose solving large linear systems using preconditioned, iterative
methods. Lines 2932 solve the linear system using an incomplete LU -factorisation
(calculated in line 29), preconditioned GMRES (line 32). When using GMRES, we
use a restart value of 30. For more details on what these terms mean, see Appendix A.
Finally, the solution is plotted using lines 3538.
This Matlab function may be executed by typing, for example,
[x, y, lnods, U] = Chap7_CalculateExampleFem(50, 50);

into a Matlab session.


Listing 7.1 Chap7_CalculateExampleFem.m, a Matlab function for calculating
 finite element solution of the model problem.
the
1 f u n c t i o n [ x , y , lnods , U ] = C h a p 7 _ C a l c u l a t e E x a m p l e F e m ( Nx , Ny )
2
3 % G e n e r a t e data s t r u c t u r e s n e e d e d
4 [x , y , lnods , d i r _ n o d e s ] = G e n e r a t e M e s h ( Nx , Ny );
5
6 % I n i t i a l i s e A and b to zero
7 A = s p a r s e (( Nx +1)*( Ny +1) , ( Nx +1)*( Ny +1));
8 b = zeros (( Nx +1)*( Ny +1) , 1);
9
10 % Loop over elements , c a l c u l a t i n g l o c a l c o n t r i b u t i o n s , and
11 % adding the local c o n t r i b u t i o n s into the global A and b
12 for k =1:2* Nx * Ny
13 [ A_local , b _ l o c a l ] = ...
14 C a l c u l a t e C o n t r i b u t i o n O n E l e m e n t (x , y , lnods , k );
15
16 A ( lnods (k ,:) , lnods (k ,:)) = ...
17 A ( l n o d s ( k ,:) , l n o d s ( k , : ) ) + A _ l o c a l ;
18 b ( lnods (k ,:)) = b ( lnods (k ,:)) + b _ l o c a l ;
19 end
20
21 % Handle Dirichlet boundary conditions
22 A ( dir_nodes ,:) = 0;
23 for i =1: l e n g t h ( d i r _ n o d e s )
24 A ( d i r _ n o d e s ( i ) , d i r _ n o d e s ( i )) = 1;
25 end
26 b ( d i r _ n o d e s ) = 0;
27
28 % C o m p u t e ILU f a c t o r i z a t i o n of A
29 [ Lower , Upper ] = ilu ( A );
30
31 % Use ILU p r e c o n d i t i o n e d GMRES to solve linear system
32 U = gmres (A , b , 30 , [] , 1000 , Lower , Upper );
33
34 % Plot s o l u t i o n
35 t r i s u r f ( lnods , x , y , U );
36 x l a b e l ( x )
37 y l a b e l ( y )
38 z l a b e l ( U )
39
40
41 % F u n c t i o n to g e n e r a t e data s t r u c t u r e s r e q u i r e d by the mesh
42 f u n c t i o n [ x , y , lnods , d i r _ n o d e s ] = G e n e r a t e M e s h ( Nx , Ny )
43
138 7 Linear Elliptic Partial Differential Equations

44 % Set e n t r i e s of l n o d s
45 lnods = zeros (2* Nx * Ny , 3);
46 for j =1: Ny ;
47 for i =1: Nx ;
48 ele = 2*( j -1)* Nx +2* i -1;
49 lnods ( ele ,:) = ...
50 [( j -1)*( Nx +1)+ i , (j -1)*( Nx +1)+ i +1 , j *( Nx +1)+ i ];
51
52 ele = ele +1;
53 lnods ( ele ,:) = ...
54 [ j *( Nx +1)+ i +1 , j *( Nx +1)+ i , (j -1)*( Nx +1)+ i +1];
55 end
56 end
57
58 % Set e n t r i e s of x and y over the i n t e r v a l from 0 to 1
59 x = zeros (( Nx +1)*( Ny +1) ,1);
60 y = zeros (( Nx +1)*( Ny +1) ,1);
61
62 for i =1: Nx +1
63 x ( i : Nx +1:( Nx +1)*( Ny +1)) = ( i -1)/ Nx ;
64 end
65
66 for j =1: Ny +1
67 y (( j - 1 ) * ( Nx + 1 ) + 1 : j *( Nx +1)) = ( j -1)/ Ny ;
68 end
69
70 % S p e c i f y l o c a t i o n s of D i r i c h l e t b o u n d a r y c o n d i t i o n s
71 % Nodes on y =0
72 d i r _ n o d e s (1: Nx +1) = 1: Nx +1;
73 % Nodes on x =1
74 d i r _ n o d e s ( Nx +2: Nx + Ny +1) = 2*( Nx +1): Nx +1:( Ny +1)*( Nx +1);
75 % Nodes on y =1
76 d i r _ n o d e s ( Nx + Ny +2:2* Nx + Ny +1) = ...
77 ( Ny + 1 ) * ( Nx +1) -1: -1: Ny *( Nx + 1 ) + 1 ;
78 % Nodes on x =0
79 d i r _ n o d e s (2* Nx + Ny + 2 : 2 * ( Nx + Ny )) = ...
80 ( Ny -1)*( Nx +1)+1: -( Nx +1): Nx +2;
81
82
83
84 % C a l c u l a t e local c o n t r i b u t i o n s to A and b from elem e n t k
85 f u n c t i o n [ A_local , b _ l o c a l ] = ...
86 C a l c u l a t e C o n t r i b u t i o n O n E l e m e n t ( x , y , lnods , k )
87
88 % D e f i n e x and y c o o r d i n a t e of each node
89 x1 = x ( lnods (k ,1));
90 x2 = x ( lnods (k ,2));
91 x3 = x ( lnods (k ,3));
92 y1 = y ( lnods (k ,1));
93 y2 = y ( lnods (k ,2));
94 y3 = y ( lnods (k ,3));
95
96 % C a l c u l a t e J a c o b i a n of t r a n s f o r m a t i o n b e t w e e n c a n o n i c a l
97 % e l e m e n t and e l e m e n t k
98 detF = ( x2 - x1 )*( y3 - y1 ) - ( x3 - x1 )*( y2 - y1 );
99
100 % E v a l u a t e d e r i v a t i v e s of basis f u n c t i o n s in terms of (x , y )
101 % coordinates
102 Finv = [ y3 - y1 , x1 - x3 ; y1 - y2 , x2 - x1 ]/ detF ;
7.9 Computational Implementation 139

103 d p h i _ d X = [ -1 , -1; 1 , 0; 0 , 1];


104 d p h i _ d x = d p h i _ d X * Finv ;
105
106 % A s s e m b l e local c o n t r i b u t i o n to A
107 A _ l o c a l = zeros (3 ,3);
108 for i =1:3
109 for j =1:3
110 A _ l o c a l ( i , j ) = detF *0.5* ...
111 ( d p h i _ d x ( i ,1)* d p h i _ d x ( j ,1) + ...
112 d p h i _ d x ( i ,2)* d p h i _ d x ( j , 2 ) ) ;
113 end
114 end
115
116 % A s s e m b l e local c o n t r i b u t i o n to b
117 b _ l o c a l = ones (3 ,1)* detF /6;

In the computational implementation presented in this section, we chose to use a


sparse matrix to store the matrix A and to use a preconditioned iterative technique
to solve the linear system that arises. For this simple example problem, it is likely
that a sufficiently accurate finite element solution may be obtained by setting both
N x and N y to small enough values that A may be stored as a full matrix. The linear
system may then be solved using a direct solver, such as the backslash operator in
Matlab. For more complex partial differential equations, the linear systems may be
larger, and using sparse matrices and preconditioned iterative techniques will be the
only practical option for storing and solving these systems. We therefore feel that the
time is right to introduce the linear algebra techniques used in this implementation.

7.10 More Complex Geometries

Calculation of the finite element solution of a differential equation requires us to


partition the domain on which the solution is defined into elements. In this chapter,
we considered only the square domain 0 < x < 1, 0 < y < 1, which could be
partitioned manually. Partitioning other geometries into triangular elements, even
the simple circular domain x 2 + y 2 < 1 for example, is not so straightforward.
Furthermore, problems of practical interest are often posed on domains that are
irregular. Fortunately, for two-dimensional problems, two excellent, freely available
packages are available for partitioning a subset of the (x, y)-plane into a mesh
of triangular elements. One of these is DistMesh [7], which may be interfaced
from Matlab. The other is triangle [8,9], written in the programming language
C. Both packages can easily be found using a search engine, and documentation is
available explaining how they may be used to generate a mesh of triangular elements
for a given domain .
We now give some general comments on using mesh generation packages that
are not aimed at any specific package. A mesh generation package will replace the
function GenerateMesh called from line 4 of Listing 7.1. This function returned
the coordinates of the nodes x, y, the connectivity array lnods and the list of nodes
140 7 Linear Elliptic Partial Differential Equations

that are on the part of the boundary where Dirichlet boundary conditions are applied.
In general, mesh generation packages may be used to create at least the first three of
these. One word of caution is that the user should perform a check to ensure that the
connectivity array does list the nodes of each element in an anticlockwise direction.
This is straightforward to doif the determinant of the Jacobian of the transformation
between an element and the canonical element, given by Eq. (7.38), is negative, then
the nodes are not listed in the correct order. If the nodes are not ordered correctly,
then this may be fixed by interchanging the order of two nodes of that element in the
connectivity array. Suppose a list of nodes lying on the boundary where Dirichlet
boundary conditions are applied is not available. This list may easily be generated
simply loop over all nodes, and check whether they lie on this boundary.

7.11 Exercises

7.1 Modify the computational implementation given in Listing 7.1 so that it may
be used to solve the partial differential equation
 
2u 2u
+ = 5, 3 < x < 3, 1 < y < 0,
x2 y2

subject to Dirichlet boundary conditions on the whole boundary, given by

u(x, 1) = (x + 3)(x 3), 3 < x < 3,


u(3, y) = 0, 1 < y < 0,
u(x, 0) = 0, 3 < x < 3,
u(3, y) = 0, 1 < y < 0.

Use the material presented in Sect. 7.8 to evaluate U (0.48, 0.2).

7.2 Use a mesh generation package such as DistMesh, described in Sect. 7.10, to
generate a mesh of triangular elements that is a partitioning of the square 0 < x < 1,
0 < y < 1. Modify Listing 7.1 so that it uses the mesh that you have generated.
Confirm that your implementation is correct by comparison with the output from the
original implementation given by Listing 7.1.

7.3 In this exercise, we will calculate the finite element solution of the differential
equation given by
 
2u 2u
+ = 1, x 2 + y 2 < 1,
x2 y2
7.11 Exercises 141

subject to Dirichlet boundary conditions on the whole boundary, given by

u(x, y) = 0, x 2 + y 2 = 1.

This boundary value problem has solution

1 
u(x, y) = 1 x 2 y2 .
4
(a). Use a mesh generation package such as DistMesh, described in Sect. 7.10,
to generate a mesh of triangular elements that is a partitioning of the domain
x 2 + y 2 = 1.
(b). Modify Listing 7.1 so that it uses the mesh generated in part (a) to calculate
the finite element solution of the boundary value problem defined above. Verify
your solution is correct by comparison with the analytic solution.

7.4 Modify Listing 7.1 so that (i) the matrix A is stored as a full matrix, rather than
a sparse matrix; and (ii) the linear system is solved using the Matlabs backslash
operator. Investigate the performance of both implementations by varying the values
of N x and N y used as input.
Chapter 8
More General Elliptic Problems

In the previous chapter, we explained how to calculate the finite element solution of a
very simple elliptic partial differential equation, where Dirichlet boundary conditions
were applied on the whole boundary. We develop these ideas in this chapter to allow
the application of the finite element method to more general linear, elliptic partial
differential equations, and more general boundary conditions.

8.1 A Model Problem

In this chapter, we will illustrate the application of the finite element method to the
model partial differential equation

( p(x, y)u) + q(x, y) u + r (x, y)u = f (x, y), (x, y) , (8.1)

where is some finite region of the (x, y)-plane, the vector-valued function q(x, y)
is given by
 
q1 (x, y)
q(x, y) = ,
q2 (x, y)

and p(x, y), q(x, y), r (x, y) and f (x, y) are given functions. We partition the bound-
ary into two regions, D and N . On the boundary D , we apply Dirichlet
boundary conditions:

u(x, y) = g D (x, y), (x, y) D , (8.2)

and on the boundary N , we apply natural Neumann boundary conditions:

Springer International Publishing AG 2017 143


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_8
144 8 More General Elliptic Problems

p(x, y) (u) n = g N (x, y), (x, y) N , (8.3)

where n is the normal vector to the boundary N , pointing out of the domain ,
and of unit length.

8.2 The Weak Formulation

The Sobolev space H 1 () and the subspaces HE1 () and H01 () of H 1 () are
defined in Sect. 7.2.1. The first step when deriving the weak formulation of the
differential equation given by Eq. (8.1), subject to the boundary conditions given
by Eqs. (8.2) and (8.3), is to multiply the differential equation by a test function
v(x, y) H01 (), and to integrate the resulting product over the domain :
 
( ( pu) + q u + r u) v d A = f v d A. (8.4)

Using Eq. (B.2) from Appendix B allows us to write

v ( pu) = (v pu) + p (v) (u) ,

and so Eq. (8.4) may be written


 
(v pu) + p (v) (u) + vq u + r uv d A = f v d A.

Applying the divergence theorem to the first term on the left-hand side of this equation
gives
  
v p (u) n ds + p (v) (u) + vq u + r uv d A = f v d A.

(8.5)

The boundary was partitioned into the components D and N . As v(x, y)


H01 (), we have v(x, y) = 0 for all points on D . Using this observation, and
the Neumann boundary condition given by Eq. (8.3), we may write the line integral
above as
  
v p (u) n ds = v p (u) n ds + v p (u) n ds
D N

= 0+ vg N ds.
N

Substituting this into Eq. (8.5), we obtain, after a little rearranging,


8.2 The Weak Formulation 145
  
p (v) (u) + vq u + r uv d A = f v dA + vg N ds.
N

All that is now required to state the weak formulation of the boundary value
problem defined by Eqs. (8.1)(8.3) is to demand that the weak solution u(x, y)
satisfies the Dirichlet boundary conditions given by Eq. (8.2). We then define the
weak solution by:
find u(x, y) HE1 (), such that
  
p (v) (u) + vq u + r uv d A = f v dA + vg N ds, (8.6)
N

for all v(x, y) H01 ().

8.3 The Mesh and Basis Functions

We partition the domain into a mesh of triangular elements, as described in


Sect. 7.3.1. We use basis functions that allow a linear approximation to be made
to the solution on each element. Suitable basis functions are the basis functions
defined in Sect. 7.3.2 by Eqs. (7.12)(7.20). The set of functions S h , and the subsets
S Eh and S0h of S h , are then those defined in Sect. 7.4.

8.4 The Finite Element Solution

Having defined the weak solution by Eq. (8.6), the sets S Eh and S0h allow us to define
the finite element solution by
find U (x, y) S Eh , such that
  
p (i ) (U ) + i q U + rU i d A = f i d A + i g N ds,
N
(8.7)

for all i (x, y) S0h .

8.5 The Algebraic Equations

We write the finite element solution as:


N node

U (x, y) = U j j (x, y), (8.8)


j=1
146 8 More General Elliptic Problems

where Nnode is the number of nodes in the mesh, and note that U (x, y) written in
this form is a member of the set S h . We will assemble a system of Nnode algebraic
equations for the unknown values U1 , U2 , . . . , U Nnode using a similar procedure to
that used in Sect. 7.6 for the simple model partial differential equation. As in earlier
chapters, this system comprises two categories of equations. The first category of
equations ensure that U (x, y) satisfies the Dirichlet boundary conditions, so that
U (x, y) lies in the subset S Eh of the set S h , as required. The second category of
equations arises from substituting a suitable test function i (x) into Eq. (8.7). We
now discuss both categories of equations separately.

8.5.1 Satisfying the Dirichlet Boundary Conditions

Suppose that node i of the mesh lies on D , the part of the boundary where Dirichlet
boundary conditions are applied. The Dirichlet boundary condition is satisfied at this
node if

U (xi , yi ) = g D (xi , yi ).

Recall from Eq. (7.26) that U (xi , yi ) = Ui . A suitable algebraic equation that ensures
that U (x, y) satisfies a Dirichlet boundary condition at node i is then

Ui = g D (xi , yi ). (8.9)

8.5.2 Using Suitable Test Functions

Suppose that node i of the mesh does not lie on the part of the boundary where Dirich-
let boundary conditions are applied. The basis function i (x, y) will be nonzero at
node i and will take the value zero at all other nodes. As such, i (x, y) will take
the value zero at all nodes on the part of the boundary where Dirichlet boundary
conditions are applied, and so i (x, y) S0h . Noting that Eq. (8.8) allows us to write


N node

U (x, y) = U j j (x, y),


j=1

we then substitute i (x, y) into Eq. (8.7) and obtain, after some manipulation,


N  
node
   
p (i ) j + i q j + r i j d A U j =
j=1
 
f i d A + i g N ds.
N
(8.10)
8.5 The Algebraic Equations 147

8.5.3 The Linear System

We may now combine Eqs. (8.9) and (8.10) into the linear system

AU = b, (8.11)

where, if node i lies on D , we deduce from Eq. (8.9) that



1, j = i,
Ai, j = (8.12)
0, j = i,
bi = g D (xi , yi ), (8.13)

and, if node i does not lie on D , we deduce from Eq. (8.10) that

   
Ai, j = p (i ) j + i q j + r i j d A,

j = 1, 2, . . . , Nnode , (8.14)
 
bi = f i d A + i g N ds. (8.15)
N

8.6 Assembling the Algebraic Equations

The only boundary conditions satisfied by the model partial differential equation in
Chap. 7 were Dirichlet boundary conditions. The model differential equation used in
this chapter contains the additional feature of some Neumann boundary conditions.
This results in an extra term in some entries of the vector on the right-hand side of
the linear system given by Eq. (8.11), specifically the integral over N that appears
in Eq. (8.15). We will see later that it is convenient to write this extra integral as the
sum of contributions from individual element edges.
As in earlier chapters, the linear system given by Eq. (8.11) is assembled by first
evaluating the entries given by Eqs. (8.14) and (8.15), i.e. the entries that are given by
integrals over the whole domain and, for the vector b, an extra integral over N . As
usual, the integrals over will be calculated by decomposing the integral into the
sum of integrals over individual elements. The integrals over N will, as explained
above, be calculated by decomposing these integrals into the sum of integrals over
individual element edges. We then write Eqs. (8.14) and (8.15), the equations that
apply when node i does not lie on D , as:

Nele 
    
Ai, j = p (i ) j + i q j + r i j d A,
k=1 ek

j = 1, 2, . . . , Nnode , (8.16)
148 8 More General Elliptic Problems

Nele 
 NNeu 

bi = f i d A + i g N ds, (8.17)
k=1 ek k=1 e Nk

where Nele is the number of elements in the mesh, ek is the region occupied by
element k, NNeu is the number of element edges that lie on N and e Nk is edge k
of the element edges that lie on N .
After the entries of A and b given by Eqs. (8.14) and (8.15) have been assembled,
we set the entries that are given explicitly by Eqs. (8.12) and (8.13). We now give
more details on how these tasks may be carried out.

8.6.1 Evaluating Integrals Over

We begin by calculating the contributions to Eqs. (8.16) and (8.17) that arise from
integrating over an element. Suppose that element k contains the nodes k1 , k2 , k3 ,
listed in an anti-clockwise order. We explained in Sect. 7.7.1 that only the basis
functions k1 (x), k2 (x), k3 (x) are nonzero on this element, and that the nonzero
contributions to Eqs. (8.16) and (8.17) from integrating over this element can be
(k)
stored in a 3 3 matrix Alocal with entries given by, for i = 1, 2, 3, j = 1, 2, 3:

(k)      
Alocal,i, j = p(x, y) ki k j + ki q(x, y) k j + r (x, y)ki k j d A,
ek

(k)
and a vector with three entries blocal , where the entries are given by, for i = 1, 2, 3:

(k)
blocal,i = f (x, y)ki d A.
ek

(k)
As explained in Sect. 7.7.1, the entry Alocal,i, j contributes to entry Aki ,k j of the global
(k)
matrix A for i = 1, 2, 3, and j = 1, 2, 3. Similarly, the entry blocal,i contributes to
entry bki of the global vector b for i = 1, 2, 3.
We use the local basis functions given by Eqs. (7.12)(7.20) to write these local
(k)
contributions as integrals over the canonical triangle. The entries of Alocal,i, j are given
by, for i = 1, 2, 3, j = 1, 2, 3:
 
(k)      
Alocal,i, j = p(x, y) local,i local, j + local,i q(x, y) local, j +


r (x, y)local,i local, j det(F) d A X ,
(8.18)
8.6 Assembling the Algebraic Equations 149

where, as in Chap. 7,  , coupled with d A X , denotes integration over the canonical
triangular element and det(F) is the Jacobian of the transformation between element
k and the canonical element, given by Eq. (7.37). The partial derivatives that appear in
the integrand above may all be calculated using the method described in Sect. 7.7.1.
To evaluate the integral above, we need to write the functions p, q and r as functions
of X and Y , as these are the variables that we are integrating with respect to. This
is done by using Eqs. (7.12) and (7.13) to evaluate x and y as functions of X and
Y we note that these equations may be written in the form:

x = xk1 local,1 (X, Y ) + xk2 local,2 (X, Y ) + xk3 local,3 (X, Y ),


y = yk1 local,1 (X, Y ) + yk2 local,2 (X, Y ) + yk3 local,3 (X, Y ).

The function p(x, y), for example, may be evaluated for any values of X and Y by
first using the expressions above to calculate the required values of x and y.
(k)
Similarly, the entries, of blocal,i, j are given by, for i = 1, 2, 3,


(k)
blocal,i = f (x, y)local,i det(F) d A X , (8.19)


where the function f (x, y) may be written as a function of X and Y using the same
method as for functions of x and y that appear in Eq. (8.18).
The local contributions to A and b, given by Eqs. (8.18) and (8.19), are now
written as integrals of functions of X and Y over the canonical element. Although
we can write down explicit expressions for all terms that appear in the integrand of
these equations, performing the integration analytically may be very difficult, or even
impossible. For ordinary differential equations, we saw that the (one-dimensional)
integrals arising may be approximated numerically using quadrature. We will explain
in Sect. 8.7 how to approximate integrals over the canonical triangle, such as those
given by Eqs. (8.18) and (8.19), using quadrature.
Having evaluated the local contributions to A and b, we then add these local
contributions into the appropriate entries of A and b as described in Sect. 7.7.1. We
perform this operation on every element to assemble the integral over .

8.6.2 Evaluating Integrals Over N

We now turn our attention to the second integral on the right-hand side of Eq. (8.17),
the integral over the part of the boundary where Neumann boundary conditions are
applied. Suppose that edge k of the element edges that lie on N , denoted by e Nk ,
connects node k1 to node k2 . Only the basis functions k1 and k2 will be nonzero
on this edge, and so there will be a nonzero contribution from this edge only to the
entries bk1 and bk2 of the global vector b. In the spirit of calculating only nonzero
local contributions, we store these two nonzero local contributions to b from this
150 8 More General Elliptic Problems

(k)
element edge in the vector blocal edge with two entries given by, for i = 1, 2,


(k)
blocal edge,i = ki (x, y)g N (x, y) ds. (8.20)
e Nk

As the edge e Nk connects node k1 to node k2 , points (x, y) that lie on this edge are
parameterised by, for 0 X 1,

x = (1 X )xk1 + X xk2 , (8.21)


y = (1 X )yk1 + X yk2 . (8.22)

Further, we may write the basis functions k1 and k2 in terms of the variable X on
this edge as, for 0 X 1,

k1 (X ) = 1 X,
k2 (X ) = X.

It is straightforward to justify these expressions for k1 and k2 using this definition


it can be seen that k1 and k2 are linear functions, and that they take the correct values
at the node k1 (where X = 0) and the node k2 (where X = 1). The integral given by
Eq. (8.20) may then be written, for i = 1, 2:
 1
(k)
blocal edge,i =h ki (X )g N (x, y) dX, (8.23)
0

where h is the length of the edge, given by



 2  2
h= x k1 x k2 + yk1 yk2 ,

and x and y are given as functions of X by Eqs. (8.21) and (8.22), allowing g N (x, y)
to be evaluated along this edge as a function of X .
The integral given by Eq. (8.23) is an integral over the interval 0 X 1. It may
therefore be approximated using quadrature techniques, as described in Sect. 3.5. On
each element edge, having calculated the nonzero local contributions, we then add
(k)
these contributions into the global vector b: entry blocal edge,i contributes to the entry
bki of b for i = 1, 2.

8.6.3 Setting Entries Defined Explicitly

Having assembled all entries of the linear system that are of the form given by
Eqs. (8.14) and (8.15), we then complete the assembly of the linear system by setting
8.6 Assembling the Algebraic Equations 151

the entries given by Eqs. (8.12) and (8.13). As explained in earlier chapters, it is
important to explicitly set all entries given by Eqs. (8.12) and (8.13), as incorrect
values may have been inserted into these entries while assembling the contributions
to Eqs. (8.14) and (8.15).

8.7 Quadrature Over Triangles

Assembling the linear system, as described in Sect. 8.6, requires us to evaluate inte-
grals of the form

h(X, Y ) d A X ,


over the canonical element, i.e. the region occupying X 0, Y 0 and 1 X Y


0. We evaluated some integrals of this form in Chap. 7, but these were integrals
of very simple functions that could be evaluated analytically. For general partial
differential equations defined by Eqs. (8.1)(8.3), the functions p(x, y), q(x, y),
r (x, y) and f (x, y) may be such that the integrals given by Eqs. (8.14) and (8.15) are
very difficult, or even impossible, to evaluate analytically. In these circumstances,
we require a numerical quadrature rule that allows us to approximate integrals over
triangular elements, similar to that described for functions of one variable in Sect. 3.5.
We now derive a suitable quadrature rule by using a map between the unit square
and the canonical triangle.
In ( X , Y )-coordinates, the unit square is given by 0 X 1, 0 Y 1. We
may map between the unit square in ( X , Y )-coordinates and the canonical triangle
in (X, Y )-coordinates using the transformation defined by

X = X ,
Y = (1 X )Y .

Using this transformation, an integral over the canonical triangle of a function writ-
ten in (X, Y )-coordinates is related to an integral over the unit square in ( X , Y )-
coordinates by
  1  1 
h(X, Y ) d A X = h(X, Y )(1 X ) d X dY .
 Y =0 X =0

We write this equation as


  1
h(X, Y ) d A X = h(Y ) dY , (8.24)
 Y =0
152 8 More General Elliptic Problems

where
 1
h(Y ) = h(X, Y )(1 X ) d X
X =0
 1
= h X , (1 X )Y (1 X ) d X .
X =0

This integral is over the interval 0 X 1 and may be approximated by Gaussian


quadrature as described in Sect. 3.5. Using M quadrature points, weights w1 ,
w2 , . . . , w M , and quadrature points Z 1 , Z 2 , . . . , Z M , we may write this approxi-
mation as


M
h(Y ) = wm h Z m , (1 Z m )Y (1 Z m ). (8.25)
m=1

Similarly, the integral on the right-hand side of Eq. (8.24) is an integral over the
interval 0 Y 1, and may be approximated, using the same Gaussian quadrature
scheme, by
 
M
h(X, Y ) d A X = wn h(Z n ). (8.26)
 n=1

Substituting Eqs. (8.25) into (8.26) yields our desired quadrature rule:
 
M 
M
h(X, Y ) d A X = wm wn (1 Z m )h (Z m , (1 Z m )Z n ) . (8.27)
 m=1 n=1

8.8 Computational Implementation

We calculate the finite element solution of the model problem defined, for 1 < x <
1, 1 < y < 1, by

  u u
ex u + 3x + 2y 6u = 2ex (1 + x + 6y) ,
x y

subject to Dirichlet boundary conditions

u(x, 1) = x 2 2, 1 < x < 1,


u(1, y) = 2y + 1, 3
1 < y < 1,
8.8 Computational Implementation 153

and natural Neumann boundary conditions

ex (u) n = 6ex , 1 < x < 1, y = 1,


ex (u) n = 2ex , x = 1, 1 < y < 1.

This boundary value problem has solution u(x, y) = x 2 + 2y 3 .


A computational implementation for calculating the solution of the model problem
above, using the material presented in this chapter, is given in Listing 8.1. This listing
follows a very similar pattern to that given in Listing 7.1, which we assume the reader
is familiar with; the discussion below focuses on the novel features of Listing 8.1.
The Matlab function again requires the parameters N x , N y that allow the mesh to
be specified as input and returns x, y, lnods and U. In lines 410, we specify the
functions that appear in Eqs. (8.1)(8.3). The function g N (x, y) is specified using two
separate functionsthe function gn1 specifies the Neumann boundary conditions on
y = 1, and the function gn2 specifies the Neumann boundary condition on x = 1.
The function GenerateMesh is called in line 13 of the listing. The body of this
function occupies lines 74115 of the listing. In addition to the arrays generated in
Listing 7.1, this function also generates an array neu_edges. This array has two
columns, and each row of this array stores the nodes at either end of an element edge
that lies on N . This array is generated in lines 109115 of the listing.
Having specified the mesh, we then initialise A and b (lines 1617), and loop
over all elements (lines 2129) calculating the nonzero local contributions from
integrating over each element, and adding these contributions into the global matrix
A and global vector b. The local contributions are calculated using the function
CalculateContributionOnElement that occupies lines 121190 of the list-
ing. This function first defines the locations of the nodes of the element (lines 126
131), calculates the determinant of the mapping from element k to the canoni-
cal element (line 135), and calculates the partial derivatives of basis functions on
each element (lines 139141). We then define the quadrature points and weights in
lines 144146, before initialising the local contributions to A and b. We then per-
form the quadrature required to evaluate the integrals given by Eqs. (8.18) and (8.19),
using the quadrature rule given by Eq. (8.27). We loop over the quadrature points,
first evaluating the basis functions (lines 157160), before evaluating the values of x
and y at the quadrature points (lines 163164). We then use these values of x and y
to evaluate the required function values (lines 167171). Finally, in lines 174190,
we use the quadrature rule to calculate the local entries of A and b.
Returning to the main body of the code, we loop over all element edges on N
in lines 3449, calculating local contributions to b using the function Calculate-
ContributionOnNeumannEdge, and adding these contributions into the global
vector b. The function CalculateContributionOnNeumannEdge is written
in lines 197233 of the listing. We begin by defining the location of the nodes of
the line segment in lines 202205, before calculating the length of the element in
line 208. The quadrature rule is defined in lines 211213. The local contribution
to b is initialised in line 216, before being calculated, using the quadrature rule, in
lines 229233.
154 8 More General Elliptic Problems

The Dirichlet boundary conditions are handled by lines 5256, completing the
assembly of the linear system. The linear system is then solved (lines 5962), and
the finite element solution plotted in lines 6568.
The Matlab function in Listing 8.1 may be executed by typing, for example,
[x, y, lnods, U] = Chap8_CalculateExampleFem(50, 50);

into a Matlab session.

Listing 8.1 Chap8_CalculateExampleFem.m, a Matlab function for calculating


 finite element solution of the model problem given by Eqs. (8.1)(8.3).
the
1 f u n c t i o n [x , y , lnods , U ] = C h a p 8 _ C a l c u l a t e E x a m p l e F e m ( Nx , Ny )
2
3 % Define functions
4 p = @ ( x , y ) exp ( x );
5 q1 = @ ( x , y ) 3* x ;
6 q2 = @ ( x , y ) 2* y ;
7 r = @ ( x , y ) -6;
8 f = @ ( x , y ) -2* exp ( x )*(1+ x +6* y );
9 gn1 = @ ( x , y ) 6* exp ( x );
10 gn2 = @ ( x , y ) 2* exp ( x );
11
12 % G e n e r a t e data s t r u c t u r e s n e e d e d to s p e c i f y the mesh
13 [ x , y , lnods , dir_nodes , n e u _ e d g e s ] = G e n e r a t e M e s h ( Nx , Ny );
14
15 % I n i t i a l i s e A and b to zero
16 A = s p a r s e (( Nx +1)*( Ny +1) , ( Nx +1)*( Ny +1));
17 b = zeros (( Nx +1)*( Ny +1) , 1);
18
19 % Loop over elements , c a l c u l a t i n g local c o n t r i b u t i o n s , and
20 % i n c r e m e n t i n g g l o b a l A and b
21 for k =1:2* Nx * Ny
22 [ A_local , b _ l o c a l ] = ...
23 C a l c u l a t e C o n t r i b u t i o n O n E l e m e n t ( x , y , lnods , k , ...
24 p , q1 , q2 , r , f );
25
26 A ( lnods (k ,:) , lnods (k ,:)) = ...
27 A ( lnods (k ,:) , lnods (k ,:)) + A _ l o c a l ;
28 b ( lnods (k ,:)) = b ( lnods (k ,:)) + b _local ;
29 end
30
31 % Loop over edges on y =1 , c a l c u l a t i n g local c o n t r i b u t i o n s
32 % from N e u m a n n b o u n d a r y conditions , and adding into the
33 % global b
34 for k =1: Nx
35 b _ l o c a l = C a l c u l a t e C o n t r i b u t i o n O n N e u m a n n E d g e (x , y , ...
36 neu_edges , k , gn1 );
37
38 b ( n e u _ e d g e s ( k ,:)) = b ( n e u _ e d g e s (k ,:)) + b _ l o c a l ;
39 end
40
41 % Loop over edges on x = -1 , c a l c u l a t i n g local c o n t r i b u t i o n s
42 % from N e u m a n n b o u n d a r y conditions , and adding into the
43 % global b
44 for k = Nx +1: Nx + Ny
45 b _ l o c a l = C a l c u l a t e C o n t r i b u t i o n O n N e u m a n n E d g e ( x , y , ...
46 neu_edges , k , gn2 );
47
48 b ( n e u _ e d g e s (k ,:)) = b ( n e u _ e d g e s ( k ,:)) + b _ l o c a l ;
49 end
8.8 Computational Implementation 155

50
51 % Handle Dirichlet boundary conditions
52 A ( dir_nodes ,:) = 0;
53 for i =1: l e n g t h ( d i r _ n o d e s )
54 A ( d i r _ n o d e s ( i ) , d i r _ n o d e s ( i )) = 1;
55 end
56 b ( d i r _ n o d e s ) = x ( d i r _ n o d e s ).^2 + 2* y ( d i r _ n o d e s ).^3;
57
58 % C o m p u t e ILU f a c t o r i s a t i o n of A
59 [ Lower , Upper ] = ilu ( A );
60
61 % Use ILU p r e c o n d i t i o n e d GMRES to solve linear system
62 U = gmres (A , b , 100 , [] , 1000 , Lower , Upper );
63
64 % Plot s o l u t i o n
65 t r i s u r f ( lnods , x , y , U );
66 x l a b e l ( x )
67 y l a b e l ( y )
68 z l a b e l ( U )
69
70
71
72
73 % F u n c t i o n to g e n e r a t e data s t r u c t u r e s r e q u i r e d by the mesh
74 f u n c t i o n [x , y , lnods , dir_nodes , n e u _ e d g e s ] = ...
75 G e n e r a t e M e s h ( Nx , Ny )
76
77 % Set e n t r i e s of l n o d s
78 lnods = zeros (2* Nx * Ny , 3);
79 for j =1: Ny ;
80 for i =1: Nx ;
81 ele = 2*( j -1)* Nx +2* i -1;
82 lnods ( ele ,:) = ...
83 [( j -1)*( Nx +1)+ i , (j -1)*( Nx +1)+ i +1 , j *( Nx +1)+ i ];
84
85 ele = ele +1;
86 lnods ( ele ,:) = ...
87 [ j *( Nx +1)+ i +1 , j *( Nx +1)+ i , (j -1)*( Nx +1)+ i +1];
88 end
89 end
90
91 % Set e n t r i e s of x and y over the i n t e r v a l from 0 to 1
92 x = zeros (( Nx +1)*( Ny +1) ,1);
93 y = zeros (( Nx +1)*( Ny +1) ,1);
94
95 for i =1: Nx +1
96 x ( i : Nx +1:( Nx +1)*( Ny +1)) = 2*( i -1)/ Nx -1;
97 end
98
99 for j =1: Ny +1
100 y (( j - 1 ) * ( Nx + 1 ) + 1 : j *( Nx +1)) = 2*( j -1)/ Ny -1;
101 end
102
103 % S p e c i f y l o c a t i o n s of D i r i c h l e t b o u n d a r y c o n d i t i o n s
104 % Nodes on y = -1
105 d i r _ n o d e s (1: Nx +1) = 1: Nx +1;
106 % Nodes on x =1
107 d i r _ n o d e s ( Nx +2: Nx + Ny +1) = 2*( Nx +1): Nx +1:( Ny +1)*( Nx +1);
108
109 % S p e c i f y l o c a t i o n s of N e u m a n n b o u n d a r y c o n d i t i o n s
110 % Edges on y =1
111 n e u _ e d g e s (1: Nx ,:) = [(( Ny +1)*( Nx +1): -1: Ny *( Nx +1)+2) , ...
112 (( Ny +1)*( Nx +1) -1: -1: Ny *( Nx +1)+1) ];
156 8 More General Elliptic Problems

113 % Edges on x = -1
114 n e u _ e d g e s ( Nx +1:2* Nx ,:) = [( Ny *( Nx +1)+1: -( Nx +1): Nx +2) , ...
115 (( Ny -1)*( Nx +1)+1: -( Nx +1):1) ];
116
117
118
119
120 % C a l c u l a t e local c o n t r i b u t i o n s to A and b from e l e m e n t k
121 f u n c t i o n [ A_local , b _ l o c a l ] = ...
122 C a l c u l a t e C o n t r i b u t i o n O n E l e m e n t ( x , y , lnods , k , p , ...
123 q1 , q2 , r , f )
124
125 % D e f i n e x and y c o o r d i n a t e of each node
126 x1 = x ( lnods (k ,1));
127 x2 = x ( lnods (k ,2));
128 x3 = x ( lnods (k ,3));
129 y1 = y ( lnods (k ,1));
130 y2 = y ( lnods (k ,2));
131 y3 = y ( lnods (k ,3));
132
133 % C a l c u l a t e J a c o b i a n of t r a n s f o r m a t i o n b e t w e e n c a n o n i c a l
134 % e l e m e n t and e l e m e n t k
135 detF = ( x2 - x1 )*( y3 - y1 ) - ( x3 - x1 )*( y2 - y1 );
136
137 % E v a l u a t e d e r i v a t i v e s of basis f u n c t i o n s in terms of (x , y )
138 % coordinates
139 Finv = [ y3 - y1 , x1 - x3 ; y1 - y2 , x2 - x1 ]/ detF ;
140 d p h i _ d X = [ -1 , -1; 1 , 0; 0 , 1];
141 d p h i _ d x = d p h i _ d X * Finv ;
142
143 % Define quadrature points
144 M = 3;
145 Q u a d W e i g h t s = [5/18 , 4/9 , 5 / 1 8 ] ;
146 Q u a d P o i n t s = [0.5*(1 - sqrt (0.6)) , 0.5 , 0 . 5 * ( 1 + sqrt ( 0 . 6 ) ) ] ;
147
148 % I n i t i a l i s e local A and b to zero
149 A _ l o c a l = zeros (3 ,3);
150 b _ l o c a l = zeros (3 ,1);
151
152 % Loop over q u a d r a t u r e p o i n t s to c a l c u l a t e local ...
153 % c o n t r i b u t i o n s to A and b
154 for n =1: M
155 for m =1: M
156 % Set v a l u e s of basis f u n c t i o n s
157 phi (1) = 1 - Q u a d P o i n t s ( m ) - ...
158 (1 - Q u a d P o i n t s ( m ))* Q u a d P o i n t s ( n );
159 phi (2) = Q u a d P o i n t s ( m );
160 phi (3) = (1 - Q u a d P o i n t s ( m ))* Q u a d P o i n t s ( n );
161
162 % Set v a l u e s of x and y
163 x_val = x1 * phi (1) + x2 * phi (2) + x3 * phi (3);
164 y_val = y1 * phi (1) + y2 * phi (2) + y3 * phi (3);
165
166 % E v a l u a t e f u n c t i o n s of x and y
167 p_val = p ( x_val , y_val );
168 q 1 _ v a l = q1 ( x_val , y_val );
169 q 2 _ v a l = q2 ( x_val , y_val );
170 r_val = r ( x_val , y_val );
171 f_val = f ( x_val , y_val );
172
173 % Add w e i g h t e d q u a d r a t u r e value to local A and b
174 for i =1:3
175 for j =1:3
8.8 Computational Implementation 157

176 A _ l o c a l ( i , j ) = A _ l o c a l (i , j ) + ...
177 detF * Q u a d W e i g h t s ( m )* ...
178 Q u a d W e i g h t s ( n )*(1 - Q u a d P o i n t s ( m ))* ...
179 ( p _ v a l *( d p h i _ d x (i ,1)* dph i _ d x ( j ,1) + ...
180 d p h i _ d x ( i ,2)* dphi_dx (j ,2)) + ...
181 q 1 _ v a l * phi ( i )* d p h i _ d x ( j ,1) + ...
182 q 2 _ v a l * phi ( i )* d p h i _ d x ( j ,2) + ...
183 r_val * phi ( i )* phi ( j ));
184 end
185 b _ l o c a l ( i ) = b _ l o c a l ( i ) + ...
186 detF * Q u a d W e i g h t s ( m )* Q u a d W e i g h t s ( n )* ...
187 (1 - Q u a d P o i n t s ( m ))* f_val * phi ( i );
188 end
189 end
190 end
191
192
193
194
195
196
197 f u n c t i o n b _ l o c a l = ...
198 C a l c u l a t e C o n t r i b u t i o n O n N e u m a n n E d g e (x , y , neu_edges , ...
199 k , gn )
200
201 % D e f i n e x and y c o o r d i n a t e of each node
202 x1 = x ( n e u _ e d g e s ( k ,1));
203 x2 = x ( n e u _ e d g e s ( k ,2));
204 y1 = y ( n e u _ e d g e s ( k ,1));
205 y2 = y ( n e u _ e d g e s ( k ,2));
206
207 % C a l c u l a t e l e n g t h of edge
208 h = sqrt (( x1 - x2 )^2 + ( y1 - y2 )^2);
209
210 % D e f i n e q u a d r a t u r e rule
211 M = 3;
212 Q u a d W e i g h t s = [5/18 , 4/9 , 5 / 1 8 ] ;
213 Q u a d P o i n t s = [0.5*(1 - sqrt (0.6)) , 0.5 , 0 . 5 * ( 1 + sqrt ( 0 . 6 ) ) ] ;
214
215 % I n i t i a l i s e local b to zero
216 b _ l o c a l = zeros (2 ,1);
217
218 % Loop over q u a d r a t u r e p o i n t s to c a l c u l a t e local
219 % c o n t r i b u t i o n s to b
220 for m =1: M
221 % Define basis f u n c t i o n s at each q u a d r a t u r e point
222 phi = [1 - Q u a d P o i n t s ( m ); Q u a d P o i n t s ( m )];
223
224 % D e f i n e v a l u e s of x and y
225 x_val = x1 * phi (1) + x2 * phi (2);
226 y_val = y1 * phi (1) + y2 * phi (2);
227
228 % E v a l u a t e g_N
229 g n _ v a l = gn ( x_val , y_val );
230
231 % Add w e i g h t e d c o n t r i b u t i o n to b
232 b _ l o c a l = b _ l o c a l + h * Q u a d W e i g h t s ( m )* g n _ v a l * phi ;
233 end
158 8 More General Elliptic Problems

8.9 Exercises

8.1 Calculate the finite element solution of the boundary value problem, defined for
0 < x < 2, 0 < y < 1, by
 
2u 2u u u
4 + +4 +2 + 2u = 1,
x2 y2 x y

subject to the Dirichlet boundary conditions

1
u(x, 0) = ex + , 0 < x < 2,
2
1
u(2, y) = e y+2
+ , 0 < y < 1,
2
1
u(x, 1) = ex+1 + , 0 < x < 2,
2
1
u(0, y) = e y + , 0 < y < 1.
2

The solution of this boundary value problem is u(x, y) = ex+y + 21 . Use this analytic
solution to verify that your finite element solution is correct.

8.2 Calculate the finite element solution of the boundary value problem, defined for
0 < x < 2, 1 < y < 0, by
2 2 u u
ex y u + ex + ey ey u = 2xe y 2,
x y

subject to the Dirichlet boundary conditions


2
u(x, 0) = ex , 0 < x < 2,
u(2, y) = e y+4 , 1 < y < 0,
x 2 1
u(x, 1) = e , 0 < x < 2,
u(0, y) = e , y
1 < y < 0.
2
+y
The solution of this boundary value problem is u(x, y) = ex . Use this analytic
solution to verify that your finite element solution is correct.

8.3 Modify your solution to Exercise 8.2 to calculate the finite element solution of
the boundary value problem, defined for 0 < x < 2, 1 < y < 0, by
2 2 u u
ex y u + ex + ey ey u = 2xe y 2,
x y
8.9 Exercises 159

subject to the Dirichlet boundary conditions


2
u(x, 0) = ex , 0 < x < 2,
u(2, y) = e y+4 , 1 < y < 0,

and the Neumann boundary conditions

ex y (u) n = 1
2
0 < x < 2, y = 1,
ex y (u) n = 0
2
x = 0, 1 < y < 0.
2
+y
Use the true solution, u(x, y) = ex , to verify that your finite element solution is
correct.

8.4 Define the region by the intersection of the regions given by

x > 0, y > 0, y < 1, x + y < 11.

The partial differential equation is defined by


 
2u 2u
+ 2 = 4, (x, y) ,
x 2 y

subject to the Dirichlet boundary conditions

u(x, 0) = x 2 , 0 < x < 11,


u(0, y) = y , 2
0 < y < 1,

and Neumann boundary conditions

(u) n = 2,
0 < x < 10, y = 1,
(u) n = 2(x + y), 10 < x < 11, x + y = 11.

This boundary value problem has solution u(x, y) = x 2 + y 2 .

(a) Modify the mesh generated in Listing 8.1 so that it covers the region given by
0 < x < 10, 0 < y < 1.
(b) Scale the values of xi at each node in the mesh generated in part (a) by a factor
of 1 + 0.1(1 yi ), and verify that this new mesh partitions the region .
(c) Use the mesh generated in part (b) to calculate the finite element solution of the
boundary value problem above. Verify that your finite element solution is correct
by comparison with the true solution.

8.5 A more general partial differential equation than that given by Eq. (8.1) is defined
on some region by
160 8 More General Elliptic Problems

(P(x, y)u) + q(x, y) u + r (x, y)u = f (x, y), (x, y) ,

where P(x, y) is a 2 2 matrix with entries given by


 
P11 (x, y) P12 (x, y)
P(x, y) = ,
P21 (x, y) P22 (x, y)

and P11 , P12 , P21 , P22 , q, r, f are given functions. On the boundary D , we apply
Dirichlet boundary conditions:

u(x, y) = g D (x, y), (x, y) D ,

and on the boundary N , we apply natural Neumann boundary conditions:

(Pu) n = g N (x, y), (x, y) N ,

where n is the normal vector to the boundary N , pointing out of the domain ,
and of unit length.
(a) Write down the weak formulation of the boundary value problem above.
(b) Note that, if
 
4 0
P(x, y) = ,
0 1

then

2u 2u
(P(x, y)u) = 4 + .
x2 y2

Calculate the finite element solution of the partial differential equation, defined
for 0 < x < 1, 0 < y < 1, by
 
2u 2u  
4 2+ 2 + 2x + 4x 2 y 2 + 4y 4 u = 0,
x y

subject to Dirichlet boundary conditions

u(x, 0) = 1, 0< x < 1,


2
u(1, y) = ey , 0< y < 1,
u(x, 1) = ex , 0< x < 1,
u(0, y) = 1, 0< y < 1.
2
The solution of this differential equation is u(x, y) = ex y . Use this analytic
solution to verify that your finite element solution is correct.
Chapter 9
Quadrilateral Elements

In Chaps. 7 and 8, we calculated the finite element solution of partial differential


equations, having partitioned the domain on which the differential equation is defined
into a mesh of triangular elements. Triangular elements are not, however, the only
shape of elements that can be used when partitioning the domain. In this chapter, we
explain how a mesh comprising quadrilateral elements may be used when calculating
the finite element solution of a partial differential equation.

9.1 A Model Problem

We use the same model differential equation as in Chap. 8:

( p(x, y)u) + q(x, y) u + r (x, y)u = f (x, y), (x, y) , (9.1)

where is some finite region of the (x, y)-plane, and p(x, y), q(x, y), r (x, y) and
f (x, y) are given functions. The boundary, , is partitioned into two regions, D
and N . On the boundary D , we apply Dirichlet boundary conditions:

u(x, y) = g D (x, y), (x, y) D , (9.2)

and on the boundary N , we apply natural Neumann boundary conditions:

p(x, y) (u) n = g N (x, y), (x, y) N , (9.3)

where n is the normal vector to the boundary N , pointing out of the domain ,
and of unit length.

Springer International Publishing AG 2017 161


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_9
162 9 Quadrilateral Elements

9.2 The Weak Formulation

The weak formulation of Eqs. (9.1)(9.3) is identical to that derived in Sect. 8.2 and
is given by:

find u(x, y) HE1 (), such that


  
p (v) (u) + vq u + r uv d A = f v dA + vg N ds, (9.4)
N

for all v(x, y) H01 ().

9.3 The Computational Mesh

In the exercises at the end of this chapter, we will develop computational imple-
mentations for calculating the finite element solution of boundary value problems.
These boundary value problem will be posed on rectangular domains a < x < b,
c < y < d, where a, b, c, d are constants, and so we now explain how to partition a
rectangular domain into a mesh of quadrilateral elements.
A rectangular domain may easily be partitioned into a mesh of uniform rectangles,
with N x rectangles in the x-direction and N y rectangles in the y-direction, as shown in
Fig. 9.1 when N x = 2 and N y = 3. As with the mesh of triangular elements developed
in Sect. 7.3.1, we number the nodes, starting with the node at the bottom left-hand
corner of the mesh, and number the nodes consecutively moving in the x-direction
from this first node. We then move up to the next row in the y-direction and repeat
this process, starting from the node at the left-hand end of these nodes. We proceed
through the mesh in this fashion until all nodes have been numbered. This numbering
convention is illustrated in Fig. 9.1a when N x = 2 and N y = 3. A similar process
is used to number the elementsthis is illustrated in Fig. 9.1b when N x = 2 and
N y = 3.
As when using a mesh of triangular elements, we will require a connectivity
array, again called lnods, to store the nodes of each element. Row k of this array will
store the nodes of element k, listed in an anticlockwise direction. Each quadrilateral
element will contain four nodes and so, if there are Nele elements in the mesh, this
array will be of size Nele 4. For the mesh shown in Fig. 9.1, where N x = 2 and
N y = 3, the connectivity array may be written as

1 2 5 4
2 3 6 5

4 5 8 7
lnods =
5
.
6 9 8
7 8 11 10
8 9 12 11
9.3 The Computational Mesh 163

(a) (b)
10 11 12
1 1

0.8 0.8 5 6
7 8 9
0.6 0.6
y

y
3 4
0.4 4 5 6 0.4

0.2 0.2
1 2
1 2 3
0 0
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1
x x

Fig. 9.1 a The labelling of the nodes in the mesh. b The labelling of the elements in the mesh

In common with a mesh of triangular elements, the connectivity array is not unique.
First, the order that the elements of the mesh are stored in does not matter, and so we
may interchange any of the rows of this array. Furthermore, we only have to list the
nodes of each element in an anticlockwise directionthe first row of the connectivity
array above could be written 2, 5, 4, 1, or 5, 4, 1, 2 or 4, 1, 2, 5 instead of 1, 2, 5, 4.
In Sect. 7.3.1, we defined a hanging node to be a point in the domain that
is a node of one element, but lies on an edge of another element without being a
node. We illustrate an example of a hanging node in a mesh of quadrilateral elements
in Fig. 9.2, with the hanging node identified by an open circle. As with triangular
elements, we do not permit hanging nodes in a mesh of quadrilateral elements.
Finally, we reiterate the warning about long, thin elements that we first gave
in Sect. 7.3.1. Symptoms of long, thin elements include the following: (i) a high
ratio between the length of the longest edge and the length of the shortest edge of
an element; (ii) a very low interior angle between edges of an element; or (iii) an
interior angle between edges of an element that is close to 180 . Elements such
as these should be avoided, as they can degrade the accuracy of the finite element
solution.

Fig. 9.2 An example of a


hanging node 1

0.8

0.6
y

0.4

0.2

0
0 0.2 0.4 0.6 0.8 1
x
164 9 Quadrilateral Elements

9.4 Basis Functions

In the previous chapters, whether calculating the finite element solution of an ordi-
nary differential equation or a partial differential equation, we defined global basis
functions through: (i) a transformation between a general element of the mesh and
a canonical element and (ii) the definition of local basis functions on this canonical
element. For a mesh of quadrilateral elements, a suitable canonical element is the
unit square in (X, Y )-coordinates, defined by 0 X 1, 0 Y 1, and illustrated
in Fig. 9.3.
Suppose element k of the mesh includes nodes the k1 , k2 , k3 , k4 , listed in an
anticlockwise direction. A mapping between this element (in (x, y)-coordinates)
and the canonical element (in (X, Y )-coordinates) is defined by, for 0 X 1,
0 Y 1:

x = (1 X )(1 Y )xk1 + X (1 Y )xk2 + X Y xk3 + (1 X )Y xk4 , (9.5)


y = (1 X )(1 Y )yk1 + X (1 Y )yk2 + X Y yk3 + (1 X )Y yk4 . (9.6)

Local basis functions on the canonical element are defined by, for 0 X 1,
0 Y 1,

local,1 (X, Y ) = (1 X )(1 Y ), (9.7)


local,2 (X, Y ) = X (1 Y ), (9.8)
local,3 (X, Y ) = X Y, (9.9)
local,4 (X, Y ) = (1 X )Y. (9.10)

The local basis functions defined by Eqs. (9.7)(9.10) are known as bilinear local
basis functions, as they are products of a linear function of X and a linear function
of Y . They are not, however, linear functions when viewed as a function of both X

A general element in (x,y) coordinates The canonical elementin (X,Y ) coordinates


1
(xk4 , yk4 )
(xk3 , yk3 )
0.8

0.6
Y
y

0.4

0.2
(xk2 , yk2 )
(xk1 , yk1 ) 0

0 0.2 0.4 0.6 0.8 1


x X

Fig. 9.3 A general element and the canonical element


9.4 Basis Functions 165

local,1 (X,Y ) local,2 (X,Y )

1 1

0.5 0.5

1 1
0 0.5 0 0.5
0 0.5 0 0 0.5 0
1 Y 1 Y
X X

local,3 (X,Y ) local,4 (X,Y )

1 1

0.5 0.5

1 1
0 0.5 0 0.5
0 0.5 0 0 0.5 0
1 Y 1 Y
X X

Fig. 9.4 The local basis functions local,1 , local,2 , local,3 , local,4 defined by Eqs. (9.7)(9.10)

and Y however, as they include the quadratic term X Y . These local basis functions
are illustrated in Fig. 9.4.
Global basis functions are now defined on element k, in terms of the local basis
functions given by Eqs. (9.7)(9.10), by

k1 (X, Y ) = local,1 (X, Y ), (9.11)


k2 (X, Y ) = local,2 (X, Y ), (9.12)
k3 (X, Y ) = local,3 (X, Y ), (9.13)
k4 (X, Y ) = local,4 (X, Y ), (9.14)
j (X, Y ) = 0, j  = k1 , j  = k2 , j  = k3 , j  = k4 . (9.15)

We illustrate an example global basis function as a function of x and y in Fig. 9.5.


Suppose the mesh contains Nnode nodes. We leave it as an exercise for the reader
to verify whether the global basis functions defined by Eqs. (9.5)(9.15) satisfy the
usual property that, for j = 1, 2, . . . , Nnode ,

1, i = j,
j (xi ) = (9.16)
0, i = j.
166 9 Quadrilateral Elements

Fig. 9.5 An example global


basis function

9.5 Sets of Functions

We now define the sets of functions S h , S Eh , S0h that will be used when specifying the
finite element solution. We define

N node

S = functions V (x, y) given by V (x, y) =


h
V j j (x, y) where j (x, y),
j=1

j = 1, 2, . . . , Nnode , are defined by Eqs. (9.5)(9.15) .

The sets S Eh and S0h are then defined by



S Eh = functions V (x, y) in S h such that V (x, y) satisfies all Dirichlet boundary

conditions ,

S0h = functions V (x, y) in S h such that V (x, y) = 0 at all points(x, y) where

Dirichlet boundary conditions are specified .

9.6 The Finite Element Formulation

Having defined the weak solution by Eq. (9.4), the sets S Eh and S0h defined in Sect. 9.5
allow us to define the finite element solution by:
9.7 The System of Algebraic Equations 167

find U (x, y) S Eh , such that


  
p (i ) (U ) + i q U + rU i d A = f i d A + i g N ds,
N
(9.17)

for all i (x, y) S0h .

9.7 The System of Algebraic Equations

The finite element solution is written in the usual form:

N node

U (x, y) = U j j (x, y), (9.18)


j=1

where Nnode is the number of nodes in the mesh. It is easy to see that U (x, y), as
defined above, lies in the set S h .
Although the basis functions, 1 , 2 , . . . , Nnode , used in this chapter are different
from the basis functions used in Chap. 8, both the form of the finite element solution,
Eq. (9.18), and the finite element formulation, Eq. (9.17), are identical to those given
in Eqs. (8.7) and (8.8) in Chap. 8. As a consequence, we may use the same arguments
as in Sect. 8.5.3 to deduce that the algebraic equations satisfied by U1 , U2 , . . . , U Nnode
may be written as:

AU = b, (9.19)

where, if node i lies on D ,



1, j = i,
Ai, j = , (9.20)
0, j = i,
bi = g D (xi , yi ), (9.21)

and if node i does not lie on D ,




Ai, j = p (i ) j + i q j + r i j d A,

j = 1, 2, . . . , Nnode , (9.22)
 
bi = f i d A + i g N ds. (9.23)
N
168 9 Quadrilateral Elements

The entries of A and b given by Eqs. (9.22) and (9.23) are therefore written in an
identical form to those given by Eqs. (8.14) and (8.15). They differ, however, as the
basis functions used in this chapter are different to those used in Chap. 8.

9.8 Assembling the System of Algebraic Equations

We will follow the usual procedure when calculating the entries of the linear system
given by Eq. (9.19)that is, we first evaluate the entries, given by Eqs. (9.22) and
(9.23), that are given by integrals over the domain, , and integrals over the part
of the boundary where Neumann boundary conditions are applied, N . As before,
the integrals over will be calculated by decomposing the integral into the sum of
integrals over individual elements, and the integrals over N will be calculated by
decomposing the integral into the sum of integrals over element edges that lie on
N . We therefore write Eqs. (9.22) and (9.23), the equations for row i of the linear
system given by Eq. (9.19), where node i of the mesh does not lie on D , as

Nele 


Ai, j = p (i ) j + i q j + r i j d A,
k=1 ek

j = 1, 2, . . . , Nnode , (9.24)
Nele 

NNeu 

bi = f i d A + i g N ds, (9.25)
k=1 ek k=1 e Nk

where Nele is the number of elements in the mesh, ek is the region occupied by ele-
ment k, NNeu is the number of element edges that lie on N , and e Nk is edge k of
the element edges that lie on N . We note that Eqs. (9.24) and (9.25) appear iden-
tical to Eqs. (8.16) and (8.17). The difference is that the basis functions i (x, y) are
now defined by Eqs. (9.5)(9.15) rather than Eqs. (7.12)(7.20). We may, however,
assemble these entries of A and b in the same fashion as in Sects. 8.6.1 and 8.6.2
that is, we first calculate the local contributions from integrating over each element,
before calculating the local contributions from integrating along each element edge
that lies on N .
Having assembled the entries of the linear system that are of the form given by
Eqs. (9.22) and (9.23), we then set the entries given by Eqs. (9.20) and (9.21) to
complete the assembly of the linear system. We now describe each of these steps in
a little more detail.
9.8 Assembling the System of Algebraic Equations 169

9.8.1 Evaluating Integrals Over

We begin by calculating the contributions to Eqs. (9.24) and (9.25) that arise from
integrating over each element. Suppose element k contains the nodes k1 , k2 , k3 ,
k4 , listed in an anticlockwise order. The only basis functions that are nonzero on
this element are k1 (x), k2 (x), k3 (x), k4 (x), and so the nonzero contributions to
(k)
Eq. (9.24) from integrating over this element can be stored in a matrix Alocal of size
4 4, with entries given by, for i = 1, 2, 3, 4, j = 1, 2, 3, 4:

(k)
Alocal,i, j = p(x, y) ki k j + ki q(x, y) k j + r (x, y)ki k j d A,
ek

(k)
where entry Alocal,i, j contributes to entry Aki ,k j of the global matrix A.
Similarly, the only nonzero contributions to Eq. (9.25) from integrating over this
(k)
element may be stored in a vector with four entries, blocal , with entries given by, for
i = 1, 2, 3, 4:

(k)
blocal,i = f (x, y)ki d A,
ek

(k)
where entry blocal,i contributes to entry bki of the global vector b.
As in earlier chapters, we will find it convenient to write these local contributions
as integrals over the canonical element, in this case the unit square given by 0
X 1, 0 Y 1. Using the transformation between element k and the canonical
element given by Eqs. (9.5)(9.6), and the definition of local basis functions given
by Eqs. (9.11)(9.14), we may write, for i = 1, 2, 3, 4, j = 1, 2, 3, 4:
    
(k)
1 1
local,i local, j local,i local, j
Alocal,i, j = p(x, y) + +
Y =0 X =0 x x y y
 
local, j local, j
local,i q1 (x, y) + q2 (x, y) +
x y
 
r (x, y)local,i local, j det(F) dX dY,
(9.26)

where F is the Jacobian matrix of the transformation, defined by


 
x x
X Y
F= y y ,
X Y

which may be computed using the definitions of x and y given by Eqs. (9.5)(9.6).
To evaluate the integral that appears in Eq. (9.26), we must first write all terms
in the integrand as functions of X and Y . We may use the chain rule to evaluate the
170 9 Quadrilateral Elements

partial derivatives of the basis functions that appear in the integral, as was done for
basis functions on a triangular element in Eq. (7.39):
local,1 local,1
local,1 local,1
x y
local,2 local,2 X Y
local,2 local,2  X X 
X x y
x y Y
local,3 local,3 = local,3 local,3 Y Y
X
x y Y x y
local,4 local,4 local,4 local,4
x y X Y

(1 Y ) (1 X )
1Y X
=

F 1 ,
(9.27)
Y X
Y 1 X

where we have used the definition of local basis functions given by Eqs. (9.7)(9.10)
to evaluate the partial derivatives of these local basis functions with respect to X and
Y , and noted that
X X
  x 1
x
x y
Y Y
= X Y
y y
x y X Y

= F 1 .

On using Eq. (9.27), we may now express all the partial derivatives that appear in
Eq. (9.26) as functions of X and Y . Note that, in contrast to the partial derivatives of
linear basis functions in Chaps. 7 and 8, the partial derivatives of the bilinear basis
functions used in this chapter are not constant on each element.
Functions such as p(x, y) may be evaluated as functions of X and Y by first
using Eqs. (9.5)(9.6) to calculate x and y as functions of X and Y , before using
these expressions for x and y to evaluate p(x, y). We may now evaluate all terms
that appear in the integrand of Eq. (9.26) as functions of X and Y . If this integral
cannot be evaluated analytically, we may use quadrature to approximate its value
numerically; we explain in Sect. 9.9 how quadrature may be performed over the unit
square 0 X 1, 0 Y 1.
Using the same change of variable as above, we may write the local nonzero
contributions to b from element k as, for i = 1, 2, 3, 4:
 1  1 
(k)
blocal,i = f (x, y) local,i (X, Y ) det(F) dX dY. (9.28)
Y =0 X =0

The function f (x, y) may be evaluated on the canonical element as described earlier
for functions such as p(x, y). The integral above may then be evaluated analytically,
or approximated numerically, using quadrature.
9.9 Quadrature 171

9.8.2 Evaluating Integrals Over N

Having assembled the contributions to Eqs. (9.24) and (9.25) from integration over
elements, we now assemble the contribution to (9.25) from integration over element
edges that lie on N . This may be done in an identical fashion to that described in
Sect. 8.6.2.

9.8.3 Setting Entries Defined Explicitly

Finally, we set the entries of A and b given by Eqs. (9.20) and (9.21). At this stage,
it is important to correct any incorrect entries that have been introduced into these
rows earlier in the assembly processsee Sect. 8.6.3 for more details.

9.9 Quadrature

When calculating the contributions to the global matrix A and global vector b that
are given by Eqs. (9.26) and (9.28), we have to evaluate integrals of the form
 1  1 
h(X, Y ) dX dY. (9.29)
Y =0 X =0

Writing this double integral as


 1  1   1
h(X, Y ) dX dY = h(Y ) dY, (9.30)
Y =0 X =0 Y =0

where
 1
h(Y ) = h(X, Y ) dX, (9.31)
X =0

we will now show that we may evaluate Eq. (9.29) using numerical quadrature,
by adapting the material presented in Sect. 3.5. Suppose we have a quadrature
rule for integrating over the interval 0 Z 1 that uses m quadrature points
Z 1 , Z 2 , . . . , Z m , with weights w1 , w2 , . . . , w M . We may then write Eq. (9.31) as

M
h(Y ) = wm h(Z m , Y ).
m=1

Substituting this into Eq. (9.30) gives


172 9 Quadrilateral Elements

 1  1 

M  1
h(X, Y ) dX dY = wm h(Z m , Y ) dY,
Y =0 X =0 m=1 Y =0

where we have interchanged the order of integration and summation. We then apply
the same quadrature rule to the integral over 0 Y 1 on the right-hand side of the
equation above to obtain
 1  1 

M
h(X, Y ) dX dY = wm wn h(Z m , Z n ),
Y =0 X =0 m=1 n=1

which may be used to approximate the integrals given by Eqs. (9.26) and (9.28).

9.10 Exercises

We suggest that the reader begins with the exercise below. Other suitable exercises
are then the exercises at the end of Chap. 8, where a finite element solution to the
boundary value problems given in these exercises can be calculated using a mesh of
quadrilateral elements and bilinear basis functions.

9.1 In Listing 8.1, we provided a computational implementation for calculating the


finite element solution, using linear basis functions on a mesh of triangular elements,
for the boundary value problem defined, for 1 < x < 1, 1 < y < 1, by

u u
ex u + 3x + 2y 6u = 2ex (1 + x + 6y) ,
x y

subject to Dirichlet boundary conditions

u(x, 1) = x 2 2, 1 < x < 1,


u(1, y) = 2y + 1,3
1 < y < 1,

and natural Neumann boundary conditions

ex (u) n = 6ex , 1 < x < 1, y = 1,


ex (u) n = 2ex , x = 1, 1 < y < 1.

This boundary value problem has solution u(x, y) = x 2 + 2y 3 .


In this exercise, we will modify Listing 8.1 so that it solves the boundary value
problem using bilinear global basis functions.
9.10 Exercises 173

(a) Lines 7889 of Listing 8.1 generate the connectivity array lnods for a mesh of
triangular elements. Modify this function so that it generates the connectivity
array for a mesh of quadrilateral elements, discussed in Sect. 9.3.
(b) Modify the function CalculateContributionOnElement so that it com-
putes the nonzero local contributions to the linear system that arise from integrat-
(k) (k)
ing over element k, Alocal and blocal , discussed in Sect. 9.8.1. When modifying
this function, you should remember the following: (i) there are now four nodes
(k)
in each element, and so there will be four local basis functions, and Alocal and
(k)
blocal will be of a different size; (ii) expressions for the Jacobian of the transfor-
mation F and local basis functions will be different; (iii) the Jacobian and partial
derivatives of the basis functions will no longer be constant over the element;
and (iv) the quadrature rule for integrating over the canonical square element is
different to the quadrature rule for integrating over the canonical triangle that is
used in Listing 8.1.
(c) Make any other modifications that are needed, such as the number of elements
that are looped over in lines 2129 of Listing 8.1, and verify your implementation
is correct by comparison with the true solution.
Chapter 10
Higher Order Basis Functions

In Chap. 4, we described the use of quadratic, and higher order, basis functions
when calculating the finite element solution of ordinary differential equations. In this
chapter, we will explain how higher order basis functions may be used to calculate the
finite element solution of a partial differential equation. We give a concrete example
of the use of quadratic basis functions on a mesh of triangular elements. The exercises
at the end of the chapter contain examples of higher order basis functions on meshes
of both triangular and quadrilateral elements.

10.1 The Model Boundary Value Problem and Weak


Formulation

We use the same model differential equation as in Chaps. 8 and 9:

( p(x, y)u) + q(x, y) u + r (x, y)u = f (x, y), (x, y) , (10.1)

where is some finite region of the (x, y)-plane, and p(x, y), q(x, y), r (x, y) and
f (x, y) are given functions. The boundary, , is partitioned into two regions, D
and N . On the boundary D , we apply Dirichlet boundary conditions:

u(x, y) = g D (x, y), (x, y) D , (10.2)

and on the boundary N , we apply natural Neumann boundary conditions:

p(x, y) (u) n = g N (x, y), (x, y) N , (10.3)

Springer International Publishing AG 2017 175


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_10
176 10 Higher Order Basis Functions

where n is the normal vector to the boundary N , pointing out of the domain
and of unit length. The weak solution, derived in Sect. 8.2, is defined by:
find u(x, y) HE1 (), such that
  
p (v) (u) + vq u + r uv d A = f v dA + N vg N ds, (10.4)

for all v(x, y) H01 ().

10.2 Quadratic Basis Functions on a Mesh of Triangular


Elements

We partition the mesh into triangular elements, as described in Sect. 7.3.1. A quadratic
approximation to the solution on an element will take the form

a1 + a2 x + a3 y + a4 x 2 + a5 x y + a6 y 2 ,

and will be specified by the six values ai , i = 1, 2, . . . , 6. As we have six degrees


of freedom on each element, we will require six basis functionsand therefore six
nodeson each element. Each triangular element of the mesh used in Sect. 7.3.1
contained three nodes, one node at each vertex. By placing an extra node at the mid-
point of each edge of the triangle, as shown in Fig. 10.1, we have then assigned six
nodes to each element of the mesh. The global numbering of the nodes is shown in
Fig. 10.1; the numbering of the elements is identical to that shown in Fig. 7.2b.
We will require a connectivity array, again denoted by lnods, for the elements
and nodes in the mesh. When using linear basis functions, row k of this array stored
the nodes contained by element k, listed in an anticlockwise direction. For quadratic
basis functions, the first three entries of row k of the connectivity array will contain
the three nodes at the vertices of element k, again listed in an anticlockwise direction.
The next three entries of row k will contain the three nodes at the mid-points of each
edge of this element, listed in an anticlockwise order starting from the node opposite
the first vertex listed. For example, the row of the connectivity array corresponding
to the element shown in Fig. 10.2 could be

k1 , k2 , k3 , k4 , k5 , k6 ,
or k2 , k3 , k1 , k5 , k6 , k4 ,
or k3 , k1 , k2 , k6 , k4 , k5 .

The connectivity array for the mesh shown in Fig. 10.1 could be written as
10.2 Quadratic Basis Functions on a Mesh of Triangular Elements 177

1.2

31 32 33 34 35
1

26 27 28 29 30
0.8

21 22 23 24 25

0.6
y

16 17 18 19 20

0.4
11 12 13 14 15

0.2 6 7 8 9 10

1 2 3 4 5
0

0 0.2 0.4 0.6 0.8 1 1.2


x

Fig. 10.1 The nodes and elements of a mesh of triangular elements that may be used with quadratic
basis functions

(xk3 , yk3 )

(xk4 , yk4 )
(xk5 , yk5 )
y

(xk2 , yk2 )
(xk6 , yk6 )
(xk1 , yk1 )

Fig. 10.2 The nodes of an element


178 10 Higher Order Basis Functions

1 3 11 7 6 2
13 11 3 7 8 12

3 5 13 9 8 4

15 13 5 9 10 14

11 13 21 17 16 12

23 21 13 17 18 22
lnods =
13
.
15 23 19 18 14
25 23 15 19 20 24

21 23 31 27 26 22

33 31 23 27 28 32

23 25 33 29 28 24
35 33 25 29 30 34

Global basis functions will, as in Chaps. 7 and 8, be defined by mapping each


element of this mesh onto the canonical triangular element and defining local basis
functions on this canonical element. Suppose the nodes of element k are k1 , k2 , k3 , k4 ,
k5 , k6 , listed in the order prescribed above for the connectivity array lnods. We again
map between element k and the canonical element X 0, Y 0, 1 X Y 0
using the usual transformation:

x = (1 X Y )xk1 + X xk2 + Y xk3 , (10.5)


y = (1 X Y )yk1 + X yk2 + Y yk3 . (10.6)

Local quadratic basis functions local,i (X, Y ), i = 1, 2, . . . , 6 are defined on the


canonical element by

1
local,1 (X, Y ) = 2(1 X Y ) X Y , (10.7)
2

1
local,2 (X, Y ) = 2X X , (10.8)
2

1
local,3 (X, Y ) = 2Y Y , (10.9)
2
local,4 (X, Y ) = 4X Y, (10.10)
local,5 (X, Y ) = 4Y (1 X Y ), (10.11)
local,6 (X, Y ) = 4X (1 X Y ). (10.12)

These local basis functions are shown in Fig. 10.3.


Global basis functions are now defined by

ki (X, Y ) = local,i (X, Y ), i = 1, 2, . . . , 6, (10.13)


j (X, Y ) = 0, j = ki , i = 1, 2, . . . , 6. (10.14)
10.2 Quadratic Basis Functions on a Mesh of Triangular Elements 179

local,1 (X,Y ) local,2 (X,Y ) local,3 (X,Y )


1 1 1

0.5 0.5 0.5

0 0 0

0.5 1 0.5 1 0.5 1


0 0.5 0 0.5 0 0.5
0.5 1 0 0.5 1 0 0.5 1 0
Y Y Y
X X X

local,4 (X,Y ) local,5 (X,Y ) local,6 (X,Y )


1 1 1

0.5 0.5 0.5

0 1 0 1 0 1
0 0.5 0 0.5 0 0.5
0.5 1 0 0.5 1 0 0.5 1 0
Y Y Y
X X X

Fig. 10.3 The local basis functions

(a) 1 (b) 1

0.5 0.5

0 0
4 4
0 2 0 2
2 0 2 0
4 y 4 y
x x

Fig. 10.4 Some example basis functions

Some example basis functions are shown in Fig. 10.4. In Fig. 10.4a, we show an
example basis function that takes the value 1 at a node that is at the vertex of a
triangle. In Fig. 10.4b, we show an example basis function that takes the value 1 at a
node that is at the mid-point of the edge of a triangle.
Remember that nodes that are not at a vertex of a triangular element are placed at
the mid-point of an edge of an element. Suppose the mesh contains Nnode nodes. It is
then straightforward to verify that the basis functions given by Eqs. (10.5)(10.14)
satisfy, for j = 1, 2, . . . , Nnode , the usual condition:

1, i = j,
j (xi ) = (10.15)
0, i = j.
180 10 Higher Order Basis Functions

10.3 The Finite Element Solution

The sets of functions S h , S Eh and S0h are now defined by

N
node
S = functions V (x, y) given by V (x, y) =
h
V j j (x, y), where j (x, y),
j=1

j = 1, 2, . . . , Nnode , are defined by Eqs. (10.5)(10.14) ,

S Eh = functions V (x, y) in S h such that V (x, y) satisfies all Dirichlet boundary

conditions ,

S0h = functions V (x, y) in S h such that V (x, y) = 0 at all points (x, y) where

Dirichlet boundary conditions are specified .

These sets, and the weak formulation of the problem given by Eq. (10.4), allow us to
define the finite element solution by

find U (x, y) S Eh , such that


  
p (i ) (U ) + i q U + rU i d A = f i d A + i g N ds,
N
(10.16)

for all i (x, y) S0h .

10.4 The Algebraic Equations

We write the finite element solution as a member of the set S h given by


N node

U (x, y) = U j j (x, y), (10.17)


j=1

where Nnode is the number of nodes in the mesh. As explained in Sects. 8.5.3 and 9.7,
the algebraic equations satisfied by U1 , U2 , . . . , U Nnode may be written as

AU = b, (10.18)
10.4 The Algebraic Equations 181

where, if node i lies on D , to ensure that U (x, y) S Eh , we have



1, j = i,
Ai, j = , (10.19)
0, j = i,
bi = g D (xi , yi ), (10.20)

and if node i does not lie on D ,



   
Ai, j = p (i ) j + i q j + r i j d A,

j = 1, 2, . . . , Nnode , (10.21)
 
bi = f i d A + i g N ds. (10.22)
N

10.5 Assembling the Algebraic Equations

The entries of the linear system given by Eq. (10.18) are assembled in the same
way as they have been throughout this book. We first assemble the entries given
by Eqs. (10.21) and (10.22). There are two components to handle when assembling
these entries. First, we calculate the terms in Eqs. (10.21) and (10.22) that arise from
integrating over . We then calculate the terms in Eq. (10.22) that arise from inte-
grating over N . Having calculated the entries given by Eqs. (10.21) and (10.22),
we then set the entries given by Eqs. (10.19) and (10.20). We now give more details
on how these tasks are performed for quadratic basis functions. We assume that the
reader is familiar with the assembly of the linear system described in Sect. 8.6 when
linear basis functions were used and focus on the description of additional features
that arise when using quadratic basis functions.

10.5.1 Evaluating Integrals Over

We calculate the integrals over that appear in Eqs. (10.21) and (10.22) by summing
the contributions to these integrals from individual elements. Suppose the nodes of
element k are k1 , k2 , k3 , k4 , k5 , k6 , listed in the order prescribed for the connectivity
array lnods. Only the basis functions ki , i = 1, 2, . . . , 6, will be nonzero on this
element, and so the nonzero local contributions to Eqs. (10.21) and (10.22) from
(k) (k)
integration over this element may be stored in a 6 6 matrix Alocal and a vector blocal,
(k)
with six entries. Using Eqs. (10.5)(10.14), we may write the entries of Alocal,i, j as
integrals over the canonical element given by, for i = 1, 2, . . . , 6, j = 1, 2, . . . , 6:
182 10 Higher Order Basis Functions

(k)      
Alocal,i, j = p(x, y) local,i local, j + local,i q(x, y) local, j +


r (x, y)local,i local, j det(F) d A X ,


(10.23)

where, as in Chap. 7,  , together with d A X , denotes integration over the canonical
triangular element, and det(F) is the Jacobian of the transformation between element
(k)
k and the canonical element, given by Eq. (7.37). Similarly, the entries of blocal,i are
given by, for i = 1, 2, . . . , 6,

(k)
blocal,i = f (x, y)local,i det(F) d A X . (10.24)


(k)
Entry Alocal,i, j , i = 1, 2, . . . , 6, j = 1, 2, . . . , 6, contributes to entry Aki ,k j of A, and
(k)
entry blocal,i , i = 1, 2, . . . , 6, contributes to entry bki of b.
We now need to write each term in the integrands of Eqs. (10.23) and (10.24) as
a function of X and Y . Remembering from Eq. (7.37) that
 
x x
X Y
F= y y
X Y

x k2 x k1 x k3 x k1
= , (10.25)
yk2 yk1 yk3 yk1

we may easily evaluate the determinant of F. Further, Eq. (7.40) allows us to write
X X

x y
Y Y
= F 1 ,
x y

allowing us to use the chain rule to evaluate partial derivatives of the basis local
functions with respect to x and y. These partial derivatives are given by, for i =
1, 2, . . . , 6:

local,i local,i X local,i Y


= + ,
x X x Y x
local,i local,i X local,i Y
= + .
y X y Y y

Functions such as p(x, y) may be written as functions of X and Y by using Eqs. (10.5)
and (10.6) to first write x and y as functions of X and Y .
All terms in the integrands of Eqs. (10.23) and (10.24) may now written as a
function of X and Y . The integrals may then be evaluated, either analytically, or
using the quadrature rules over the canonical triangle derived in Sect. 8.7.
10.5 Assembling the Algebraic Equations 183

10.5.2 Evaluating Integrals Over N

In contrast to the integrals over element edges described in Sect. 8.6.2, the edges of
elements will now contain three nodes. Suppose that edge k of the element edges
that lie on N , denoted by e Nk , starts at node k1 , passes through node k2 and ends
at node k3 . Only the basis functions k1 , k2 and k3 will be nonzero on this edge. As
a consequence, the contribution to Eq. (10.22) from integration over this edge will
generate nonzero contributions only to the entries bk1 , bk2 and bk3 of the global vector
b. We store only these three nonzero local contributions from integrating over this
(k)
edge in the vector blocal edge with entries given by, for i = 1, 2, 3,


(k)
blocal edge,i = ki (x, y)g N (x, y) ds. (10.26)
e Nk

(k)
Entry blocal edge,i , i = 1, 2, 3, of these local contributions will contribute to the entry
bki of the global vector b. As the edge e Nk connects node k1 to node k3 , and the
edge is a straight line, points x and y that lie on this edge are parameterised by, for
0 X 1,

x = (1 X )xk1 + X xk3 , (10.27)


y = (1 X )yk1 + X yk3 . (10.28)

Further, we may write the basis functions k1 , k2 and k3 in terms of the variable X
as, for 0 X 1,

1
k1 (X ) = 2 X (1 X ),
2
k2 (X ) = 4X (1 X ),

1
k3 (X ) = 2X X .
2

It is straightforward to justify these expressions for k1 , k2 and k3 it can be seen


that they are quadratic functions and that they take the correct values at the node k1
(where X = 0), the node k2 (where X = 0.5) and the node k3 (where X = 1). The
integral given by Eq. (10.26) may then be written as, for i = 1, 2, 3:
 1
(k)
blocal edge,i = h ki (X )g N (x, y) dX, (10.29)
0

where h is the length of the edge, given by



 2  2
h= x k1 x k3 + yk1 yk3 ,
184 10 Higher Order Basis Functions

and x and y are given as functions of X by Eqs. (10.27) and (10.28).


If the integral given by Eq. (10.29) cannot be evaluated analytically, then it may
be evaluated using quadrature techniques, as described in Sect. 3.5. On each local
edge, having calculated the local contributions, we then add these contributions into
the global vector b.

10.5.3 Setting Entries Defined Explicitly

Finally, we set the entries of A and b given by Eqs. (10.19) and (10.20). At this stage,
it is important to correct any incorrect entries that have been introduced earlier in the
assembly processsee Sect. 8.6.3 for more details.

10.6 Exercises

The exercises below implement the material described in this chapter. Should the
reader wishes to apply these techniques to other boundary value problems, then
suitable examples are given in the exercises at the end of Chap. 8.

10.1 In Listing 8.1, we provided a computational implementation for calculating the


finite element solution, using linear basis functions on a mesh of triangular elements,
of the boundary value problem defined, for 1 < x < 1, 1 < y < 1, by

  u u
ex u + 3x + 2y 6u = 2ex (1 + x + 6y) ,
x y

subject to Dirichlet boundary conditions

u(x, 1) = x 2 2, 1 < x < 1,


u(1, y) = 2y + 1,
3
1 < y < 1,

and natural Neumann boundary conditions

ex (u) n = 6ex , 1 < x < 1, y = 1,


ex (u) n = 2ex , x = 1, 1 < y < 1.

This boundary value problem has solution u(x, y) = x 2 + 2y 3 .


In this exercise, we will modify Listing 8.1 so that it calculates the finite element
solution of this boundary value problem using quadratic basis functions.

(a) The function GenerateMesh contained in Listing 8.1 generates the coordi-
nates of nodes, the connectivity array lnods, a list of nodes on the Dirichlet
10.6 Exercises 185

boundary and an array that stores the nodes that lie on each element edge of the
boundary N . Replace the function GenerateMesh contained in Listing 8.1
by a new function that generates these arrays for quadratic basis functions.
(b) Modify the function CalculateContributionOnElement so that it com-
putes the local contributions to the linear system that arise from integrating over
(k) (k)
element k, Alocal and blocal , when quadratic basis functions are used. When mod-
ifying this function, you should remember that: (i) there are now six nodes in
(k)
the local element, and so there will be six local basis functions, and Alocal and
(k)
blocal will be of a different size; (ii) expressions for the local basis functions will
be different to the original listing; and (iii) the partial derivatives of the basis
functions will no longer be constant over the element.
(c) Modify the function CalculateContributionOnNeumannEdge so that
it now calculates the contribution from integrating over an element edge that lies
on N when quadratic basis functions are used.
(d) Make any other modifications to Listing 8.1 that are needed, such as changing
the size of the global matrix A and global vector b in lines 1617.

Verify that your implementation is correct by comparison with the true solution.

10.2 A cubic approximation on the canonical triangular element X 0, Y 0,


1 X Y 0 may be written

a1 + a2 x + a3 y + a4 x 2 + a5 x y + a6 y 2 + a7 x 3 + a8 x 2 y + a9 x y 2 + a10 y 3 ,

and so we have ten degrees of freedom on each element. We therefore need to define
ten local basis functions on the canonical triangular element. Ten nodes on this
element are shown in Fig. 10.5, together with a local numbering of nodes. Suitable
local basis functions are then

9 1 2
local,1 (X, Y ) = X Y X Y (1 X Y ) ,
2 3 3

9 1 2
local,2 (X, Y ) = X X X ,
2 3 3

9 1 2
local,3 (X, Y ) = Y Y Y ,
2 3 3

27 1
local,4 (X, Y ) = XY X ,
2 3

27 1
local,5 (X, Y ) = XY Y ,
2 3

27 1
local,6 (X, Y ) = Y Y (1 X Y ) ,
2 3

27 2
local,7 (X, Y ) = Y X Y (1 X Y ) ,
2 3
186 10 Higher Order Basis Functions

Fig. 10.5 The nodes for


cubic basis functions on the 3
1
canonical triangular element

6 5
2/3

Y
7 10 4
1/3

1 8 9 2
0

0 1/3 2/3 1
X

27 2
local,8 (X, Y ) = X X Y (1 X Y ) ,
2 3

27 1
local,9 (X, Y ) = X X (1 X Y ) ,
2 3
local,10 (X, Y ) = 27X Y (1 X Y ) .

(a) Generate a mesh that partitions the region 0 < x < 1, 0 < y < 1 into triangular
elements. Generate nodes so that each element has nodes that may be mapped
to the nodes of the canonical element shown in Fig. 10.5.
(b) The model boundary value problem in Chap. 7 was defined by

2u 2u
+ = 1, 0 < x < 1, 0 < y < 1,
x2 y2

subject to Dirichlet boundary conditions on the whole boundary, given by

u(x, 0) = 0, 0<x <1


u(1, y) = 0, 0 < y < 1,
u(x, 1) = 0, 0 < x < 1,
u(0, y) = 0, 0 < y < 1.

Use the mesh generated in part (a) to calculate the finite element solution of this
boundary value problem that makes a cubic approximation to the solution on each
element. Validate your solution by comparison with the output of Listing 7.1.

10.3 A biquadratic approximation on the canonical quadrilateral element 0 X


1, 0 Y 1, may be written
10.6 Exercises 187

Fig. 10.6 The nodes for


biquadratic basis functions 4 7 3
1
on quadrilateral elements

8 9 6

Y
1/2

1 5 2
0

0 1/2 1
X

a1 + a2 x + a3 y + a4 x 2 + a5 x y + a6 y 2 + a7 x 2 y + a8 x y 2 + a9 x 2 y 2 ,

and so we have nine degrees of freedom on each element. We therefore need to


define nine local basis functions on the canonical element 0 X 1, 0 Y 1.
Nine nodes on this element are shown in Fig. 10.6, together with a local numbering
of nodes. Defining

1
Q 1 (Z ) = 2 Z (1 Z ) ,
2
Q 2 (Z ) = 4Z (1 Z ),

1
Q 3 (Z ) = 2Z Z ,
2

Suitable local basis functions are then

local,1 (X, Y ) = Q 1 (X )Q 1 (Y ),
local,2 (X, Y ) = Q 3 (X )Q 1 (Y ),
local,3 (X, Y ) = Q 3 (X )Q 3 (Y ),
local,4 (X, Y ) = Q 1 (X )Q 3 (Y ),
local,5 (X, Y ) = Q 2 (X )Q 1 (Y ),
local,6 (X, Y ) = Q 3 (X )Q 2 (Y ),
local,7 (X, Y ) = Q 2 (X )Q 3 (Y ),
local,8 (X, Y ) = Q 1 (X )Q 2 (Y ),
local,9 (X, Y ) = Q 2 (X )Q 2 (Y ).
188 10 Higher Order Basis Functions

(a) Generate a mesh that partitions the region 0 < x < 1, 0 < y < 1 into quadri-
lateral elements. Generate nodes so that each element has nodes that may be
mapped to the nodes of the canonical element shown in Fig. 10.6.
(b) Use the mesh generated in part (a) to calculate the finite element solution to
the boundary value problem given in Exercise 10.2, where your finite element
solution makes a biquadratic approximation to the solution on each element.
Validate your solution by comparison with the output of Listing 7.1.
Chapter 11
Nonlinear Elliptic Partial Differential
Equations

In Chap. 5, we explained how to apply the finite element method to nonlinear ordinary
differential equations. We saw that calculating the finite element solution of nonlinear
differential equations required us to solve a nonlinear system of algebraic equations
and discussed how these algebraic equations could be solved.
In this chapter, we explain how to apply the finite element method to nonlinear
partial differential equations by combining: (i) the material on calculating the finite
element solution of linear partial differential equations given in Chaps. 710 and
(ii) the material on calculating the finite element solution of nonlinear ordinary
differential equations in Chap. 5. We consider equations of the form
    
u u u u
p x, y, u, , + r x, y, u, , = 0, (x, y) ,
x y x y

where is some subset of the (x, y)-plane, subject to suitable boundary conditions
on the boundary . The application of the finite element method to equations of
this form is illustrated using an example.

11.1 A Model Problem

We illustrate the application of the finite element method to nonlinear elliptic partial
differential equations using the model differential equation, defined for the region
defined by 0 < x < 1, 0 < y < 1, by
 2  2 
  u u
eu u + ex +y
2 2
+ + 4 = 0, (11.1)
x y

Springer International Publishing AG 2017 189


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_11
190 11 Nonlinear Elliptic Partial Differential Equations

subject to Dirichlet boundary conditions

u(x, 0) = x 2 , 0 < x < 1, (11.2)


u(x, 1) = x 2 + 1, 0 < x < 1, (11.3)
u(0, y) = y 2 , 0 < y < 1, (11.4)

and the Neumann boundary condition


2
+1
eu (u) n = 2e y , x = 1, 0 < y < 1, (11.5)

where n is the normal vector pointing out of , with unit length. This boundary
value problem has solution u(x, y) = x 2 + y 2 .

11.2 The Weak Formulation

The weak formulation of the differential equation given by Eq. (11.1), subject to
the boundary conditions given by Eqs. (11.2)(11.5), is obtained by first multiplying
Eq. (11.1) by a test function v(x, y) H01 () and integrating the resulting product
over the region :
   2  2 
  u u
v eu u + ex +y
2 2
+ +4 d A = 0.
x y

After a little manipulation, followed by the application of the divergence theorem,


this becomes

veu (u) n ds+

    2 
x 2 +y 2 u 2 u
e (u) (v) + ve
u
+ + 4 d A = 0,
x y

where is the boundary of , and n is the normal vector of unit length pointing out
of . As v(x, y) H01 (), then, by definition, v(x, y) = 0 on D , the part of the
boundary where Dirichlet boundary conditions are applied. Using this observation,
and the Neumann boundary condition given by Eq. (11.5), we may write the integral
above as
    2  
x 2 +y 2 u 2 u
2e y +1 v ds = 0,
2
e (u) (v) + ve
u
+ + 4 dA
x y N
11.2 The Weak Formulation 191

where N is the part of the boundary where Neumann boundary conditions are
applied, namely x = 1, 0 < y < 1.
The weak solution is then defined by:
find u(x, y) H E1 () such that
  2  2 
2 +y 2 u u
eu (u) (v) + ve x + +4 d A
x y

2 +1
2e y v ds = 0, (11.6)
N

for all test functions v(x, y) H01 ().

11.3 The Mesh and Basis Functions

We partition into a mesh of triangular elements as described in Sect. 7.3.1 and


calculate a finite element solution that uses the linear basis functions described in
Sect. 7.3.2. The sets S h , S Eh and S0h are then defined by Eqs. (7.22)(7.24).

11.4 The Finite Element Solution

We write the finite element solution as the member of S h given by


N node

U (x, y) = U j j (x, y), (11.7)


j=1

where Nnode is the number of nodes in the mesh. Using the definition of the weak
solution given by Eq. (11.6), we define the finite element solution by
find U (x, y) S Eh such that
  2  2 
2 +y 2 U U
eU (U ) (i ) + i e x + +4 d A
x y

2 +1
2e y i ds = 0, (11.8)
N

for all test functions i (x, y) S0h .


192 11 Nonlinear Elliptic Partial Differential Equations

11.5 The Nonlinear System of Algebraic Equations

When applying the finite element method to linear differential equations, whether
ordinary differential equations or partial differential equations, the definition of the
finite element solution allowed us to derive a linear system of algebraic equations.
In Chap. 5, we saw that, for nonlinear ordinary differential equations, the definition
of the finite element solution resulted in a system of nonlinear algebraic equations
that we write as

R(U) = 0, (11.9)

where R is a vector of length Nnode . As with linear differential equations, the indi-
vidual equations that comprise Eq. (11.9) fall into two categories: (i) equations that
arise from forcing U (x, y) to satisfy the Dirichlet boundary conditions and lie in
the set S Eh ; and (ii) equations that arise from substituting a suitable basis function
i (x, y) into Eq. (11.8). We now discuss these categories separately.

11.5.1 Satisfying the Dirichlet Boundary Conditions

We may combine the Dirichlet boundary conditions given by Eqs. (11.2)(11.4) into
the single boundary condition

u(x, y) = g D (x, y), (x, y) D ,

where g D (x, y) is a suitable encapsulation of the right-hand sides of Eqs. (11.2)


(11.4). Suppose node i lies on the part of the boundary where Dirichlet boundary
conditions are applied. Using Eq. (7.26), we may write U (xi , yi ) = Ui , and so the
Dirichlet boundary condition is satisfied at this node provided

Ui = g D (xi , yi ).

Entry i of the residual vector R that appears in Eq. (11.9) is therefore given by

Ri = Ui g D (xi , yi ). (11.10)

11.5.2 Using Suitable Test Functions

In Sect. 11.5.1, we derived an expression for entry Ri of the residual R that appears
in Eq. (11.9) when node i lies on D . Suppose, instead, that node i does not lie on
D . The basis function i (x, y) is then a member of the set S0h and is a suitable
11.5 The Nonlinear System of Algebraic Equations 193

test function for Eq. (11.8). When node i does not lie on D , we then have
  2   
x 2 +y 2 U U 2
Ri = e (U ) (i ) + i e
U
+ + 4 d A
x y

2e y +1 i ds.
2

N
(11.11)

In common with the examples in earlier chapters, we will find it convenient to


decompose the integral over above into the sum of contributions from individual
elements and to decompose the integral over N into the sum of contributions from
individual element edges. We therefore write

Nele  
 
U i U i
Ri = eU
+ +
k=1 ek
x x y y
 2  2  Nneu 
U U
x +y
2e y +1 i ds, (11.12)
2 2 2
i e + +4 dA
x y k=1 e Nk

where Nele is the number of elements in the mesh, ek is the area occupied by element
k, Nneu is the number of element edges that lie on N , and e Nk is element edge k
that lies on N .

11.6 Assembling the Nonlinear System of Algebraic


Equations

To use the finite element solution given by Eq. (11.7), we first have to solve the system
of nonlinear algebraic equations, Eq. (11.9), with entries given by Eqs. (11.10) and
(11.11). In Chap. 5, we saw that computational methods exist for solving systems
of nonlinear algebraic equations that require only the specification of the residual
vector R. These methods are, however, inefficient for systems with a large number
of unknowns. Under these conditions, more efficient computational methods should
be used that require, in addition to the residual vector R, the Jacobian matrix J , with
entries given by

Ri
Ji, j = . (11.13)
U j

We now explain how both R and J may be evaluated in practice. As before, this is
done by: (i) evaluating integrals over , (ii) evaluating integrals over N and (iii)
194 11 Nonlinear Elliptic Partial Differential Equations

evaluating contributions that can be set explicitly. We now give some more details
on each of these tasks.

11.6.1 Evaluating Integrals Over

As we are using a mesh of triangular elements, and a linear approximation to the


solution on each element, each element will contain three nodes. Suppose k1 , k2 ,
k3 are the nodes of element k, listed in an anticlockwise direction. Only the basis
functions k1 , k2 , k3 will be nonzero on this element, and so the only nonzero
contributions to Eq. (11.12) from this element arise from i = k1 , i = k2 and i = k3 .
We therefore calculate only these nonzero entries and store them in a local residual
(k)
vector Rlocal , with entries given by:
   
(k) U ki U ki
Rlocal,i = eU
+ +
ek x x y y
    
x 2 +y 2 U 2 U 2
k i e + +4 d A, i = 1, 2, 3. (11.14)
x y

(k)
Entry i, where i = 1, 2, 3, of Rlocal , then contributes to entry ki of the global
residual R.
For a given U (x, y), the local residual vector discussed above may be calculated
by mapping element k to the canonical triangle using the transformation given by
Eqs. (7.12) and (7.13). The terms in the integrand may all be written as functions of
X and Y on the canonical elementEqs. (7.17)(7.19) allow us to write

ki (X, Y ) = local,i (X, Y ), i = 1, 2, 3,


U (X, Y ) = Uk1 local,1 (X, Y ) + Uk2 local,2 (X, Y ) + Uk3 local,3 (X, Y ). (11.15)

Finally, Eqs. (7.12) and (7.13) allow x and y to be written as functions of X and Y ,
allowing us to evaluate the terms that include x and y. We may now evaluate the
integral given by Eq. (11.14), either analytically, or using quadrature as described in
Sect. 8.7.
We now turn our attention to computing the entries of the Jacobian matrix
defined by Eq. (11.13). Let node i be a node of the mesh that does not lie on
D . Using Eq. (11.12), entries of the Jacobian matrix in row i may be written,
for j = 1, 2, . . . , Nnode ,
11.6 Assembling the Nonlinear System of Algebraic Equations 195

N    
Ri ele
U i U i
= eU + +
U j k=1 ek U j x x y y
    
x 2 +y 2 U 2 U 2
i e + +4 d A, (11.16)
x y

and so entries depend only on integrals over individual elements, and there is no
contribution from integrals over element edges that lie on N . We now consider
contributions to the Jacobian matrix from the same element k, with nodes k1 , k2 , k3 ,
as used above when evaluating local contributions to the residual vector. We imme-
diately see that contributions from element k to the Jacobian matrix are nonzero only
for the three rows i = k1 , i = k2 and i = k3 . Furthermore, we see from Eq. (11.15),
that the finite element solution U (x, y) on element k depends on only three values of
U: the values U j , with j = k1 , j = k2 and j = k3 . The partial derivative with respect
to U j that appears in Eq. (11.16) is therefore zero unless j takes one of the values
k1 , k2 , k3 . As such, we may store all nonzero local contributions to J from element
(k)
k in the 3 3 matrix Jlocal , with entries given by, for i = 1, 2, 3, j = 1, 2, 3:
   
(k) U ki U ki
Jlocal,i, j = eU + +
ek Uk j x x y y
    
x 2 +y 2 U 2 U 2
k i e + +4 d A.
x y

This integral may be evaluated by mapping to the canonical triangle, and evaluating
all quantities as functions of X and Y as was done for the local contribution to the
residual. The local contributions are then added to the appropriate entry of the global
(k)
Jacobian matrix; for i = 1, 2, 3, j = 1, 2, 3, the entry Jlocal,i, j of the local Jacobian
matrix contributes to the entry Jki ,k j of the global Jacobian matrix.

11.6.2 Evaluating Integrals Over N

The contributions to Eq. (11.12) from integrals over element edges that lie on N
take exactly the same form as the integrals evaluated in Sect. 8.6.2. These contri-
butions may, therefore, be evaluated in exactly the same way. We noted in the pre-
vious section that there is no contribution to the Jacobian matrix from integrating
over N .
196 11 Nonlinear Elliptic Partial Differential Equations

11.6.3 Setting Entries Defined Explicitly

The entries of R that take the form of Eq. (11.10) may easily be evaluated explicitly.
The entries of the Jacobian matrix that arise from these rows are given by:

1, i = j,
Ji, j =
0, i = j,

and so these entries may also be set explicitly. Remember, however, to remove any
incorrect entries that were earlier introduced into these rows when integrating over
and N . See Sect. 8.6.3 for more details.

11.7 Exercises

11.1 Use the material in this chapter to calculate the finite element solution of the
differential equation given by Eq. (11.1), subject to the boundary conditions given by
Eqs. (11.2)(11.5). Verify that your finite element solution is correct by comparison
with the true solution u(x, y) = x 2 + y 2 .

11.2 Calculate the finite element solution of the differential equation, defined for
0 < x < 1, 0 < y < 1, by:
 2  2 
  u u
(1 + u 2 )u + (x 2 + y 2 )u + 3 + u = 0,
x y

subject to Dirichlet boundary conditions

u(x, 0) = 1, 0 < x < 1,


u(1, y) = e , y
0 < y < 1,
u(x, 1) = ex , 0 < x < 1,
u(0, y) = 1, 0 < y < 1.

Use your own choice of triangular elements or quadrilateral elements and your own
choice of polynomial approximation on each element. Verify that your finite element
solution is correct by comparison with the true solution u(x, y) = ex y .
Chapter 12
Systems of Elliptic Equations

We saw, in Chap. 6, how the finite element method may be applied to systems of
ordinary differential equations. Then, in Chaps. 711, we saw how to apply the finite
element method to elliptic partial differential equations, using a variety of meshes
and basis functions. In this chapter, we combine this material, allowing us to apply
the finite element method to systems of elliptic partial differential equations.

12.1 A Model Problem

We define to be the region 0 < x < 1, 0 < y < 1. Our model problem is the
system of partial differential equations, defined on by:

(u 1 + u 2 ) + 3u 1 + 3u 2 = 2ex+y , (12.1)
 
(2u 1 + u 2 ) + 3u 1 = ex 3e y ey , (12.2)

subject to Dirichlet boundary conditions

u 1 (x, 0) = 2ex , u 2 (x, 0) = 0, 0 < x < 1, (12.3)


u 1 (1, y) = 2e cosh y, u 2 (1, y) = 2e sinh y, 0 < y < 1, (12.4)
u 1 (x, 1) = 2ex cosh 1, u 2 (x, 1) = 2ex sinh 1, 0 < x < 1, (12.5)
u 2 (0, y) = 2 sinh y, 0 < y < 1, (12.6)

and the Neumann boundary condition

(u 1 + u 2 ) n = 2e y , x = 0, 0 < y < 1, (12.7)

Springer International Publishing AG 2017 197


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_12
198 12 Systems of Elliptic Equations

where n is the normal vector pointing out of , of unit length. This model problem
has true solution

u 1 (x, y) = 2ex cosh y, (12.8)


u 2 (x, y) = 2e sinh y.
x
(12.9)

12.2 The Weak Formulation

We will follow a similar method to derive the weak formulation as we did in Sect. 6.2
for systems of ordinary differential equations. We multiply the first differential
equation, Eq. (12.1), by a suitable test function v1 (x, y), and the second equation,
Eq. (12.2), by another suitable test function v2 (x, y). Before we do this, we must
decide which sets v1 and v2 belong to. This requires us to allocate the boundary
conditions, given by Eqs. (12.3)(12.7), to each of the two differential equations.
The Neumann boundary condition given by Eq. (12.7) is a natural boundary
condition for the first differential equation, Eq. (12.1). If we combine this boundary
condition with the Dirichlet boundary conditions on u 1 given by Eqs. (12.3)(12.5),
we then have boundary conditions on the whole of the boundary for Eq. (12.1).
We then allocate the Dirichlet boundary conditions on u 2 given by Eqs. (12.3)
(12.6) to the second partial differential equation, Eq. (12.2). We now have boundary
conditions for both equations on the whole boundary .
The first differential equation, Eq. (12.1), satisfies Neumann boundary condi-
tions on x = 0, 0 < y < 1, and Dirichlet boundary conditions on the remainder of
the boundary. The test function v1 (x, y) then belongs to the set H 1 (), subject to
the condition that v1 (x, y) = 0 where Dirichlet boundary conditions are applied for
Eq. (12.1). Performing the usual tasks of multiplying Eq. (12.1) by v1 (x, y), inte-
grating the resulting product over , applying the divergence theorem and using the
Neumann boundary condition given by Eq. (12.7), we obtain:

(u 1 ) (v1 ) + (u 2 ) (v1 ) + 3u 1 v1 + 3u 2 v1 d A =

 
2ex+y v1 d A 2e y v1 ds,
N
(12.10)

where N is the boundary x = 0, 0 < y < 1 where Neumann boundary conditions


are applied.
The second differential equation, Eq. (12.2), satisfies Dirichlet boundary condi-
tions on the whole boundary . The test function v2 (x, y) therefore belongs in the
set H 1 (), subject to the condition that v2 (x, y) = 0 on . Performing the same
operations as were used to derive Eq. (12.10) yields
12.2 The Weak Formulation 199

2 (u 1 ) (v2 ) + (u 2 ) (v2 ) + 3u 1 v2 d A =


 
ex 3e y ey v2 d A.

(12.11)

The weak solutions, u 1 (x, y) and u 2 (x, y), are then defined by:

find u 1 (x, y), u 2 (x, y) that satisfy the Dirichlet boundary conditions and are
such that

(u 1 ) (v1 ) + (u 2 ) (v1 ) + 3u 1 v1 + 3u 2 v1 d A =

 
2ex+y v1 d A 2e y v1 ds,
N
(12.12)

2 (u 1 ) (v2 ) + (u 2 ) (v2 ) + 3u 1 v2 d A =


 
ex 3e y ey v2 d A,

(12.13)

for all test functions v1 (x, y), v2 (x, y) that are zero when Dirichlet boundary
conditions are applied to the appropriate equation.

12.3 The Mesh and Basis Functions

We will partition into a mesh of triangular elements and use the linear basis func-
tions described in Sect. 7.3.2. The sets S h , S Eh and S0h are then defined by Eqs. (7.22)
(7.24).

12.4 The Finite Element Solution

We write the finite element solutions as the members of S h given by


N node

U (1) (x, y) = U (1)


j j (x, y), (12.14)
j=0


N node

U (2) (x, y) = U (2)


j j (x, y), (12.15)
j=0
200 12 Systems of Elliptic Equations

where Nnode is the number of nodes in the mesh, and j (x), j = 1, 2, . . . , Nnode , are
the basis functions defined in Sect. 7.3.2. The finite element solutions, U (1) (x, y) and
U (2) (x, y), are then defined by:

find U (1) (x, y), U (2) (x, y), given by Eqs. (12.14) and (12.15), that satisfy the
Dirichlet boundary conditions and are such that

   
U (1) (i ) + U (2) (i ) + 3U (1) i + 3U (2) i d A =

 
2ex+y i d A 2e y i ds, (12.16)
N

for all test functions i (x, y) that are zero when Dirichlet boundary conditions
are applied to Eq. (12.1), and

   
2 U (1) (i ) + U (2) (i ) + 3U (1) i d A =


 
ex 3e y ey i d A,

(12.17)

for all test functions i (x, y) that are zero when Dirichlet boundary conditions
are applied to Eq. (12.2).

12.5 The Algebraic Equations

We now derive the system of algebraic equations that arises from substituting the
expressions for U (1) (x, y), U (2) (x, y), given by Eqs. (12.14) and (12.15), into the
conditions required from the finite element solution that were stated in the previous
section.
Suppose node i of the mesh lies on the part of the boundary where Dirichlet
boundary conditions are applied for the first differential equation, Eq. (12.1). To
satisfy the Dirichlet boundary conditions, we must set

Ui(1) = g D1 (xi , yi ), (12.18)

where g D1 (x, y) is the function that encapsulates the Dirichlet boundary conditions
on u 1 given by Eqs. (12.3)(12.5). If node i does not lie on the part of the boundary
where Dirichlet boundary conditions are applied for the first differential equation,
then i (x, y) will be zero on this boundary and is a suitable basis function for
substituting into Eq. (12.16). Using the expressions for U (1) (x, y), U (2) (x, y), given
by Eqs. (12.14) and (12.15), we obtain, after some manipulation:
12.5 The Algebraic Equations 201


N  
node
 
j (i ) + 3i j d A U (1)
j +
j=1


N  
node
 
j (i ) + 3 j i d A U (2)
j =
j=1
 
2e x+y
i d A 2e y i ds,
N
(12.19)

We may combine Eqs. (12.18)(12.19) into the equation

A(11) U(1) + A(12) U(2) = b(1) , (12.20)

where if node i of the mesh lies on the part of the boundary where Dirichlet boundary
conditions are applied for the first differential equation, then

1, j = i,
Ai,(11)
j = 0, j = i,
Ai,(12)
j = 0,

bi(1) = g D1 (xi , yi ),

and if node i does not lie on this boundary, then



 
Ai,(11)
j = j (i ) + 3i j d A,

 
Ai,(12)
j = j (i ) + 3 j i d A,
 
bi(1) = 2ex+y i d A 2e y i ds.
N

Applying a similar approach for the second differential equation, Eq. (12.2), we
obtain, after much manipulation, the equation

A(21) U(1) + A(22) U(2) = b(2) , (12.21)

where if node i of the mesh lies on the part of the boundary where Dirichlet boundary
conditions are applied for the second differential equation, then
202 12 Systems of Elliptic Equations

Ai,(21)
j = 0,

(22) 1, j = i,
Ai, j =
0, j = i,
bi(2) = g D2 (xi , yi ),

where g D2 (x, y) is the function that encapsulates the Dirichlet boundary conditions
on u 2 given by Eqs. (12.3)(12.6). If node i does not lie on this boundary, then

 
Ai,(21)
j = 2 j (i ) + 3i j d A,

 
Ai,(22)
j = j (i ) d A,


 
bi(2) = ex 3e y ey i d A.

We then compute the vectors U(1) and U(2) by combining Eqs. (12.20) and (12.21)
into the single linear system

AU = b,

where
     
A(11) A(12) U(1) b(1)
A= , U= , b= .
A(21) A(22) U(2) b(2)

This linear system may be assembled by calculating the entries as described in


Chaps. 7 and 8.

12.6 Exercises

12.1 Using the material presented in this chapter, calculate the finite element solu-
tion of the system of equations given by Eqs. (12.1)(12.7), using a mesh of triangular
elements and making a linear approximation to the solution on each element.

12.2 Extend the computational implementation that you wrote in Exercise 12.1 to
use, for example, a quadrilateral mesh and/or higher order basis functions.
Chapter 13
Parabolic Partial Differential Equations

We now describe how to apply the finite element to parabolic partial differential
equations. This is done by approximating the parabolic partial differential equation
by either a sequence of ordinary differential equations or a sequence of elliptic partial
differential equations. We may then solve these ordinary differential equations or
elliptic partial differential equations using the techniques developed earlier in this
book.

13.1 A Linear Parabolic Partial Differential Equation

Our first example is the parabolic partial differential equation, defined for 0 < x < 1,
0 < y < 1, 0 < t < 3, by

u
= (u) + f (x, y, t), (13.1)
t
where

f (x, y, t) = et (2x(1 x) + 2y(1 y) x y(1 x)(1 y)) ,

subject to the Dirichlet boundary conditions given by, for 0 < t < 3:

u(x, 0, t) = 0, 0 < x < 1, (13.2)


u(1, y, t) = 0, 0 < y < 1, (13.3)
u(x, 1, t) = 0, 0 < x < 1, (13.4)
u(0, y, t) = 0, 0 < y < 1, (13.5)

Springer International Publishing AG 2017 203


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0_13
204 13 Parabolic Partial Differential Equations

and the initial conditions

u(x, y, 0) = x y(1 x)(1 y), 0 < x < 1, 0 < y < 1. (13.6)

This differential equation has solution u(x, y, t) = x y(1 x)(1 y)et .


We begin by discretising Eq. (13.1) in time. Let 0 = t0 < t1 < t2 < < t M1 <
t M = 3 be the values of t where we approximate u(x, y, t). We then define, for
m = 0, 1, 2, . . . , M, the function u (m) (x, y) to be the approximation to the function
u(x, y, t) evaluated at t = tm , i.e.,

u (m) (x, y) u(x, y, tm ). (13.7)

The function u (m) (x, y) is often called the solution on timestep m. We place the
superscript m in parentheses in Eq. (13.7) to avoid confusion with u being raised to
the power of m. Clearly, from the initial conditions given by Eq. (13.6), we have

u (0) (x, y) = x y(1 x)(1 y), 0 < x < 1, 0 < y < 1. (13.8)

The boundary conditions given by Eqs. (13.2)(13.5) imply that for m = 1, 2, . . . , M,

u (m) (x, 0) = 0, 0 < x < 1, (13.9)


(m)
u (1, y) = 0, 0 < y < 1, (13.10)
(m)
u (x, 1) = 0, 0 < x < 1, (13.11)
(m)
u (0, y) = 0, 0 < y < 1. (13.12)

The partial derivative of u with respect to time on the interval tm1 < t tm ,
m = 1, 2, . . . , M, may be approximated using

u u (m) (x, y) u (m1) (x, y)


, tm1 < t tm .
t tm tm1

In our finite element approximation, on the interval tm1 < t tm , m = 1, 2, . . . , M,


we evaluate all terms on the right-hand side of Eq. (13.1) at time tm . This is known
as an implicit approximation to the differential equation. An implicit approximation
ensures that the finite element solutions computed have good numerical stability
properties; i.e., this approximation ensures that the inevitable small rounding errors
that occur in any computational implementation do not grow exponentially, eventu-
ally swamping the solution. On this interval, we may then write

2u 2 u (m) 2u 2 u (m)
= , = .
x2 x2 y2 y2
13.1 A Linear Parabolic Partial Differential Equation 205

An implicit discretisation in time of Eq. (13.1) on the interval tm1 < t tm , m =


1, 2, . . . , M, then satisfies, for 0 < x < 1, 0 < y < 1,

u (m) u (m1) 2 u (m) 2 u (m)


= + + f (x, y, tm ),
tm tm1 x2 y2

which, after a little rearranging, may be written as


 
2 u (m) 2 u (m) u (m) u (m1)
+ + = f (x, y, tm ) + . (13.13)
x2 y2 tm tm1 tm tm1

Provided we know u (m1) (x, y), Eqs. (13.9)(13.13) are a linear elliptic partial
differential equation and boundary conditions for u (m) (x, y), m = 1, 2, . . . , M. A
finite element solution to this boundary value problem may easily be computed
using the finite element techniques given in Chaps. 710. As u (0) (x, y) is given by
Eq. (13.8), we may use Eqs. (13.9)(13.13) to compute the finite element solution
U (1) (x, y) that is an approximation to u (1) (x, y). This finite element approximation
U (1) (x) may be used, along with Eqs. (13.9)(13.13), to compute the finite element
solution U (2) (x, y) that is an approximation to u (2) (x, y). We then proceed in this
manner, calculating a finite element solution U (m) (x, y) that is an approximation to
u (m) (x, y), until we reach the final time where m = M.
We leave the computational implementation of the calculation of this finite element
solution as an exercise for the reader. There is, however, one very important point to
be made on the finite element solution of the sequence of elliptic partial differential
equations described above, which has a significant effect on the efficiency of the
computational implementation. When solving each linear elliptic partial differential
equation given by Eq. (13.13), where m = 1, 2, . . . , M, the finite element solution
on timestep m will require the solution of the linear system

A(m) U(m) = b(m) . (13.14)

Suppose the points t0 , t1 , ..., t M are equally spaced, so that tm tm1 takes a
constant value for m = 1, 2, ..., M. It will be seen in Exercise 13.1 that A(m) is
identical on each timestep, and so this matrix only need be computed once, at the
start of the computational implementation.

13.2 A Nonlinear Parabolic Equation

An example nonlinear parabolic equation is Fishers equation:

u 2u
= 2 + u(1 u), 1 < x < 10, 0 < t < 10, (13.15)
t x
206 13 Parabolic Partial Differential Equations

subject to boundary conditions

u
= 0, x = 1, 10, (13.16)
x
and initial conditions

u(x, 0) = u 0 (x), 1 < x < 10, (13.17)

where

1, 1 < x 0,
u 0 (x) =
0, 0 < x < 10.

As with the linear differential equation discussed in Sect. 13.1, we let 0 = t0 <
t1 < t2 < < t M1 < t M = 10 be the values of t where we approximate u(x, t)
and then define, for m = 0, 1, 2, . . . , M,

u (m) (x) u(x, tm ).

The initial conditions given by Eq. (13.17) may then be written as

u (0) (x) = u 0 (x), (13.18)

and the boundary conditions given by Eq. (13.16) imply that

du (m)
= 0, x = 1, 10. (13.19)
dx
An implicit discretisation in time of Eq. (13.15) on the interval tm1 < t tm , m =
1, 2, . . . , M then satisfies

u (m) u (m1) d2 u (m)


= + u (m) (1 u (m) ), 1 < x < 10,
tm tm1 dx 2

which, after a little rearranging, may be written as

d2 u (m) (m) (m) u (m) (x) u (m1)


u (1 u ) + = 0, 1 < x < 10. (13.20)
dx 2 tm tm1

Provided we know u (m1) (x), the differential equation given by Eq. (13.20) is a
nonlinear differential equation for u (m) , subject to the boundary conditions given
by Eq. (13.19). We may calculate the finite element approximation U (m) using the
techniques discussed in Chap. 5 for nonlinear ordinary differential equations. We
may therefore proceed through all timesteps m as in Sect. 13.1, calculating finite
13.3 A Semi-implicit Discretisation in Time 207

element solutions U (m) that approximate the functions u (m) (x), until we reach the
final time where m = M.

13.3 A Semi-implicit Discretisation in Time

We have used an implicit approximation to the time derivative in all examples pre-
sented so far in this chapter. We explained earlier that this ensured that the finite
element solutions calculated had good numerical stability properties. There are some
equations where the so-called semi-implicit approximation is more computationally
efficient and also possesses good stability properties. Suppose we are solving the
reaction-diffusion system given, for a < x < b, 0 < t < T , by

u
= D (u) + f (x, y, t, u), (13.21)
t
where D is constant, subject to suitable initial and boundary conditions. Calculating
the finite element solution at times 0 = t0 < t1 < t2 < < t M1 < t M = T , and
writing u (m) (x) u(x, tm ) for m = 0, 1, 2, . . . , M, the implicit approximation of
Eq. (13.21) is a nonlinear differential equation for u (m) , m = 1, 2, . . . , M given by

  u (m) u (m1)
D u (m) + f (x, y, tm , u (m) ) = 0.
tm tm1

The semi-implicit approximation evaluates the term given by f (x, y, t, u) at time


tm1 rather than time tm . The semi-implicit approximation may then be written as

  u (m) u (m1)
D u (m) + = + f (x, y, tm1 , u (m1) ). (13.22)
tm tm1 tm tm1

The semi-implicit approximation given by Eq. (13.22) has two attractive features.
First, the differential equation for u (m) , given by Eq. (13.22), is a linear equation.
The finite element solution can therefore be calculated by solving a linear system of
algebraic equations rather than a nonlinear system. Secondly, provided the timestep
tm tm1 is constant for m = 1, 2, ..., M, the matrix that appears in the linear sys-
tem is the same for each timestep, as discussed in Sect. 13.1. Thus, the semi-implicit
approximation has the computational advantage that this matrix need only be calcu-
lated once. Although the stability properties of the semi-implicit method are not as
good as the implicit method, this very rarely causes a problem.
208 13 Parabolic Partial Differential Equations

13.4 Exercises

13.1 Calculate the finite element solution of the parabolic partial differential equa-
tion given by Eqs. (13.1)(13.6). Validate your finite element solution by comparison
with the true solution u(x, y, t) = x y(1 x)(1 y)et .
On each timestep, you need to solve a linear system. Use a constant timestep, so
that tm tm1 is constant for m = 1, 2, ..., M. Verify that for this model problem,
you need only calculate the matrix that appears in this linear system once, before
looping over timesteps.

13.2 A parabolic partial differential equation in one spatial dimension is defined by,
for 0 < x < , 0 < t < 10:
 
u 2u 1
= 2 + 2 sin x + t + ,
t x 4

with boundary conditions given by

u = sin t, x = 0, 0 < t < 10,


u
= cos( + t), x = , 0 < t < 10,
x
and initial conditions given by

u(x, 0) = sin x, 0 < x < .

This differential equation has solution u(x, t) = sin(x + t).


(a) Using a uniform mesh for both the spatial region 0 < x < and the time interval
0 < t < 10 calculate the finite element approximation to the solution at time
t = 10. Use a linear approximation to the solution on each element. Note that
you only need to calculate the global finite element matrix once, as described in
Sect. 13.1.
(b) Repeat part (a) using a higher order polynomial approximation on each element.

13.3 Fishers equation is given by Eqs. (13.15)(13.17).

(a) Calculate the finite element solution of this nonlinear partial differential equation
using the implicit method described in Sect. 13.2.
(b) Calculate the finite element solution of this nonlinear partial differential equation
using the semi-implicit method described in Sect. 13.3.

13.4 A system of parabolic partial differential equations, in one spatial dimension,


that describes pattern formation (see Murray [5], for more details) is given by
13.4 Exercises 209
 
u 1 2u1 u 1 u 2
= + a u1 ,
t x2 1 + u 1 + K u 21
 
u 2 2u2 u 1 u 2
= d 2 + (b u 2 ) ,
t x 1 + u 1 + K u 21

for 0 < x < 10, 0 < t < 50, with parameter values = 1.5, = 9, K = 0.1, =
18.5, a = 92, b = 64, d = 10, initial conditions u 1 (x, 0) = 10 + sin x, u 2 (x, 0) =
9 + sin x, and boundary conditions

u 1 u 2
= = 0, x = 0, 10, 0 < t < 50.
x x
Calculate the finite element solution of this system of equations. This system may
be solved using the semi-implicit approximation described in Sect. 13.3.

13.5 The system of parabolic partial differential equations given in Exercise 13.4
may be generalised to two dimensions by writing them as, for 0 < t < 50,
 
u 1 u 1 u 2
= (u 1 ) + a u 1 ,
t 1 + u 1 + K u 21
 
u 2 u 1 u 2
= d (u 2 ) + (b u 2 ) ,
t 1 + u 1 + K u 21

using the same parameter values as in Exercise 13.4, initial conditions u 1 (x, y, 0) =
10 + sin x + sin y, u 2 (x, y, 0) = 9 + sin x + sin y, and boundary conditions

n u 1 = n u 2 = 0,

on all boundaries for 0 < t < 50.

(a) Solve the partial differential equations above on the square 0 < x < 10,
0 < y < 1, for times 0 < t < 50. This system may be solved using the semi-
implicit approximation described in Sect. 13.3.
(b) Repeat part (a), with the parameter now taking the value = 0.9.
Appendix A
Methods for Solving Linear and Nonlinear
Systems of Algebraic Equations

All applications of the finite element method in this book have required the solution
of a system of algebraic equations. These algebraic equations were linear when
the differential equation was linear, and nonlinear when the differential equation
was nonlinear. Despite the central role played by the solution of these systems of
equations, we have said very little about how to solve them.
The numerical solution of both linear and nonlinear systems of equations is under-
pinned by very elegant mathematics and is a fascinating field in its own right. There
are many excellent expositions of this subject, for example Trefethen and Bau [11],
Golub and van Loan [3], Greenbaum [4], van der Vorst [12] and the recent review
article by Wathen [13], to name a few. A more specialised treatment from a finite ele-
ment standpoint may be found in Elman, Silvester and Wathen [1]. It is not possible
to do justice to this field in a short appendix, and we do not attempt to do so. Instead,
we present an overview of established, practical techniques that may be used to solve
the systems of algebraic equations arising from the finite element method. Although
we provide an outline of the algorithms that may be used, we do not recommend that
the user develops software to implement these algorithms themselves. Indeed, we
would discourage the reader from doing so, as many commercial and open-source
software packages (for example, Matlab, Octave, PETSc) are readily available and
provide reliable, computationally efficient and robust implementations. The purpose
of outlining the algorithms here is to allow the reader to understand both the oper-
ations performed by each algorithm andmost importantlythe consequences of
these operations in terms of computational requirements. It is intended that this will
give the reader guidance in choosing the most appropriate technique for their compu-
tations. In the discussion that follows, we assume some familiarity with basic linear
algebra and continuous mathematics, but aim to make the material as accessible as
possible.
Throughout this book, we have used the convention that the finite element solution
at node i in a finite element mesh is given by Ui . Using this convention, systems of
algebraic equations have been written as the linear system AU = b, or the nonlinear
system R(U) = 0. In a slight abuse of this convention, in this appendix, we write
these systems of equations as Au = b and R(u) = 0, respectively, i.e. we write the

Springer International Publishing AG 2017 211


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0
212 Appendix A: Methods for Solving Linear and Nonlinear

vector of unknowns as u rather than U. This is mainly to avoid confusion with upper
triangular matrices, which will be used in this chapter and are commonly denoted
by U. Furthermore, writing the vector of unknowns as u fits in with a common
convention from linear algebra, namely that vectors are represented by lower-case
letters.

A.1 Linear Systems

Linear systems arising from the finite element method have two key properties: (i)
they are nonsingular, and (ii) they are sparse, i.e. they have very few nonzero entries
in each row of the matrix. Techniques for solving these linear systems fall into two
categories, direct methods and iterative methods, that we now discuss.

A.1.1 Direct Methods

We begin by describing a method for solving linear systems that most readers will
be familiar with: Gaussian elimination with partial pivoting, sometimes known as
row pivoting. This technique, although not requiring sophisticated mathematics, is a
practical solver for many linear systems. As explained above, we do not provide full
details of the computational implementation of this technique, but merely enough
detail to allow the reader to understand both the computational operations that are
required and the practical consequences of these operations. First, we require some
definitions.
A matrix is said to be lower triangular if all entries above the diagonal take the
value zero. Clearly, the matrix L is lower triangular if, and only if, Li,j = 0 when
i < j. Similarly, a matrix U is upper triangular if, and only if, all entries below the
diagonal take the value zero, that is Ui,j = 0 if i > j. Finally, a permutation matrix is
a square matrix with the following two properties: (i) each row and each column of
the matrix contain precisely one entry that takes the value 1, and (ii) all other entries
of the matrix take the value 0. Permutation matrices take their name because they
permute rows of a matrix, as we shall now explain. Suppose P is a permutation matrix
of size N N where, in row i, the entry that takes the value 1 appears in column j.
Let A be another matrix of size N N and b be a row vector with N entries. The
entries of row j of A will appear in row i of PA, and the entry in row j of b will appear
in row i of Pb. Permutation matrices are used to implement the pivoting steps of
Gaussian elimination.
Suppose we solve the nonsingular linear system

Au = b, (A.1)
Appendix A: Methods for Solving Linear and Nonlinear 213

using Gaussian elimination with partial pivoting, where A is a N N matrix. One


common practical implementation of this technique yields a permutation matrix P,
a lower triangular matrix L with all diagonal entries taking the value 1 and an upper
triangular matrix U with all diagonal entries nonzero, such that PA = LU, where all
matrices are of size N N. This is often known as the LU decomposition of A.
Noting that Eq. (A.1) may be written as

PAu = b,

where

b = Pb,

we see that the LU decomposition of A allows us to write the given linear system as

LUu = b. (A.2)

Care should be taken not to confuse the upper triangular matrix U with the vector of
unknows u. Setting v = Uu allows us to decompose the linear system as the coupled
system

Lv = b, Uu = v. (A.3)

Although it may appear that decomposing the linear system into the two systems
above doubles the work required, this is not the case. The structure of triangular
matrices allows us to explicitly solve the system very simply. Remembering that L
is a lower triangular matrix with all diagonal entries taking the value 1 and that U is
an upper triangular matrix, we may write Eq. (A.3) as:

1 0 0 ...
0 v1 b1
L2,1 1
0 0
... v2
b2
L3,1 L3,2
1 0
...
v3 = b3 , (A.4)
.. .. .. .. ..
..
. ..
. . . .
. .
LN,1 LN,2 LN,3 . . . 1 vN bN

U1,1 U1,2 . . . U1,N2 U1,N1 U1,N u1 v1
0 U2,2 . . . U2,N2 U2,N1 U2,N v2
u2
.. .. .. .. .. .. .. ..
. . . . . . .
. = . (A.5)
0 0 . . . UN2,N2 UN2,N1 UN2,N uN2 vN2

0 0 ... 0 UN1,N1 UN1,N uN1 vN1
0 0 ... 0 0 UN,N uN vN

We may sweep through the first of these linear systems, Eq. (A.4), solving for each
value of vi in turn:
214 Appendix A: Methods for Solving Linear and Nonlinear

v1 = b1 ,
v2 = b2 L2,1 v1 ,
v3 = b3 L3,1 v1 L3,2 v2 ,
.. ..
. .
vN = bN LN,1 v1 LN,2 v2 LN,3 v3 . . . LN,N1 vN1 .

Similarly, as U is an upper triangular matrix, we may sweep through the second


linear system, Eq. (A.5), by starting from the last equation:

1
uN = vN ,
UN,N
1 
uN1 = vN1 UN1,N uN ,
UN1,N1
1 
uN2 = vN2 UN2,N uN UN2,N1 uN1 ,
UN2,N2
.. ..
. .
1 
u1 = v1 U1,N uN U1,N1 uN1 . . . U1,2 u2 .
U1,1

Note that division by the diagonal entries of U in the equations above is well defined,
as we have explained that these entries are nonzero when using Gaussian elimination.
This completes the solution of the original linear system, Eq. (A.1).
We now evaluate LU decomposition as a practical method for solving the linear
systems that arise from the finite element method. As the size of the system increases,
the computational work required is dominated by computing the matrices P, L, U,
and the solution of Eq. (A.3) requires an insignificant computational effort in compar-
ison. This is a very useful observation when many linear systems defined by the same
matrix A are to be solved, as is the case for some of the parabolic partial differential
equations seen in Chap. 13. For these systems, we need only evaluate P, L, U once:
these matrices may then be reused. A further feature of LU decomposition to bear
in mind is that although A is a sparse matrix, the computed factors L and U will not,
in general, be sparse. Should N, the number of rows in the linear system, be large,
then, although storing the sparse matrix A is feasible, storing the dense matrices L
and U may not be possible. LU decomposition would then fail as a practical method
for solving Eq. (A.1).
In summary, LU decomposition is suitable for problems with a relatively small
number of unknowns. What a small number of unknowns actually means in this
context will depend on the computer being usedit is likely that small may mean
hundreds of thousands of modern laptop or desktop computers.
Readers may have wondered why we have not yet explained what a direct solver
is. This is because it is easier to do so with the aid of an example, such as Gaussian
elimination with partial pivoting. This algorithm uses prescribed steps, with a compu-
Appendix A: Methods for Solving Linear and Nonlinear 215

tational cost, both in terms of memory requirement and operations to be performed,


that is known in advance. The algorithm then returns a solution that we may, neglect-
ing computational rounding errors, assume to be the exact solution. These are the
distinguishing features of any direct solver.

A.1.2 Iterative Methods

In the previous section, we loosely defined a direct method for solving a linear system
to be a method that used prescribed steps, with computational complexity known a
priori, to calculate the exact solution to a problem. An alternative family of methods
are iterative methods, where an initial guess is made to the solution, and this guess
improved iteratively until it is sufficiently good. By sufficiently good, we usually
mean that Au b < for some prescribed , where v represents the norm of
a vector v. Iterative methods are often used to solve the sparse linear systems that
arise from the finite element method when the number of unknowns is too large for
a direct method, such as that described in Sect. A.1.1, to be applied.
In the absence of specialist knowledge, some basic advice for using iterative meth-
ods to solve the sparse linear system Au = b may be expressed very simply. If the
matrix A is symmetric and positive definitethat is, A = A and v Av > 0 for all
nonzero vectors vthen the conjugate gradient methodshould be used. If not the
Generalised Minimal RESiduals (GMRES) technique should be used. We present
the algorithm for both of these techniques to allow the reader to understand the
computations underpin these techniques and their consequences in terms of com-
putational memory and operations required. This is particularly important for the
use of GMRES, as this technique requires some input from the user that can have a
significant effect on the efficiency and computational memory requirements. Writing
down the algorithms also allows us to outline how convergence may be accelerated
by preconditioning techniques in Sect. A.1.3.
We also include a summary of two other iterative techniques: GaussSeidel itera-
tion and multigrid. GaussSeidel iteration is very rarely used as an iterative technique.
It is, however, often used as part of a multigrid technique. Multigrid techniques are
a family of iterative techniques for solving linear systems. It is not possible, in a
short appendix, to fully explain how these techniques may be applied as an effective
iterative solver. Instead, we give an overview of the principles that underpin this tech-
nique. Although we would not suggest that a novice uses a basic multigrid technique
as an iterative solver, we will claim, when discussing preconditioning in Sect. A.1.3,
that a basic multigrid technique can be an effective preconditioner.

A.1.2.1 The Conjugate Gradient Method

The conjugate gradient method may be used for solving linear systems Au = b
when the matrix A is symmetric and positive definite. Care should be taken to avoid
216 Appendix A: Methods for Solving Linear and Nonlinear

destroying the symmetry of A that arises in finite element applications by modifying


A to satisfy Dirichlet boundary conditions. The conjugate gradient method is set out
in Algorithm 1. Each step simply requires the evaluation of an algebraic expression.
Vectors qk , uk and rk are created on step k of the algorithm. If these vectors were stored
until the algorithm terminates, this could require substantial computational storage
should the number of iterations becomes large. Note, however, that these vectors
are only required for one further step of the iteration and may then be deleted. The
conjugate gradient method is therefore very easy to implement and requires only
minimal computational storage. It is also very easy to useit requires only an initial
guess u0 and tolerance . It can also be shown that this method has exceptional
convergence properties.

Algorithm 1 The conjugate gradient algorithm


1: Enter with initial guess u0 , tolerance > 0
2: r0 = Au0 b
3: q0 = r0
4: k=1
5: while rk1  > do
r rk1
6: k = qk1Aq
k1 k1
7: uk = uk1 + k qk1
8: rk = rk1 + k Aqk1
rk rk
9: k =  r
rk1 k1
10: qk = rk + k qk1
11: k =k+1
12: end while
13: u = uk1

A.1.2.2 The Generalised Minimal RESiduals (GMRES) Method

We saw in Sect. A.1.2.1 that the conjugate gradient algorithm required only the
evaluation of algebraic expressions that were straightforward to execute and did not
require substantial computational memory to store the results of these computations.
This makes the conjugate gradient technique a very attractive iterative method for
solving linear systems. However, this method may only be used for matrices that
are both symmetric definite and positive definite. A popular iterative technique for
matrices that do not have these properties is the GMRES method.
The steps required for the GMRES method are given in Algorithm 2. In contrast to
the conjugate gradient algorithm, there are two additional complexities. First, we see
that a dense matrix H is generated as part of this algorithm. On iteration j, this matrix
has j + 1 rows and j columns. If the number of iterations required is sufficiently small,
this will not cause a problem. However, if a large number of iterations are required,
this matrix will require substantial storage space in the computers memory, which
Appendix A: Methods for Solving Linear and Nonlinear 217

may not be possible. Furthermore, in line 15 of the algorithm, we see that we have
to solve a least squares problem during each iteration of the algorithm which, in
contrast to the operations required for the conjugate gradient method, is not a simple
algebraic operation.

Algorithm 2 The GMRES algorithm


1: Enter with initial guess u0 , tolerance > 0
2: r0 = Au0 b
3: = r0 
4: f1 = r0 /
5: j = 1
6: while rj1  > do
7: w = Afj
8: for i = 1, 2, . . . , j do
9: Hi,j = w fi
10: w = w Hi,j fi
11: end for
12: Hj+1,j = w
13: fj+1 = Hj+1,j
1
w
 
14: e1 = 1 0 0 . . . 0
15: Solve the least squares
problem to find yj such that yj = miny e1 Hy2
16: F = f1 f1 . . . fj
17: uj = u0 + Fyj
18: rj = Auj b
19: j =j+1
20: end while
21: u = uj1

In practice, generation of a large, dense matrix H may be avoided by limiting the


maximum number of times the while loop is executed to some fixed number M that
is specified by the user. If convergence has not been reached after M iterations of this
while loop, the algorithm exits with final iterate u. The GMRES algorithm is then
executed again, using this computed vector u as the initial guess. This is repeated
until convergence is obtained. Although this avoids the generation of a large, dense
matrix, there is no guarantee that this algorithm will terminate, and so a suitable
value of Moften known as the restart valueis estimated by trial and error. If
GMRES performs poorly for a given choice of M, then increasing M may improve
performance. This is not always true, however: see [2], for some counter-intuitive
observations on the choice of restart value.
We noted above that the GMRES algorithm requires the solution of a least squares
problem during each iteration of the algorithm. Least squares problems such as these
are typically solved using a QR factorisation of the matrix H, that is, computing a
factorisation H = QR where Q is an orthogonal matrix1 and R is an upper triangular
matrix. This factorisation may be calculated using GramSchmidt orthogonalisation,

1 An orthogonal matrix is a square matrix that satisfies QQ = I where I is the identity matrix.
218 Appendix A: Methods for Solving Linear and Nonlinear

a technique that may be implemented efficiently but is unstable to rounding errors.


When H is of a small size, these rounding errors usually have little effect, and this
may be the most appropriate method for computing the QR factorisation of H. The
numerical instabilities may be eliminated, however, by using the modified Gram
Schmidt orthogonalisation. This technique does, however, have the drawback of
being less efficient to implement. For more details, see Trefethen and Bau [11].
In summary, the GMRES method is a very useful iterative technique as it may be
applied to a general linear system. The user should be aware, however, that they must
specify both the restart value M and the method used for solving the least squares
problem that is embedded within the algorithm. The ability to specify both of these
features is included in all commonly used implementations of the GMRES method
and so the user, after some experience, will be able to tune their application of the
GMRES method to a specific linear system.

A.1.2.3 GaussSeidel Iteration

A further iterative technique is GaussSeidel iteration. As explained earlier, this


technique is rarely used as an iterative solver but often forms part of a multigrid
method that we describe later. Suppose we are solving the nonsingular linear system
Au = b, where all diagonal entries of A are nonzero. Write A = D + L + U, where
D is a diagonal matrix, L is a lower triangular matrix with all diagonal entries taking
the value zero and U is an upper triangular matrix with all diagonal entries taking
the value zero. Using these matrices, we may write the linear system to be solved as

(D + L)u + Uu = b.

This observation motivates the GaussSeidel algorithm given in Algorithm 3.

Algorithm 3 The GaussSeidel algorithm


1: Enter with initial guess u0 , tolerance > 0
2: r0 = Au0 b
3: k=1
4: while rk1  > do
5: Solve (D + L)uk = b Uuk1
6: rk = Auk b
7: k =k+1
8: end while
9: u = uk1

Noting that D + L is a lower triangular matrix, we see that the linear system
that is solved in line 5 of the GaussSeidel algorithm may be solved very easily
using the method described for solving the lower triangular system in Sect. A.1.1.
The computational memory requirements are also very lowthe vectors xk and rk
that are generated on each iteration only require preservation for at most one more
Appendix A: Methods for Solving Linear and Nonlinear 219

iteration and can then be deleted. The drawback is that there is no guarantee that this
algorithm will converge, and if it does converge, then convergence is usually slower
than that for GMRES or the conjugate gradient method.

A.1.2.4 Multigrid Techniques

The final iterative techniques that we discuss are the family of techniques known as
multigrid techniques. These techniques can be used as iterative solvers for linear sys-
tems: this was the initial purpose for developing them. We should emphasise that the
discussion that follows focuses only on a rudimentary implementation of multigrid
techniques and does not provide sufficient discussion to allow the implementation of
multigrid as an efficient iterative solver. The purpose of this discussion will become
clear in Sect. A.1.3, when we explain that a basic implementation of multigrid may be
used to accelerate convergence of the GMRES algorithm presented in Sect. A.1.2.2.
As the name suggests, multigrid techniques use several computational meshes
or grids. Suppose a finite element solution is required on a computational mesh,
M1 , with many nodes, and that the finite element solution on this mesh satisfies
A1 u1 = b1 , where both A1 and b1 are evaluated from the mesh M1 . Suppose further
that M1 , M2 , . . . , MJ are a sequence of successively coarser meshes and that, were
we to calculate the finite element solution on the mesh Mj , j = 1, 2, . . . , J, we would
obtain the linear system Aj uj = bj . We assume that, for j = 1, 2, . . . , J 1, we may
restrict vectors on mesh Mj to the coarser mesh Mj+1 using

uj+1 = Rj uj ,

where Rj , j = 1, 2, . . . , J 1, is a matrix known as the restriction operator. Sim-


ilarly, we assume the existence of a matrix Pj , j = 1, 2, . . . , J 1, known as the
prolongation operator, that allows us to transfer a vector on the mesh Mj+1 onto the
finer mesh Mj using

uj = Pj uj+1 ,

Our final assumption is that the coarsest mesh, MJ , contains sufficiently few nodes
that the resulting linear system can easily be solved using a direct method.
The ideas behind the basic multigrid techniques are simple. Let uj be the solution
of Aj uj = bj for j = 1, 2, . . . , J, and let vj be an approximation to uj . We define

rj = Aj vj bj , ej = vj uj , j = 1, 2, . . . , J.

Remembering that Aj uj = bj , we may deduce that

Aj ej = rj . (A.6)
220 Appendix A: Methods for Solving Linear and Nonlinear

Given an initial guess on the finest mesh M1 , we first perform a small number of
GaussSeidel iterations on the equation A1 u1 = b1 using this initial guess, before
computing the residual r1 on this fine mesh. We then restrict this residual onto the
next mesh M2 using r2 = R1 r1 , before performing a small number of GaussSeidel
iterations on Eq. (A.6) to update the residual r2 . This is repeated on the successively
coarser meshes until we restrict the residual onto the coarsest mesh, MJ , where we
solve Eq. (A.6) exactly. We then proceed back through the meshes, prolongating the
solution onto successively finer meshes and performing a small number of Gauss
Seidel iterations until we reach the finest mesh, M1 , where we update our initial
guess. We complete the multigrid cycle by performing a small number of Gauss
Seidel iterations of the equation A1 u1 = b1 .
The multigrid method described above is presented in Algorithm 4. This multigrid
cycle is repeated until the residual is sufficiently small. Note that the operations
required are all algebraic operations that can be evaluated efficiently and require
minimal computational storage when the algorithm is being performed.

Algorithm 4 One cycle of a basic multigrid algorithm


1: Enter with initial guess u1
2: M GaussSeidel iterations of A1 1 = b1 , with initial guess 1 = u1
3: r1 = A1 1 b1
4: for j = 2,3,,J-1 do
5: bj = Rj1 rj1
6: M GaussSeidel iterations of Aj j = bj , with initial guess j = 0
7: rj = Aj j bj
8: end for
9: bJ = RJ1 rJ1
10: Solve exactly AJ dJ = bJ
11: for j = J1,J2,,2 do
12: j = j + Pj dj+1
13: M GaussSeidel iterations of Aj dj = bj , with initial guess dj = j
14: end for
15: u1 = u1 + P1 d2
16: M GaussSeidel iterations of A1 1 = b1 , with initial guess 1 = u1
17: u = 1

There is clearly much flexibility when using this techniqueusing either a dif-
ferent number of grids or a different number of GaussSeidel iterations are obvious
candidates for improving convergence properties. Many other options also exist for
accelerating convergence, of which we briefly mention only a few. GaussSeidel iter-
ations were used in Algorithm 4 to smooth the error ej on mesh Mj . Other smoothers
exist and may enhance the convergence properties. We also used the same number
of GaussSeidel iterations on all meshes and on both the restricted and prolongated
solutionswe may allow these to vary if we wish. Furthermore, in Algorithm 4,
we iterated through meshes M1 , M2 to the finest mesh MJ and then back along the
reverse path to the finest mesh M1 . A more flexible technique uses cycles that are
not as uniform as this.
Appendix A: Methods for Solving Linear and Nonlinear 221

A.1.3 Preconditioning

It is now a good time to take stock of the material presented so far in this appendix.
We discussed direct methods for solving linear systems in Sect. A.1.1 and proposed
using these methods when the number of unknowns was sufficiently small that the
memory requirements were not excessive. If the number of unknowns is too large
for a direct method to be used, we proposed, in Sect. A.1.2, that an iterative method
would be more appropriate. Specifically, we propose using the conjugate gradient
method if the matrix is symmetric and positive definite, and the GMRES method
otherwise. We have not discussed the number of iterations taken for either of these
iterative techniques to converge as this requires far more detail than can be given in
a book on the finite element method. Nevertheless, we do think it is appropriate to
give some practical tips on a technique known as preconditioning for reducing both
the number of iterations required andimportantlythe computation time.
When computing finite element solutions, we often need to solve sparse linear
systems Au = b using an iterative technique. If M is a nonsingular matrix, then this
linear system is equivalent to

M 1 Au = M 1 b. (A.7)

This equation is known as the preconditioned system, and the matrix M is known
as a preconditioner. Suppose we choose the preconditioner so that M = A. This
choice of preconditioner would allow us to write Eq. (A.7) as u = M 1 b, and we
would then have solved the linear system. This choice of preconditioner is clearly not
appropriateif we were able to evaluate A1 , then we may as well use a direct method
to solve the original system. This observation motivates us to specify two conditions
that a preconditioning matrix must satisfy. The first condition is that the product M 1 A
should bespeaking very looselysome approximation to the identity matrix. We
deliberately leave this as a very imprecise statement as, in this context, a matrix
with tightly clustered eigenvalues may be considered a suitable approximation. The
second condition is that the action of M 1 on a vector is computationally cheap to
compute. This second condition requires explanation as it has significant practical
implications. Note that we do not need an explicit expression for the matrix M 1 .
Indeed, for most preconditioners that are used in practice, an expression for M 1 is
not available. Suppose we want to compute M 1 v for some given vector v. Let us
write w = M 1 v. Computing w is then equivalent to solving the linear system

Mw = v.

If M has been factorised as M = LU, where L is a lower triangular matrix, and U is


an upper triangular matrix, we may easily solve this linear system using the method
described in Sect. A.1.1. Evaluation of w = M 1 v, i.e. the action of M 1 on the
vector v, is therefore possible without M 1 being explicitly defined.
222 Appendix A: Methods for Solving Linear and Nonlinear

Suppose we are solving a linear system Au = b using either the conjugate gradient
method presented in Algorithm 1 or the GMRES method presented in Algorithm 2.
In both algorithms, we see that the only operations involving the matrix A are matrix
vector multiplications of the form Av. Suppose, instead, we solve the preconditioned
system given by Eq. (A.7), using a preconditioner M that satisfies the conditions
above. As the second of these conditions is that the action of M 1 on any vector is
cheap to compute, we may easily compute the right-hand side of this linear system,
i.e. M 1 b. When using either the conjugate gradient or GMRES algorithm for the
preconditioned system, we then have to evaluate matrixvector multiplications of
the form M 1 Av for any vector v. These may also be evaluated cheaply: writing
w = Av, we see that

M 1 Av = M 1 w,

which, using our assumption on the preconditioning matrix, is cheap to compute.


A preconditioner that satisfies the conditions outlined above will hopefully require
fewer iterations of an iterative method, while not significantly increasing the com-
putational cost of each iteration.
The preconditioned system given by Eq. (A.7) is known as a left preconditioned
system. An alternative approach is the right preconditioned system given by

AM 1 v = b, (A.8)
where u = M 1 v. (A.9)

The solution of the right preconditioned system may be computed by first solving
Eq. (A.8) for v, followed by solving Eq. (A.9) for u. The right preconditioned sys-
tem has similar properties to the left preconditioned system in that it will hopefully
decrease the number of iterations required without significantly adding to the com-
putational cost of each algorithm.
Care should be taken when preconditioning the conjugate gradient method. This
iterative technique is underpinned by the assumption that the matrix A is symmetric
and positive definite, and these properties of A must not be destroyed by precondi-
tioning. This may be achieved by writing the preconditioner as M = CC  for some
matrix C and then writing the system Au = b as:
 
C 1 A C 1 v = b,
 1
where u = C  v.

We have now described what a preconditioner is and what properties it must


satisfy. We have not yet proposed any practical preconditioners. We now give two
examples.
Appendix A: Methods for Solving Linear and Nonlinear 223

A.1.3.1 ILU Preconditioning

Incomplete LU preconditioningoften known as ILU preconditioningis a simple


preconditioner that is straightforward to implement, yet can be very effective. This
preconditioner generates two matrices L, U that are approximations to the full LU
decomposition of A discussed in Sect. A.1.1. The matrix L is a lower triangular matrix,
with nonzero entries only in the same locations as the nonzero entries in the lower
triangular portion of the matrix A. Similarly, the matrix U is an upper triangular
matrix, with nonzero entries only in the same locations as the nonzero entries in
the upper triangular portion of the matrix A. The preconditioner M is then given
by M = L U. The action of M 1 on a given vector v is cheap to compute: writing
w = M 1 v, we see that we may compute w by solving the linear system

L Uw = v,

and we saw in Eqs. (A.2) and (A.3) that systems of this form can be solved very
easily.
ILU preconditioning is available in all major open-source and commercial soft-
ware packages for the iterative solution of linear systems and is a very good default
choice of preconditioner. See [12] for more details on the implementation of this
preconditioner.

A.1.3.2 Multigrid as a Preconditioner

In Sect. A.1.2.4, we presented a basic multigrid method as an iterative solver. This


iterative solver required repeated application of Algorithm 4 until the prescribed
tolerance for convergence was met. We now note that this algorithm may actually
form the basis of a preconditioner. For a general vector v, we may define M 1 v to
be a small number of iterations of a multigrid algorithm to the vector v. This clearly
satisfies both conditions for a preconditioner set out in Sect. A.1.3. As many iterations
of the multigrid algorithm would solve the linear system, we may approximate the
inverse of A by a small number of iterations of this algorithm. Furthermore, we
noted in Sect. A.1.2.4 that an application of a low number of cycles of the multigrid
algorithmthat is, the action of M 1 on vis computationally cheap.
Multigrid has been shown to be an effective preconditionersee [6] for more
details. It is, however, more difficult to implement than ILU preconditioning.
Although it is available in some software packages, implementation of this precon-
ditioner requires significant programming input from the user such as, for example,
specification of the prolongation and restriction operators between meshes. In con-
trast, ILU preconditioning may be utilised in most software packages by a very small
number of lines of code that are generic for any application.
224 Appendix A: Methods for Solving Linear and Nonlinear

A.2 Nonlinear Systems

A general nonlinear system of N algebraic equations may be written in vector form


as

R(u) = 0, (A.10)

where u is a vector of N unknowns. The most popular iterative technique for solving
systems of equations such as Eq. (A.10) is known as Newtons method, which we
now describe.
Suppose that u is a solution of this system, so that R(u ) = 0. Note that we say
a solution rather than the solutionthere is no guarantee that this solution is
unique or, indeed, that a solution u exists. Approximating component i of R as a
Taylor series about a general point u, up to and including all linear terms in u u,
gives

N

Ri (u ) Ri (u) + Jij uj uj , i = 1, 2, . . . , N,
j=1

where J denotes the Jacobian matrix, with entries given by



Ri
Jij = ,
uj u

where the solid vertical line with subscript u indicates that entries of J are evaluated
at u. We may write this in vector form as

R(u ) R(u) + J u u .

Noting that u is a solution of Eq. (A.10), we have R(u ) = 0, and assuming the
matrix J is invertible, we may approximate the solution of Eq. (A.10) by

u u J 1 R(u).

This approximation to u clearly has the potential to be very crudeapart from


assuming that the Jacobian matrix is invertible, we have assumed that u is a suffi-
ciently good guess to the solution u that the Taylor series of R about u, up to and
including only linear terms, is sufficiently accurate to evaluate R(u ).
Newtons method is usually implemented using a modification to the approxima-
tion above. Given an initial guess to the solution u0 , we then generate successive
iterates u1 , u2 , u3 , . . . using Algorithm 5.
It is crucial to understand that Newtons method should not be considered a bul-
letproof technique for solving systems of nonlinear algebraic equations. Even if the
system of equations has a unique solution, there is no guarantee that Newtons method
Appendix A: Methods for Solving Linear and Nonlinear 225

Algorithm 5 Newtons method for solving nonlinear systems of algebraic equations


1: Enter with initial guess u0 , tolerance > 0
2: r0 = R(u0 )
3: k=1
4: while rk1  > do
5: Solve J = R(uk1 ) where the entries of J are given by

Ri
Jij = ,
uj uk1

6: Set uk = uk1 + s, where 0 < s 1 is chosen to minimise R(uk ).


7: rk = R(uk )
8: k =k+1
9: end while
10: u = uk1

will converge to this solution for a given initial guess. This may be illustrated using
a very simple example of a nonlinear equation in one scalar variable u. The function
R(u) = ueu has the unique solution u = 0 to the equation R(u) = 0. It is, however,
fairly easy to show that Newtons method will not converge to this solution for any
initial guess satisfying u0 1. Furthermore, should a nonlinear equation possesses
multiple roots, there is no guarantee which of these roots Newtons method will con-
verge to (if it does indeed converge). In particular, we should not expect Newtons
method to converge to the root closest to the initial guess. An excellent illustration
of this point may be found in Chap. 4 of Sli and Mayers, [10], using the example of
solving the equation ez z 2 = 0 for the complex number z = x + iy. A system
of two nonlinear equations for x and y may be derived by decomposing this equation
into real and imaginary parts. There are an infinite number of roots of this system of
equations, and the root that Newtons method converges to for a given initial guess
possesses some very unexpected properties. See Sli and Mayers, [10], for more
details.
The discussion in the previous paragraph should be viewed as a brief summary
of the negative aspects of Newtons method. This should be balanced by the positive
aspects. Newtons method is simple to implement, applicable to general systems of
equations, and is usually considered the go-to method for solving general nonlinear
systems of algebraic equations. We emphasise that widely available open-source and
commercial implementations of Newtons method are available. Many of these are
reliable, robust and efficient, and so the reader need not implement these algorithms
themselves. Some of these implementations require only specification of the function
R(u), while some require specification of both R(u) and the Jacobian matrix J. The
entries of J are partial derivatives of the entries of R(u), and the reader may prefer to
evaluate these derivatives numerically. If this approach is taken, then for efficiency,
the contributions to the numerical derivatives should be computed on each element
as described in Sect. 5.1.7.
Appendix B
Vector Calculus

In this appendix, we give a very brief summary of the vector calculus that is required
when applying the finite element method to partial differential equations in two
spatial dimensions.

B.1 The Gradient Operator

The gradient operator, , is defined to be the operator



x
= .
y

If a function u(x, y) possesses first partial derivatives with respect to x and y, the
gradient of u(x, y) is defined by
u
x
u = u .
y

Similarly, if a vector-valued function F(x, y), given by


 
F1 (x, y)
F(x, y) = ,
F2 (x, y)

is such that F1 (x, y) and F2 (x, y) both possess first partial derivatives with respect to
x and y, the divergence of F is defined by

Springer International Publishing AG 2017 227


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0
228 Appendix B: Vector Calculus
 
x F1 (x, y)
F =
F2 (x, y)
y

F1 F2
= + .
x y

As a consequence,
u
x x
(u) =
u
y y
u u
2 2
= + 2. (B.1)
x 2 y

The expression above is sometimes written as 2 u.

B.2 A Useful Vector Identity

Suppose the function u(x, y) possesses all first and second partial derivatives, and
the functions v(x, y) and p(x, y) possess all first partial derivatives with respect to
both x and y. An identity used when deriving the weak solution of partial differential
equations is

(vpu) = v (pu) + p (v) (u) . (B.2)

B.3 The Divergence Theorem

Let be a closed, bounded region in two dimensions, with piecewise smooth bound-
ary . Let F(x, y) be continuous, with continuous first partial derivatives on the
region . We then have
 
F dA = F n ds, (B.3)

where n is the normal vector pointing out of , with unit length.


Further Reading

Some additional texts that have been cited in earlier chapters have been listed below.

References

1. Elman, H., Silvester, D., Wathen, A.: Finite Elements and Fast Iterative Solvers. Oxford Uni-
versity Press, Oxford (2005)
2. Embree, M.: The tortoise and the hare restart GMRES. SIAM Rev. 45, 259266 (2003)
3. Golub, G.H., Van Loan, C.F.: Matrix Computations, 4th edn. The Johns Hopkins University
Press, Baltimore (2012)
4. Greenbaum, A.: Iterative Methods for Solving Linear Systems. Society for Industrial and
Applied Mathematics, Philadelphia (1997)
5. Murray, J.D.: Mathematical Biology. Springer, Berlin (1993)
6. Oosterlee, C.W., Washio, T.: An evaluation of parallel multigrid as a solver and a preconditioner
for singularly perturbed problems. SIAM J. Sci. Comput. 19, 87110 (1998)
7. Persson, P.-O., Strang, G.: A simple mesh generator in matlab. SIAM Rev. 46, 329345 (2004)
8. Shewchuk, J.R.: Triangle: engineering a 2D quality mesh generator and delaunay triangulator.
Lect. Notes Comput. Sci. 1148, 203222 (1996)
9. Shewchuk, J.R.: Delaunay refinement algorithms for triangular mesh generation. Comput.
Geom. Theory Appl. 22, 2174 (2002)
10. Sli, E., Mayers, D.F.: An Introduction to Numerical Analysis. Cambridge University Press,
Cambridge (2006)
11. Trefethen, L.N., Bau, D.: Numerical Linear Algebra. Society for Industrial and Applied Math-
ematics, Philadelphia (1997)
12. Van der Vorst, H.A.: Iterative Krylov Methods for Large Linear Systems. Cambridge University
Press, Cambridge (2003)
13. Wathen, A.J.: Preconditioning. Acta Numer. 24, 329376 (2015)

Springer International Publishing AG 2017 229


J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0
Index

B Error bound, 47, 78


Basis functions Existence and uniqueness of solutions, 26
bilinear, 164
cubic, 71
defined on canonical element, 32, 57, 71, F
76, 125, 164, 178 Finite element mesh, see Mesh
general, 75, 76 Finite element solution
linear, 9, 30, 125 evaluating, 16, 19, 135
quadratic, 56, 57, 176 Fishers equation, 205
Boundary conditions
Dirichlet, 6, 25
natural, 26 G
Neumann, 25 Gaussian elimination, 212
Robin, 45 pivoting, 212
Gaussian quadrature, see Quadrature
GaussSeidel iteration, 215, 218
C GMRES, 137, 215, 216
Canonical element, 32, 57, 71, 125, 164, 178 Gradient operator, 227
Classical formulation, 6
Classical solution, 6
Computational domain, see Domain H
Computational implementation, 17, 42, 65, Hanging node, 124, 163
87, 93, 110
Computational mesh, see Mesh
Conjugate gradient method, 215 I
Connectivity array, 123, 162, 176 ILU preconditioning, 137, 223
Implicit approximation in time, 204

D
Dirichlet boundary conditions, see Bound- L
ary conditions Linear systems of algebraic equations
Divergence theorem, 228 solution of, 211, 212
Domain, 6 direct methods, 212
iterative methods, 215
Local contributions, 14, 87
E Lower triangular matrix, 212
Elements, 8, 9, 30, 56, 70, 76, 83 LU decomposition, 213
Springer International Publishing AG 2017 231
J. Whiteley, Finite Element Methods, Mathematical Engineering,
DOI 10.1007/978-3-319-49971-0
232 Index

M Permutation matrix, 212


Mesh, 8, 9, 30, 56, 70, 76, 83, 105, 145, 162,
176
quadrilateral elements, 162 Q
triangular elements, 122, 176 Quadrature, 40, 151, 171
quality, 125 quadrilateral domains, 171
Mesh generation, 139 triangular domains, 151
distmesh, 139 use of, 69
triangle, 139
Multigrid, 215, 219
as a preconditioner, 223 S
prolongation operator, 219 Semi-implicit approximation in time, 207
restriction operator, 219 Sobolev spaces
functions of one variable, 27
functions of two variables, 120
N Sparse matrix, 137, 212
Newtons method, 224 Square integrable functions, 27, 120
Nodes, 8, 9, 30, 56, 70, 76, 83, 122, 176 Systems of differential equations, 103, 197
Nonlinear systems of algebraic equations,
86, 192
Jacobian matrix, 90, 96, 193
T
residual, 86, 192
Test functions, 8, 130
solution of, 211, 224
solving, 88, 90, 93, 96
Numerical stability, 204
U
Upper triangular matrix, 212
O
One based indexing, 6
W
Weak formulation, 6, 7, 27, 28, 46, 82, 97,
P 113, 120, 144, 162, 190, 198
Partial differential equations Weak solution, see Weak formulation
elliptic, 189, 197
nonlinear, 189
parabolic, 203 Z
Pattern formation, 208 Zero based indexing, 6

You might also like