




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





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:
Grading:
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
u′′(x) =
u(x − h) − 2 u(x) + u(x + h) 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′′′(ξ)
j=
vj a(uh, φj )
j=
vj (f, φj )
= (f,
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:
Answer: Here are the results:
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-
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:
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.
(^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