UW AMath High Performance Scientific Computing
AMath 483/583 Class Notes
Spring Quarter, 2011

Previous topic

Homework 2

Next topic

Homework 4

This Page

Homework 3ΒΆ

Due Thursday, April 21, 2011, by 11:00pm PDT, by pushing to your bitbucket repository.

The goal of this homework is to gain some more practice with Fortran 90, including subroutines, functions, and modules, and to gain experience with Makefiles. You will also learn how to use the select case statement and experiment with Newton’s method for solving a nonlinear equation.

Before tackling this homework, you should read some of the class notes and links they point to. In particular, the following sections are relevant:

Make sure you update your clone of the class repository before doing this assignment:

$ hg pull -u     # pulls and updates

You should also create a directory $MYHG/homeworks/homework3 to work in since this is where your modified versions of the files will eventually need to be.


  1. The Fortran files in $CLASSHG/codes/fortran/roots implement Newton’s method to find the n th root of a number a by finding a zero of the function f(x) = x^n - a. See Special functions for a reminder of how Newton’s method works. You have probably seen this before in calculus or a numerical methods course. The README.txt file in this directory explains how to use the code and create plots similar to those shown in Special functions. Experiment with this code and read through it carefully to make sure you understand it all.

  2. Create a directory $MYHG/homeworks/homework3 in which you will produce modified versions of these codes. Rename the newtonroots.f90 module as newtonzero.f90 in your directory and modify this code so that it uses Newton’s method to find the zero of an arbitrary user-specified function f(x) rather than only the function f(x)=x^n-a.

    To do this, do the following:

    • Create a new module functions.f90 that contains two functions named f(x) and fprime(x) that return the desired function and the derivative of that function respectively.
    • The newtonzero.f90 module should then use functions in order to make these functions available to Newton’s method.
    • You will have to modify the Newton iteration to use these general functions instead of x^n-a and its derivative.
    • Rename the nth_root(a, n, x0) function as find_zero(x0) since the old name is no longer appropriate. Also, it is no longer appropriate for a and n to be arguments since the function is now specified differently.
    • Make suitable changes to the testroot.f90 main program, and rename it testzero.f90. Note that depending on what function you specify in functions.f90, you might not know the Correct value any longer.
    • Function evaluations are also written to files fvals.txt and newtonpts.txt for use in creating a plot via make plot. Make sure this still works with the changes you’ve made, and that the plots come out looking reasonable.
    • You will also have to modify the Makefile in various ways!
    • Modify the comments in the program and the README.txt file so they are consistent with the code.

    You can test your program with any function(s) you choose. For example you might start with f(x) = x^2 - 2 but also test other things.

  3. For the code that you turn in, specify two different functions in the module functions.f90 by using the select case statement in Fortran 90 as described below.

    At the start of your module functions, before the contains statement, include the lines:

    implicit none
    integer :: fcn_choice

    This defines fcn_choice as a module variable that will be available to any program, subroutine, or function that has a use functions statement. The save command means that if the value of fcn_choice is set in one place, that value will be seen elsewhere too.

    In the function f(x) within this module, include the lines:

    select case (fcn_choice)
        case (1)
            f = x**4 - 16.d0
        case (2)
            f = -x**3 + x**2 + x + 1.d0
        case default
            print *, "*** Unrecognized fcn_choice"
    end select

    This specifies two different function and the one that will actually be used depends on the value of fcn_choice.

    You will have to do something similar in the function fprime(x) to set the derivative properly depending on which function is selected.

  4. Modify the main program in testzero.f90 so that the program prompts the user to input the value of fcn_choice (the way the original program testroot.f90 prompts for a and n).

  5. Add a select case statement to program testzero to print out the appropriate Correct value for the two test cases, using the fact that the second case has a single zero at x^*\approx 1.83928675521416.

    Make sure your program works for both cases. To get a feel for how Newton’s method works (and breaks down), you might try initial guesses x0 = -1. or x0 = 1.. What goes wrong in the latter case? (No need to turn anything in for this, but think about it!)

  6. Your module newtonzero should also work if we provide a different main program and/or functions module for grading purposes.

  7. Make sure your directory $MYHG/homeworks/homework3 contains the following files:

    • newtonzero.f90
    • testzero.f90
    • functions.f90
    • Makefile
    • README.txt

    You can include makeplot.py if you like, but since you’re not asked to modify it for this homework, you’re not required to add it to your homework submission.

When you are done, don’t forget to use the hg add, hg commit, and hg push commands to push these to your bitbucket repository for grading.