Docsity

Finite Differences and Finite Elements: A Comparative Study, Assignments of Computer Science

Toggle sidebar
bg1
Partial Solution
Your Homework Assignment
Finite Differences and Finite Elements:
Getting to Know You
Dianne P. O’Leary
First, a thank you to those who followed the submission instructions.
How to submit your homework: Please submit your work on paper.
(Email submission is acceptable only in an emergency.) Put your work in
this order:
•A listing of the output produced by your experiments.
•A discussion for Problem 6.
•A listing of each of your Matlab functions. (Remember to document
them.)
Grading:
•Problem 1: 0 points (just an exercise worth doing).
•Clear output: 5 points.
•Discussion in Problem 6: 15 points.
•Design and correctness of Matlab programs: 20 points.
•Documentation: 10 points.
Second, I really believe that it is important that a program’s output be clear
and easy to understand without additional formatting and annotation.
Scientific computing in the workplace is a team sport, and your program’s
documentation and output are your means of communicating with colleagues.
Finally, here are the answers to the problems you did plus one or two more.
In this homework, we explore the nuts-and-bolts of finite difference and finite
element approximations to a simple problem:
−(a(x)u0(x))0+c(x)u(x)=f(x)forx∈(0,1)
with the functions a,c,andfgiven and u(0) = u(1) = 0.
We will assume that a(x)≥a0,wherea
0is a positive number, and c(x)≥0
for x∈[0,1].
In the finite difference approach, we approximate each derivative of uby a
finite difference:
u0(x)=u(x)−u(x−h)
h+O(h),
u
00(x)= u(x−h)−2u(x)+u(x+h)
h
2+O(h
2
).
Problem 1. Let M=6,a(x)=1andc(x) = 0, and write the 4 finite
difference equations for uat x=.2,.4,.6, and .8.
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Finite Differences and Finite Elements: A Comparative Study and more Assignments Computer Science in PDF only on Docsity!

Partial Solution Your Homework Assignment Finite Differences and Finite Elements: Getting to Know You Dianne P. O’Leary

First, a thank you to those who followed the submission instructions.

How to submit your homework: Please submit your work on paper. (Email submission is acceptable only in an emergency.) Put your work in this order:

  • A listing of the output produced by your experiments.
  • A discussion for Problem 6.
  • A listing of each of your Matlab functions. (Remember to document them.)

Grading:

  • Problem 1: 0 points (just an exercise worth doing).
  • Clear output: 5 points.
  • Discussion in Problem 6: 15 points.
  • Design and correctness of Matlab programs: 20 points.
  • Documentation: 10 points.

Second, I really believe that it is important that a program’s output be clear and easy to understand without additional formatting and annotation. Scientific computing in the workplace is a team sport, and your program’s documentation and output are your means of communicating with colleagues.

Finally, here are the answers to the problems you did plus one or two more.

In this homework, we explore the nuts-and-bolts of finite difference and finite element approximations to a simple problem:

−(a(x)u′(x))′^ + c(x)u(x) = f (x) for x ∈ (0, 1)

with the functions a, c, and f given and u(0) = u(1) = 0.

We will assume that a(x) ≥ a 0 , where a 0 is a positive number, and c(x) ≥ 0 for x ∈ [0, 1].

In the finite difference approach, we approximate each derivative of u by a finite difference:

u′(x) =

u(x) − u(x − h) h

  • O(h),

u′′(x) =

u(x − h) − 2 u(x) + u(x + h) h^2

  • O(h^2 ).

Problem 1. Let M = 6, a(x) = 1 and c(x) = 0, and write the 4 finite difference equations for u at x =. 2 ,. 4 , .6, and .8.

Answer:

1 h^2

u 1 u 2 u 3 u 4

f 1 f 2 f 3 f 4

where h = 1/5, uj ≈ u(jh), and fj = f (jh).

Problem 2. The Matlab function finitediff1.m, found on the website, implements the finite difference method for our equation. The inputs are the parameter M and the functions a, c, and f that define the equation. Each of these functions takes a vector of points as input and returns a vector of function values. (The function a also returns a second vector of values of a′.) The outputs of finitediff1.m are a vector ucomp of computed estimates of u at the mesh points xmesh, along with the matrix A and the right-hand side g from which ucomp was computed, so that A ucomp = g. Add documentation to the function finitediff1.m so that a user could easily use it, understand the method, and modify the function if necessary.

Answer: Documentation is posted on the website for the program finitediff2.m of Problem 3, which is very similar to finitediff1.m but more useful. Please remember that if you use a code like finitediff1.m and fail to include the name of the code’s author, or at least a reference to the website from which you obtained it, it is plagairism. Similarly, your implementation of finitediff2.m should probably include a statement like, “Derived from finitediff1.m by Di- anne O’Leary.”

Problem 3. Define a central difference approximation to the first deriva- tive by

u′(x) ≈

u(x + h) − u(x − h) 2 h

a. Use the Taylor series expansions

u(x + h) = u(x) + hu′(x) +

h^2 2

u′′(x) +

h^3 6

u′′′(ξ 1 ),

u(x − h) = u(x) − hu′(x) +

h^2 2

u′′(x) −

h^3 6

u′′′(ξ 2 ),

where ξ 1 is some point between x and x + h, and ξ 2 is some point between x and x−h, to show that the difference between u′(x) and our approximation is O(h^2 ) if u has a continuous third derivative.

b. Modify the function of Problem 2 to produce a function finitediff2.m that uses this approximation in place of the first order approximation.

Answer:

a. From the given equations, we obtain

u(x + h) − u(x − h) = 2hu′(x) +

h^3 6

u′′′(ξ 1 ) +

h^3 6

u′′′(ξ 2 ),

so, rearranging and using the Mean Value Theorem from calculus we obtain

u′(x) −

u(x + h) − u(x − h) 2 h

h^2 3

u′′′(ξ)

M∑− 2

j=

vj a(uh, φj )

M∑− 2

j=

vj (f, φj )

= (f,

M∑− 2

j=

vj φj )

= (f, vh).

b. We compute

a(φj , φj ) =

0

(φ′ j (x))^2 dx

∫ (^) (j+1)h

(j−1)h

(φ′ j (x))^2 dx

∫ (^) jh

(j−1)h

h^2

dx

h

and

a(φj , φj+1) =

0

φ′ j (x)φ′ j+1(x)dx

∫ (^) (j+1)h

jh

φ′ j (x)φ′ j+1 (x)dx

∫ (^) (j+1)h

jh

h

h

dx

h

So our system becomes

h

u 1 u 2 u 3 u 4

f 1 f 2 f 3 f 4

where uj is the coefficient of φj in the representation of uh and

fj =

0

f (x)φj (x)dx =

∫ (^) (j+1)h

(j−1)h

f (x)φj (x)dx,

which is h times a weighted average of f over the jth interval. The only difference between the finite difference system and this system is that we have replaced point samples of f by average values. Note that if a(x) is not constant, then the systems look even more different.

Problem 5. Write a function fe linear.m that has the same inputs and outputs as finitediff1.m but computes the finite element approximation to the solution using piecewise linear elements. Remember to store A as a sparse matrix.

Answer: See the code posted on the website.

Problem 6. Write a function fe quadratic.m that has the same inputs and outputs as finitediff1.m but computes the finite element approxi- mation to the solution using piecewise quadratic elements.

Answer: See the code posted on the website.

Problem 7. Use your four algorithms to solve 7 problems. Compute three approximations for each algorithm and each problem, with the number of unknowns in the problem chosen to be 9, 99, and 999. For each approxi- mation, print ‖ucomputed − utrue‖∞ where utrue is the vector of true values at the points jz, where z = 1/10, 1/100, or 1/1000, respectively. Discuss the results:

  • How easy is it to program each of the four methods? Estimate how much work Matlab does to form and solve the linear systems. (The work to solve the tridiagonal systems should be about 5M multiplica- tions, and the work to solve the 5-diagonal systems should be about 11 M multiplications, so you just need to estimate the work in forming each system.)
  • For each problem, note the observed convergence rate r: if the error drops by a factor of 10r^ when M is increased by a factor of 10, then the observed convergence rate is r.
  • Explain any deviations from the theoretical convergence rate: r = 1 and r = 2 for the two finite difference implementations, and r = 2 and r = 3 for the finite element implementations.

Answer: Here are the results:

PROBLEM 1

Using coefficient functions a(1) and c(1) with true solution u(1) Infinity norm of the error at the mesh points for various methods and numbers of interior mesh points M M = 9 99 999 1st order finite difference 2.1541e-03 2.1662e-05 2.1662e- 2nd order finite difference 2.1541e-03 2.1662e-05 2.1662e- Linear finite elements 1.3389e-13 1.4544e-14 1.4033e- Quadratic finite elements 3.1004e-05 3.5682e-09 3.6271e-

PROBLEM 2

Using coefficient functions a(1) and c(2) with true solution u(1) Infinity norm of the error at the mesh points for various methods and numbers of interior mesh points M M = 9 99 999

Discussion:

Clearly, the finite difference methods are easier to program and therefore are almost always used when x is a single variable. Finite elements become useful, though, when x has 2 or more components and the shape of the domain is nontrivial.

The bulk of the work in these methods is in function evaluations. We need O(M ) evaluations of a, c, and f in order to form each matrix. For finite differences, the constant is close to 1, but quad uses many function evaluations per call (on the order of 10), making formation of the finite element matrices about 10 times as expensive.

The experimental rate of convergence should be calculated as the log 10 of the successive errors (since we increase the number of mesh points by a factor of 10 each time). There are several departures from the expected rate of convergence:

  • finitediff1 is expected to have a linear convergence rate (r = 1), but has r = 2 for the first three problems because a′^ = 0 and the approxi- mation is the same as that in finitediff2.
  • The quadratic finite element approximation has r = 4 on Test Problems 1-4, better than the r = 3 we might expect. This is called supercon- vergence and happens because we only measured the error at the mesh points, whereas the r = 3 result was for the average value of the error over the entire interval.
  • Linear finite elements give almost an exact answer to Test Problem 1 at the mesh points (but not between the mesh points). This occurs because our finite element equations demand that

a(uh, φj ) = (u′ h, φ′ j ) = [−uh(xj− 1 ) + 2uh(xj ) − uh(xj+1)]/h = (f, φj ),

and our true solution also satisfies this relation.

  • In Test Problem 5, the coefficient function a has a discontinuous deriva- tive at x = 1/3. The matrix entries computed by the numerical integra- tion routine are not very accurate, so the finite element methods appear to have slow convergence. This can be fixed by extra calls to quad so that it never tries to integrate across the discontinuity.
  • The “solution” to Test Problem 6 has a discontinuous derivative, and the “solution” to Test Problem 7 is discontinuous. None of our methods compute good approximations, although all of them return a reasonable answer (See Figure 1) that could be mistaken for what we are looking for. The finite difference approximations lose accuracy because their error term depends on u′′. The finite element equations were derived from the weak formulation of our problem, and when we used integration by parts, we left off the boundary term that we would have gotten at x = 2/3, so our equations are wrong. This is a case of, “Be careful what you ask for.”
  • The entries in the finite element matrices are only approximations to the true values, due to inaccuracy in estimation of the integrals. This means that as the mesh size is decreased, we need to reduce the tolerance that we send to quad in order to keep the matrix accurate enough.

(^00) 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

x

u

Computed solutionTrue solution

Figure 1: The solution to the 7th test problem

  • The theoretical convergence rate only holds down to the round-off level of the machine, so if we took even finer meshes (much larger M ), we would fail to see the expected rate.