Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Main Examination period 2022/23

MTH6150: Numerical Computing in C and C++

Coursework 1 [100 marks]

Question 1. [20 marks] Euler sum.

Leonard Euler discovered in 1735 that

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]

Question 2. [20 marks] Inner products, sums and norms.

The Fibonacci numbers are defined as follows:

(a)  Write a program that outputs the first N Fibonacci numbers.

Organize the program for the Fibonacci numbers by constructing a valarray 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

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

and the variance by convention is given as

[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.

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

and the variance by convention is given as

[10]

Remark: Your code must 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.

Question 5. [20 marks] Numerical integration.

We wish to compute the definite integral

numerically, with endpoints a = 0 and b = 4, and compare to the exact result, Iexact  = 2π .

(a)  Use the composite trapezium rule

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 = 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 .

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]