ABSTRACT

In this chapter, we will demonstrate the practical implementation of finite element methods for a 2-D convection-diffusion problem:

−u + v · ∇u = s in Ω, (7.1) u = uexact on ∂Ω. (7.2)

The basic procedure for solving partial differential equations (PDE) is as follows:

1. Discretize the physical domain into finite elements; 2. Rewrite the PDE in its weak formulation; 3. Calculate element matrices for each finite element; 4. Assemble element matrices to form a global linear system; 5. Implement the boundary conditions by modifying the global linear sys-

tem; 6. Solve the linear system and postprocess the solution. In the rest of this chapter, we will discuss details of implementation of

the above steps. More specifically, in Sec. 7.1, we present a simple gridgeneration algorithm and its implementation. Sec. 7.2 formulates the finite element equation for the convection-diffusion model. In Sec. 7.3, we discuss how to calculate those elementary matrices. Then we discuss the finite element assembly procedure and how to implement the Dirichlet boundary condition in Sec. 7.4. In Sec. 7.5, we present the complete MATLAB code for the P1 element and an example problem solved by our code. Finally, we present a MATLAB code for the Q1 element and demonstrate how to use it to solve an elliptic problem.