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

using fixed-point iteration. That is, using the initial guess x0 = 1, obtain a sequence of real numbers

x1 = ex0 x2 = ex1



x50 = ex49



which tends to the value x = 0.567143 . . ., that is, a root of the function f (x) = x        ex.

Formally, the iteration can be written as

xi+1 = exi for i = 0, 1, 2, . . . with x0 = 1.

The limit x satisfies f (x) = 0.


|                |

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 s = 1015. 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        ex.] Is the error what you expected (i.e. is

it smaller than s)?        [5]


创建时间:2022-05-06 13:46