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 \(f(x)\), i.e. approximate solutions to the equation \(f(x)=0\). We will consider three different functions: \(f(x)=x, f(x)=x^2\), and \(f(x)=\sin x + \cos x^2\) and use Newton’s method to find their roots.

  1. Start with cloning the course Bitbucket repository:

    $ 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).

  2. Run the script by typing in a bash terminal: $ perl newtonS.pl and inspect the output.

  3. 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 \((E_{\rm abs})_{n+1} = |x_{n+1}-x_n|\), which approximates the absolute error, is less than \(10^{-15}\). It is probably easiest to do this by changing the do loop to a do-while loop.

  4. Linear and quadratic convergence means that the errors in subsequent iterations satisfy the relations \((E_{\rm abs})_{n+1} \approx {\rm Const.} \times (E_{\rm abs})_{n}\) and \((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 \((E_{\rm abs})_{n+1} /(E_{\rm abs})_{n}\) and \((E_{\rm abs})_{n+1} / (E_{\rm abs})_{n}^2\).

  5. Determine the rate of convergence for the three different functions and try to explain. The key here is to understand what is happening to \(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.

  6. 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");.

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

$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 \(\LaTeX\).

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