首页 > > 详细

讲解MTH6150: Numerical Computing in C and C++

Main Examination period 2021/22
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.
1
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
.
.
.
which tends to the value x∞ = 0.567143 . . ., that is, a root of the function f(x) = x − e−x.
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) Write a for loop that performs the above iteration, starting from the initial condition
x0 = 1. Use an if and a break statement to stop the loop when the absolute value of
the difference |xi+1 − xi| between two consecutive iterations is less than a prescribed
tolerance  = 10−15. Use a long double to store the values of x, and the change in
x, between iterations. Use setprecision(18) to print out the final value of x to 18
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
x to compute and display the difference x − e−x.] Is the error what you expected (i.e. is
it smaller than )? [5]
2
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) and returns their inner
product
A~ ∙ B~ = Xn
i=1
AiBi (1)
as a long double number. [5]
(b) Use this function to compute the sum Pn
i=1
1
i2 for n = 106. To do so, you may define a
Euclidean n-vector A={1,1/2,1/3,...,1/1000000} as a valarray

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!