MATH 212 Advanced Calculus 2 for Electrical Engineering Advanced Calculus 2 for Nanotechnology Engineering NE 217 1-D Finite-Element Methods with Tent Test Functions Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011 by Douglas Wilhelm Harder. All rights reserved. 1-D Finite-element Methods with Tent Test Functions Outline This topic discusses an introduction to finite-element methods – Background – Define tent-shaped test functions – Perform integration-by-parts to get the linear equation • Approximate the solution by a linear solution • Find the system of equations 2 1-D Finite-element Methods with Tent Test Functions Outcomes Based Learning Objectives By the end of this laboratory, you will: – Understand the use of test functions in one dimension – Understand how to create a system of linear equations using these test functions in one dimenion 3 1-D Finite-element Methods with Tent Test Functions The Target Equation At this point, we have generalized the finite-difference method to allow arbitrarily spaced points in one dimension The ultimate goal is to do this in 2 and 3 dimensions – In order to achieve this, we need one further idea 4 1-D Finite-element Methods with Tent Test Functions Test Functions Up to this point, we have used uniform test functions: 1 x1 x x3 2 x 0 otherwise 5 1-D Finite-element Methods with Tent Test Functions Test Functions Two problems: – The impulse function appears in the integrals, and – This will not generalize to two and three dimensions! 1 x1 x x3 2 x 0 otherwise 6 1-D Finite-element Methods with Tent Test Functions Test Functions Solution: – Use a tent function 2 x 3 x 4 x 5 x 7 1-D Finite-element Methods with Tent Test Functions Test Functions Defining such functions is straight-forward: x x1 x x x1 x x2 2 1 x x3 2 x x2 x x3 x2 x3 0 otherwise x2 x1 x2 x3 1 , If x = x2, both x2 x1 x2 x3 x3 x3 x1 x1 0 0 if x = x1, , and if x = x3, x2 x3 x2 x1 8 1-D Finite-element Methods with Tent Test Functions Integration by Parts Now, substitute this test function into the integral: b b x d2 x u x dx x a 2 d 2 x a 2 0 dx which yields 3 3 x d d d dx 2 x1 u x 2 x u x dx 2 x dx dx 0 dx x1 x1 x1 x3 x x 9 1-D Finite-element Methods with Tent Test Functions Integration by Parts The tent function disappears at x1 at x3: 0x 3 3 x d d d x u x x u x dx x dx 2 2 2 1 dx dx 0 dx x1 x1 x1 3 x x thus, this simplifies to: x3 x3 x d d 2 x u x dx 2 x dx dx 0 dx x1 x1 10 1-D Finite-element Methods with Tent Test Functions Integration by Parts The tent function disappears at x1 at x3: 0x 3 3 x d d d x u x x u x dx x dx 2 2 2 1 dx dx 0 dx x1 x1 x1 3 x x thus, this simplifies to: x3 x3 x d d 2 x u x dx 2 x dx dx 0 dx x1 x1 A similar property will be used in 2 and 3 dimensions – remember this fact! 11 1-D Finite-element Methods with Tent Test Functions Integration by Parts Recall that both the test function 2(x) and u x are defined on x1 x x3 : x x1 x x2 u u x1 x x2 2 1 x x x2 x1 1 2 u x u x x3 u x x2 x x x 3 2 3 2 x2 x3 x3 x2 x x1 x x 2 x 2 1 x x3 x2 x3 x1 x x2 x2 x x3 12 1-D Finite-element Methods with Tent Test Functions Integration by Parts On the intervals, the derivatives are defined: u2 u1 x x d 2 1 u x dx u3 u2 x3 x2 1 x x d 2 x 2 1 dx 1 x2 x3 x1 x x2 x2 x x3 x1 x x2 x2 x x3 13 1-D Finite-element Methods with Tent Test Functions Integration by Parts Thus: x3 d d x u x dx x dx 2 dx 1 x2 x1 3 1 u2 u1 1 u3 u2 dx dx x2 x1 x2 x1 x x3 x3 x2 x2 2 x 1 u2 u1 1 u3 u2 x x 2 1 x3 x2 x2 x1 x2 x1 x2 x3 x3 x2 u2 u1 u3 u2 x2 x1 x3 x2 14 1-D Finite-element Methods with Tent Test Functions Integration by Parts In general: k uk uk 1 1 d d x u x dx x dx k dx x xk xk 1 xk xk 1 dx k 1 k 1 xk 1 x xk 1 xk uk 1 uk 1 dx xk 1 xk xk 1 xk uk uk 1 uk 1 uk 1 1 xk xk 1 xk 1 xk xk xk 1 xk xk 1 xk 1 xk xk 1 xk uk uk 1 uk 1 uk xk xk 1 xk 1 xk 15 1-D Finite-element Methods with Tent Test Functions Integration by Parts Substituting this back into our equation, we get: x uk 1 uk uk uk 1 k 1 k x dx xk 1 xk xk xk 1 xk 1 0 x or, expanding the left-hand side: k 1 x uk 1 1 1 1 uk 1 uk 1 k x dx uk xk xk 1 xk 1 xk 0 xk 1 xk xk xk 1 xk 1 x 16 1-D Finite-element Methods with Tent Test Functions Integration by Parts x In the special case that is a constant, this simplifies further: 0 uk 1 1 1 1 xk 1 xk 1 uk 1 u u k k 1 xk xk 1 x x x x x x 0 2 k k 1 k 1 k k 1 k 17 1-D Finite-element Methods with Tent Test Functions The Test Functions The function to find the approximations is straight-forward: function [ v ] = uniform1d( x, uab, rho ) n = length( x ) - 2; idx = 1./diff(x); M = diag( -(idx( 1:end - 1 ) + idx( 2: end )) ) + ... diag( idx( 2:end - 1 ), -1 ) + ... diag( idx( 2:end - 1 ), +1 ); xk 2 b = zeros ( n, 1 ); k 1 int( rho, a, b ) approximates for k = 1:n b(k) = int( rho, x(k), x(k + 1), x(k + 2) ); end x x dx xk b(1) = b(1) - idx(1)*uab(1); b(end) = b(end) - idx(end)*uab(end); v = [uab(1); M \ b; uab(2)]; end 18 1-D Finite-element Methods with Tent Test Functions The Test Functions In Matlab, recall that we may evaluate functions at a vector of points: – Let a, b, c be the points x = linspace( a, c, n ); rhox = rho( x ).*((x < b).*(x – a)/(b – a) + (x >= b).*(x – c)/(b – c)); xa b a x xc b c a xb b xc 19 1-D Finite-element Methods with Tent Test Functions The Test Functions For example, we get the following graph with this Matlab code: >> >> >> >> >> >> >> >> a = 1; b = 2; c = 5; x = linspace( a, c, 100 ); tentx = (x < b).*(x - a)/(b - a) + (x >= b).*(x - c)/(b – c); cosx = cos(x).*tentx; hold on plot( x, cosx, 'b' ); plot( x, cos(x), 'r' ); plot( x, tentx, 'm' ); 20 1-D Finite-element Methods with Tent Test Functions The Test Functions d2 u x 2 u x sin 4 x dx 2 Using the exact same example from the last uniform case: >> >> >> >> >> >> x_uneq = [0 0.16 0.25 0.33 0.41 0.5 0.59 0.67 0.75 0.84 1]'; u_uniform = uniform1d( x_uneq, [0, 0], @rho ); u_tent = tent1d( x_uneq, [0, 0], @rho ); plot( x_uneq, u_uniform, 'b+' ); hold on plot( x_uneq, u_tent, 'rx' ); 21 1-D Finite-element Methods with Tent Test Functions The Test Functions To demonstrate that this is better than the previous approximation: >> plot( x_uneq, u_uniform - u(x_uneq), 'b+' ); >> plot( x_uneq, u_tent - u(x_uneq), 'rx' ); >> norm( u_uniform - u(x_uneq) ) ans = 0.0012 >> norm( u_tent - u(x_uneq) ) ans = 4.7368e-011 Recall that the function u(x) is the exact solution 8 × 10–4 –1.8 × 10–11 22 1-D Finite-element Methods with Tent Test Functions Summary In this topic, we have used tent-shaped test functions as opposed to uniform test functions – The integration-by-parts was simpler • We avoided the end points as they were zero • No impulse functions… – More work is required for the code – Better solution for our example 23 1-D Finite-element Methods with Tent Test Functions What’s Next? We will next look at how we can extend this to two dimensions – Use triangles to divide up the region instead of intervals • Tessellations – Approximate the solution using interpolating polynomials Intervals Tessellation 24 1-D Finite-element Methods with Tent Test Functions References [1] Glyn James, Advanced Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2011, §§9.2-3. 25 1-D Finite-element Methods with Tent Test Functions Usage Notes • • These slides are made publicly available on the web for anyone to use If you choose to use them, or a part thereof, for a course at another institution, I ask only three things: – that you inform me that you are using the slides, – that you acknowledge my work, and – that you alert me of any mistakes which I made or changes which you make, and allow me the option of incorporating such changes (with an acknowledgment) in my set of slides Sincerely, Douglas Wilhelm Harder, MMath dwharder@alumni.uwaterloo.ca 26