Norwegian University of Science and Technology Department of Mathematical Sciences TMA422 Numerical Solution of Partial Differential Equations Using Element Methods Høst 214 Exercise set 2 1 Consider the equation u xx = f(x), < x < 1, u() =, u(1) =, which is solved with the finite element method with equidistant grid (x i = ih, h = 1/N), and the basis functions x x i 1 h, for x i 1 x x i, x ϕ i (x) = i+1 x h for x i x x i+1, otherwise. for i = 1,2,...,N 1. Denote the numerical solution u N. In Exercise 1.3, you solved this equation with f(x) = x 4. You may have noticed that the numerical and exact solution coincide on the gridpoints, that is u(x i ) = u N (x i ), for i =,1,2,...,N. In this problem, we are to show that this is true for all f(x). For i = and i = N, there is nothing to prove (Why?), what remains is i = 1,2,...,N 1. a) Prove that a(u u h,ϕ i ) = for i = 1,2,...,N 1. Solution: Let V h = span{ϕ 1,ϕ 2,...,ϕ N 1 } V = H 1 ([,1]). Then the numerical solution satisfies a(u h,v h ) = f(v h ) v V h, while the exact solution satisfies ϕ i V h V, so a(u,v) = f(v) v V. a(u u h,ϕ i ) = a(u,ϕ i ) a(u h,ϕ i ) a bilinear. = f(ϕ i ) f(ϕ i ) u and u h solves the wea problems. =. b) Prove that for all v H 1 ([,1]) C ([,1]) and i = 1,2...,N 1, a(v,ϕ i ) = 1 h (2v(x i) v(x i 1 ) v(x i+1 )). September 1, 214 Page 1 of 7
Solution: a(v,ϕ i ) = v x ϕ i. 1 ϕ h, for x i 1 < x < x i, i = 1 h, for x i < x < x i+1,, otherwise. a(v,ϕ i ) = xi x i 1 v x h xi+1 x i v x h c) The M M matrix = 1 h (v(x i) v(x i 1 ) (v(x i+1 ) v(x i ))) = 1 h (2v(x i) v(x i 1 ) v(x i+1 )). 2 1. A = 1 2........ 1 1 2 is invertible for all M. Use this fact and the results of a) and b) to prove that u(x i ) u N (x i ) = for i = 1,2...,N 1. Solution: Combining the results of a) and b), we see that v i = u(x i ) u N (x i ) satisfies v i 1 +2v i v i+1 = for i = 1,2,...,N 1. Removing v and v N, (which are equal to zero) this is equivalent to Av = where A is as above and v = [v 1 v 2...v N 1 ]. Since A is invertible, Av = v =. 2 Given the Helmholtz problem u xx +σu = f on (,1), u() = u(1) =. where σ > is a constant. Set up the wea form for this problem. Show that, when this problem is solved by a Galerin method, using V h = span {φ i } N i=1, the discrete problem can be written as (A+σM)u = f. Set up the matrix M for V h = Xh 1 on a uniform grid. Solution: Multiply by the equation by a test function v, integrate over the domain September 1, 214 Page 2 of 7
(,1), use partial integration and get rid of the boundary terms by require v() = v(1) =. The the wea formualtion becomes: Find u H 1 (Ω) such that v x u x +σ uv = fv, v H 1 (,1). Choose V h = span{ϕ 1,ϕ 2,...,ϕ N }, let our unnown approximation be written as u h (x) = where the coefficients u j is found from u j ϕ j (x), ( dϕj ) dϕ i +σ (ϕ j ϕ i ) = (f ϕ i ), i = 1,2,,N. This is a linear system of equations Au+σMu = f where (A i,j = ( dϕj ) dϕ i, M i,j = (ϕ j ϕ i ) and f i = (fϕ i ). Let V h = X 1 h on a uniform grid, so that h = 1/N and x i = ih, i =,1,...,N. ϕ i 1 ϕ i ϕ i+1 h h h h x i 2 x i 1 x i x i+1 x i+2 The non-zeros elements of the stiffness matrix A and the mass matrix M is xi+1 ( ) dϕi A i,i = = 2 xi+1 h, A dϕ i i,i+1 = = 1 h = A i+1,i M i,i = x i 1 xi+1 x i 1 ϕ 2 i = 2 3 h, M i,i+1 = dϕ i+1 x i xi+1 x i ϕ i+1 ϕ i = 1 6 h = M i+1,i. All the other elements are. So 4 1 M = h 1 4 1. 6 1 4........ 1 1 4 R (N 1) (N 1). September 1, 214 Page 3 of 7
3 Write a MATLAB program for solving the Helmholtz problem u xx +σu = f(x), < x < 1, u() = u(1) =. or, using the wea formulation find u H 1 (,1) s.t. u x v x +σ uv = fv, for all v H 1 (,1) (1) by the finite element method on Xh 2, using the algorithm outlined in the supplementary note. To test your code, let σ = 1, f = sin(πx) in which case u(x) = sin(πx)/(1+π 2 ). Use for example [,.1,.25,.3,.4,.45,.5,.55,.6,.7,.8,.9,1] for the partition of the elements. As already pointed out in exercise 2, the discrete problem can be written as (A+σM)u = b. (2) So the tas is to set up the matrices A and M and the load vector b, and solve the system. What you have to do is described in the following: a) Preliminaries: Set up the element matrices A K h and MK h, corresponding to contribution from element K to the first and second integrals of (1) resp. Solution: Starting from the very beginning: The quadratic shape functions defined on the reference element ˆK = (,1), corresponding to the nodes x 1 =, x 2 = 1/2 and x 3 = 1 is given by ψ 1 (ξ) = 2(ξ 1 2 )(ξ 1), ψ 2(x) = 4ξ(ξ 1), ψ 3 (x) = 2ξ(ξ 1 2 ). The mapping from the ˆK to K = (,+1 ) is given by x(ξ) = + h ξ and the inverse mapping is ξ(x) = (x )/h, where h = +1. A K h = 1 7 8 1 8 16 8, Mh K 3h = h 4 2 1 2 16 2 3 1 8 7 1 2 4 So the contribution from element K to the element matrices Mh K and AK h becomes: (M K h ) α,β = (A K h ) α,β = x+1 x+1 ϕ K α (x)ϕ K β (x) = h dϕ K α ψ α (ξ)ψ β (ξ)dξ dϕ K β = 1 ψ α dψ β h dξ dξ dξ se the slides on the webpage for details. Altogether, we ends up with: A K h = 1 7 8 1 8 16 8, Mh K 3h = h 4 2 1 2 16 2 3 1 8 7 1 2 4 September 1, 214 Page 4 of 7
b) Write a function computing integrals by the following quadrature formula: g(x) 1 2 (g(c 1)+g(c 2 )), c 1,2 = 1 2 ± 3 6. This will be used for to approximate the contribution from an element to the load vector. Solution: On an element, the approximation becomes x+1 g(x) g( +c 1 h )+g( +c 2 h ) 2 But the contributions to the load vector is +1 b K i = x+1 The corresponding function can be ϕ i (x)f(x) φ i (x)f(x), so that 2 ψ i (c j )f( +c j ). function b = b(f,x) % Calculate a numerical approximation to the elemental load vector % of the function f on an element [x(1),x(2)]. % using quadratic elements. h = x(2)-x(1); c1 = 1/2-sqrt(3)/6; c2 = 1/2+sqrt(3)/6; b = h*[(c1-.5)*(c1-1)*f(x(1)+h*c1) + (c2-.5)*(c2-1)*f(x(1)+h*c2);... 2*c1*(1-c1)*f(x(1)+h*c1) + 2*c2*(1-c2)*f(x(1)+h*c2); c1*(c1-.5)*f(x(1)+h*c1)+c2*(c2-.5)*f(x(1)+h*c2)]; c) Assemble the prototype matrices Ãh and M h as well as the load vector b.. Solution: For points b)-e), see Figure 1. d) Remove the rows and columns corresponding to the boundary conditions. e) Solve (2), and plot the solution. f) Change the boundary conditions to u() = 1 and u x (1) = 2. Which changes has to be done in the code? Solution: The wea formulation of the Helmholtz problem is now find u V s.t. u x v x +σ uv = fv R g,x v x +2v(1), for all v V where v = {v H 1 (,1) : v() = } and R g H 1 (,1) is some function satisfying R g () = 1. For the numerical solution, we choose R g = ϕ (x). The correpsonding discrete formulation is u j ϕ j,x ϕ i,x +σ u j ϕ j ϕ i = j= fϕ i ϕ i,x ϕ,x + 2φ i (1) for i = 1,2,,N. In practice, this means that the column corresponding to the left hand side Dirichlet condition is moved to the right hand side. In addition, we add one term in the last equation, to include the contribution from the Neumann boundary conditions. Line 31-41 in Figure 1 has to be changed to September 1, 214 Page 5 of 7
f = @(x) sin(pi*x); sigma = 1; % The partition of [,1]. x = [,.1,.25,.3,.4,.45,.5,.55,.6,.7,.8,.9,1]; A = [7/3, -8/3, 1/3; % Element stiffness matrix -8/3, 16/3, -8/3; 1/3, -8/3, 7/3]; M = [2/15, 1/15, -1/3; % Element mass matrix 1/15, 8/15, 1/15; -1/3, 1/15, 2/15]; N = length(x)-1; % Number of elements. N = 2*N+1; % Number of nodes (including the boundaries) theta = @(,alpha) 2*(-1)+alpha; % local-to-global mapping. Ah = sparse(n,n); Mh = sparse(n,n); bh = zeros(n,1); % Stiffness matrix % Mass matrix resp. % Load vector % Assemble process: for = 1:N h = x(+1)-x(); % Size of the element gi = theta(,1):theta(,1)+2; Ah(gi,gi) = Ah(gi,gi) + A/h; Mh(gi,gi) = Mh(gi,gi) + M*h; bh(gi) = bh(gi) + b(f,[x(),x(+1)]); end % For the case u()=u(1)= % Remove contributions concerning the Dirichlet boundaries. A = Ah(2:end-1,2:end-1); M = Mh(2:end-1,2:end-1); b = bh(2:end-1); % Solve the system u = (A+sigma*M)\b; % Include the boundaries. u = [;u;]; % For the plot: Create a x-vector with all the nodes % including the midpoints of each element. xi = x(1); for =1:N xi = [xi;x()+(x(+1)-x())/2; x(+1)]; end % Plot the numerical and the exact solution in the nodes. u_exact = sin(pi*xi)/(1+pi^2); plot(xi,u_exact, r,xi,u, b ) legend( u_exact, u_h ); Figure 1: Code for solving the Helmholtz equation in 1D by a quadratic FEM September 1, 214 Page 6 of 7
% For the case u()=1, u_x(1)=2. bh = bh - Ah(:,1)*1; bh(end) = bh(end)+2; A = Ah(2:end,2:end); M = Mh(2:end,2:end); b = bh(2:end); % Solve the system u = (A+sigma*M)\b; % Include the Dirichlet boundary. u = [1;u;]; September 1, 214 Page 7 of 7