Numerical Computing in C and C--
MTH6150: Numerical Computing in C and C++
Assignment date: Monday 14/3/2022
Submission deadline: Monday 2/5/2022 at 23:59 BST
The coursework is due by Monday, 2nd May 2022 at 23:59 BST. Please submit a report (in pdf format) containing answers to all questions, complete with written explanations and printed tables or figures. Tables should contain a label for each column. Figures must contain a title, axis labels and legend. Please provide a brief explanation of any original algorithm used in the solution of the questions (if not discussed in lectures). Code comments should be used to briefly explain your code. You must show that your program works using suitable examples. Build and run your code with any of the free IDEs discussed in class (such as Visual Studio, Xcode, CLion, or an online compiler), and include the code and its output in your report. The C++ code for each question must be pasted into the pdf file of your report, so that it can be commented on when marking. The code used to answer each question should also be submitted separately, together with the report, in a single cpp file or multiple cpp files. You may organise the code in different directories, one for each question. Late submissions will be treated in accordance with current College regulations. QMPlus automatically screens for plagiarism. Plagiarism is an assessment offence and carries severe penalties. In the questions below, use long double if your compiler supports it. If this is not supported, e.g. if you are using Visual Studio, you may use double.
Please read the instructions above carefully to avoid common mistakes. If in doubt, please ask. Reports that do not contain C++ code in it (but have code included separately in .cpp files) are subject to a 25% penalty. Reports that consist only of code and no explanation are subject to a 40% penalty. C++ code submitted without a report is subject to a 50% penalty. Reports not accompanied by any C++ code cannot be evaluated and will receive 0 marks. Only material submitted through QMPlus will be accepted.
Coursework [100 marks]
Question 1. [20 marks] Self-consistent iteration. Solve the transcendental equation
x = e−x
using fixed-point iteration. That is, using the initial guess x0 = 1, obtain a sequence of real numbers
x1 = e−x0 x2 = e−x1
. |
x50 = e−x49
. |
− |
Formally, the iteration can be written as
xi+1 = e−xi for i = 0, 1, 2, . . . with x0 = 1.
The limit x∞ satisfies f (x∞) = 0.
(a)
| − | |
digits of accuracy. [10]
(b) In how many iterations did your loop converge? [5]
(c) What is the final error in the above transcendental equation? [Hint: use the final value of
− |
it smaller than s)? [5]
Question 2. [20 marks] Inner products, sums and norms.
(a) Write a function named dot that takes as input two vectors A˙ = {A1, ..., An} ∈ Rn and
B˙ = {B1, ..., Bn} ∈ Rn (of type valarray<long double>) and returns their inner
product
A˙ ∙ B˙
n
Σ |
i=1
as a long double number. [5]
(b) Use this function to compute the sum Σn 1 for n = 106. To do so, you may define a
{ } |
i=1 i2
∙ ∙ − |
Remark: Define π = 3.1415926535897932385 to long double accuracy. [5]
(c) Repeat Question 2(a) using compensated (Kahan) summation and name your function
Σ |
n
i=1
c2 for n = 106 and c = 0.1.
Hint: Define a constant Euclidean n-vector C={c,c,...,c} as a valarray<long double> of size n and compute the inner product C˙ ∙ C˙.
Print the difference between your result and the exact value nc2. [5]
(d) Write code for a function object that has a member variable m of type int, a suitable constructor, and a member function of the form
{ |
m |
lm(A˙) =
‚ n
,m |
.Σ |
|Ai|m =
n
. Σ |
|Ai|
Σ1/m
(2)
∙ |
half marks awarded if you use a regular function instead of a function object.] [5]
Question 3. [20 marks] Finite differences.
(a) Write a C++ program that uses finite difference methods to numerically evaluate the second derivative f jj(x) of a function f (x) whose values on a fixed grid of points are specified f (xi) = fi, i = 0, 1, ..., N . Your code should use valarray<long double> containers to store the values of xi, fi and fijj. Assume the grid-points are
located at xi = (2i − N )/N on the interval [−1, 1] and use 2nd order finite differencing
to compute an approximation fijj for f jj(xi):
f0jj fijj fNjj
= fi+2 − 2fi+1 + fi
Δx2
= fi+1 − 2fi + fi−1
Δx2
= fi − 2fi−1 + fi−2
Δx2
if i = 0
if i = 1, 2, ..., N − 1
if i = N
with Δx = 2/N . Demonstrate that your program works by evaluating the second derivatives of a known function, f (x) = e−16x2 , with N + 1 = 64 points. Compute the difference between your numerical derivatives and the known analytical ones:
ei = fnjjumerical(xi) − fajjnalytical(xi)
at each grid-point. Output the values xi, fi, fijj, ei of each valarray<long double> on the screen and tabulate (or plot) them in your report to 10 digits of
accuracy. Comment on whether the error is what you expected. [10]
(b)
( ) |
( ) |
Here, the mean error (e) is defined by
i=0 |
N |
N + 1 |
i |
N + 1 1 |
Hint: you may use your code from Question 2d to compute the mean error. [10]
Question 4. [20 marks] Numerical integration. We wish to compute the definite integral
(b − x)x dx |
I = |
a |
numerically, with endpoints a = 0 and b = 4, and compare to the exact result, Iexact = 2π. In Questions 4a, 4b and 4c below, use instances of a valarray<long double> to store the values of the gridpoints xi, function values fi = f (xi) and numerical integration weights wi.
(a)
N |
∫ |
c |
a
Σi=0
wifi = w˙ ∙ f˙,
w˙ =
Δx
2 ∗ {1, 2, 2, ..., 2, 1}
∈ |
N |
dot or cdot, created in Question 2a or 2b, to easily evaluate the inner product w˙ ∙ f˙]. [5]
(b) Use the extended Simpson rule
∫ |
c |
a
Σi=0
wifi,
w˙ =
Δx
48 ∗ {17, 59, 43, 49, 48, 48, ..., 48, 49, 43, 59, 17}
N |
your numerical result ISimpson and the difference ISimpson − Iexact. [5]
(c) Use the Clenshaw-Curtis quadrature rule
2 |
N |
k=1 |
4k2−1 |
a |
i |
i |
i |
. 1 , i = 0 or i = N
2 |
i=0 |
w f , w
= b − a ∗
N 2. Σ Σ ,
Σ |
− |
1 − |
(N −1)/2 2 cos(2kθi) |
1 ≤ i ≤ N − 1 |
Hint: First compute the values of θi, xi, fi = f (xi) and wi and store them in
valarray<long double> containers, as shown in the lab. Then, you may use the function from Question 2a or 2c to compute the inner product w˙ ∙ f˙.
Output to the screen (and list in your report) your numerical result IClenshawCurtis and the difference IClenshawCurtis − Iexact. [5]
(d) Compute the integral I using a Mean Value Monte Carlo method with N = 10000
samples. Output to the screen (and list in your report) your numerical result IMonteCarlo
and the difference IMonteCarlo − Iexact. [5]
- 2022-03-04
- 2022-03-04
- 2022-03-04
- 2022-03-04