==================================== Homework 2 ==================================== Due 23.59, Sep/13/2020 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Managing computations through scripting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In this homework you will try out some very basic perl scripting to automate computations to find the roots of a function :math:`f(x)`, i.e. approximate solutions to the equation :math:`f(x)=0`. We will consider three different functions: :math:`f(x)=x, f(x)=x^2`, and :math:`f(x)=\sin x + \cos x^2` and use `Newton's method`__ to find their roots. 0. Start with cloning the course Bitbucket repository: .. code-block:: none $ git clone https://xxx@bitbucket.org/motamed/hpsc2020.git where ``xxx`` is your Bitbucket user name. You will need to give me your UNM-email address connected to your Bitbucket user name so that I give you read previlage. You will find the two files ``newtonS.f90.Template`` and ``newtonS.pl`` in the subdirectory ``/hw2``. The file ``newtonS.pl`` contains a basic perl script which reads the template file one line at a time and replaces the strings ``FFFF`` and ``FPFP`` with "functions'' and "derivatives'' (defined inside ``newtonS.pl``). 1. Run the script by typing in a bash terminal: ``$ perl newtonS.pl`` and inspect the output. 2. Currently the implementation of Newton's method iterates 10 times which is too much for some functions and too little for others. Modify the program (i.e. the template file) so that it iterates until the quantity :math:`(E_{\rm abs})_{n+1} = |x_{n+1}-x_n|`, which approximates the absolute error, is less than :math:`10^{-15}`. It is probably easiest to do this by changing the do loop to a `do-while loop`__. 3. Linear and quadratic convergence means that the errors in subsequent iterations satisfy the relations :math:`(E_{\rm abs})_{n+1} \approx {\rm Const.} \times (E_{\rm abs})_{n}` and :math:`(E_{\rm abs})_{n+1} \approx {\rm Const.} \times (E_{\rm abs})_{n}^2` respectively. To determine the rate of convergence for Newton's method modify the ``write`` statement in the template file to include the ratios :math:`(E_{\rm abs})_{n+1} /(E_{\rm abs})_{n}` and :math:`(E_{\rm abs})_{n+1} / (E_{\rm abs})_{n}^2`. 4. Determine the rate of convergence for the three different functions and try to explain. The key here is to understand what is happening to :math:`f'(x)` close to the root. Is it possible to come up with a **modified Newton's method** to regain the quadratic convergence for the problematic function? You do not need to implement it. Explain how this can be done. 5. Modify the script ``newtonS.pl`` so that the standard output from your Newton code gets redirected into a file ``tmp.txt`` by changing ``system("./a.out ");`` to ``system("./a.out > tmp.txt");``. 6. After each execution open the file ``tmp.txt`` and replace the blank spaces in between the results on each line by a comma with a single space on each side. Print the resulting string to the screen. There are infinitely many ways to do this in perl, here is a step by step approach: .. code-block:: none $line =~ s/\s+/ , /g; # Globally replace chunks of whitespace with a " , " $line = $line . "\n"; # Add a newline as the above removes it. $line =~ s/ , $/ /; # Remove the last " , " $line =~ s/ , / /; # Remove the first " , " print $line; # Print to screen You may find a more elegant way to do this. This kind of operation can be good if you need to reformat your output to conform with some particular table format, for example in :math:`\LaTeX`. 7. Summarize your findings in a report where you use your tabulated data. Make sure to check the report and codes into a sub-directory, named ``HW2`` in your repository and to add a description in the low-level README file where I find the report and codes and how I could go about running your code. See the sample README file in `Homework 1`__. __ http://math.unm.edu/~motamed/Teaching/Fall20/HPSC/newton.html __ https://en.wikipedia.org/wiki/Do_while_loop#Fortran __ http://math.unm.edu/~motamed/Teaching/Fall20/HPSC/hw1.html