MTH6150: Numerical Computing in C and C++
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
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
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
long double mean(const vector
} ...
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
Test all of these functions on a vector
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
long double mean(const valarray
} ...
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
Test all of these functions on a valarray
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
Output your numerical result Itrapezium and the difference Itrapezium − Iexact . [5]
(b) Repeat part (5a) using instances of a valarray
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]
2023-03-06