Numerical Computing in C and C++
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
MTH6150: Numerical Computing in C and C++
Assignment date: Monday 13/7/2022
Submission deadline: Friday 12/8/2022 at 23:59 BST
The coursework is due by Monday, 20th March 2023 at 23:59 GMT. Please submit your
C++ code files, including code comments, on QMPlus. The code used to answer each ques-
tion should also be submitted 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 plagia-
rism. Plagiarism is an assessment offence and carries severe penalties. Please read the
instructions above carefully to avoid common mistakes. If in doubt, please ask. Late sub-
missions will not be accepted after solutions are posted on QMPlus (i.e. immediately after
the deadline).
Coursework 1 [100 marks]
Question 1. [20 marks] Euler sum.
Leonard Euler discovered in 1735 that
∞∑
n=1
1
n2
=
π2
6
Construct a valarray to store the elements of the sequence an = 1/n2
in memory. Use standard summation to evaluate this sum for some large number of steps,
which should be an input to the program, to check the formula. Standard C++ does not know
about π, so you could add the following to your code:
long double pi = 3.1415926535897932385;
Display the difference between your numerical result and the exact result on the screen to 18
digits of accuracy. [20]
1
Question 2. [20 marks] Inner products, sums and norms.
The Fibonacci numbers are defined as follows:
F0 = 0, F1 = 1
Fn = Fn−1 + Fn−2, n ≥ 2
(a) Write a program that outputs the first N Fibonacci numbers.
Organize the program for the Fibonacci numbers by constructing a valarray
long> of size N = 20 to hold the values of Fn. Initialize the first 2 elements of the
valarray to the starting values F0 = 0, F1 = 1. Then write a for or while loop to
compute the elements F2, . . . , FN of the sequence and display them on the screen. [10]
(b) The ratio of successive numbers tends to a limit which is the golden ratio
lim
n→∞
Fn
Fn−1
= ϕ
ϕ =
√
5 + 1
2
Write code to check that the ratio of the values you calculate does approach this limit
for large n. Note that the square root of x in C++ is sqrt(x). Display the difference
of your numerically evaluated ratio and the golden ratio on the screen to 18 digits of
accuracy. [10]
Question 3. [20 marks] Mean, variance, min, max using vectors.
(a) Write two functions, called mean and var, that return the mean and variance of the
input argument, which should be a vector. So the first of these
would be of the form:
long double mean(const vector v){
...
}
The mean of a sample x1, x2, . . . , xn is
μˆ =
1
n
n∑
k=1
xk
and the variance by convention is given as
σˆ2 =
1
n− 1
n∑
k=1
(xk − μˆ)2
[10]
(b) Also write functions mymin and mymax to find the minimum and maximum of a
vector. [10]
Test all of these functions on a vector containing the elements
{1, 2, 3, 4, 5}. The mean and variance should be 3 and 2.5 respectively.
2
Question 4. [20 marks] Mean, variance, min, max using valarrays.
(a) Write two functions, called mean and var, that return the mean and variance of the
input argument, which should be a valarray. So the first of these
would be of the form:
long double mean(const valarray v){
...
}
The mean of a sample x1, x2, . . . , xn is
μˆ =
1
n
n∑
k=1
xk
and the variance by convention is given as
σˆ2 =
1
n− 1
n∑
k=1
(xk − μˆ)2
[10]
Remark: Your codemust make use of the following hints.
(i) Use X.sum() to sum all elements of a valarray X.
(ii) Use X*X to square all elements of a valarray X.
(iii) Use X-Y to compute the difference between all elements of a valarray X and a
valarray Y.
(iv) Use (X*Y).sum() to compute the inner (dot) product of a valarray X and a
valarray Y.
(b) Also write functions mymin and mymax to find the minimum and maximum of a
valarray. You may use X.min() and X.max() if you wish. [10]
Test all of these functions on a valarray containing the elements
{1, 2, 3, 4, 5}. The mean and variance should be 3 and 2.5 respectively.
3
Question 5. [20 marks] Numerical integration.
We wish to compute the definite integral
I =
∫ b
a
√
(b− x)x dx
numerically, with endpoints a = 0 and b = 4, and compare to the exact result, Iexact = 2π.
(a) Use the composite trapezium rule∫ b
a
f(x)dx '
n∑
i=0
wifi = ~w ∙ ~f, ~w = Δx
2
∗ {1, 2, 2, ..., 2, 1}
to compute the integral I , using n + 1 = 64 equidistant points in x ∈ [a, b], that is,
xi = a + iΔx, for i = 0, 1, ..., N , where Δx = b−an is the grid spacing.
Use instances of a vector to store the values of the gridpoints xi,
function values fi = f(xi) and numerical integration weights wi.
Output your numerical result Itrapezium and the difference Itrapezium − Iexact. [5]
(b) Repeat part (5a) using instances of a valarray to store the values
of the gridpoints xi, function values fi = f(xi) and numerical integration weights wi.
Evaluate the inner (dot) product ~w ∙ ~f by using (W*F).sum() to compute the inner
product of a valarray W and a valarray F. [10]
(c) Use your code from either part (5a) or part (5b) and turn it into a function of the
endpoints a and b of the integral and the number N of gridpoints. That is, write a
function long double integral(const long double a, const long
double b, const int n){...} which will take a, b, n as input, construct the
gridpoints and weights of the composite trapezium rule, evaluate the function values on
these gridpoints, and return the dot product ~w ∙ ~f . [5]