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

Previous topic

Homework 1

Next topic

Homework 3

This Page

Homework 2ΒΆ

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

The goal of this homework is to learn some basic Fortran 90, including subroutines, functions, arrays, if statements, and loops.

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:

$ cd $CLASSHG
$ hg pull -u     # pulls and updates

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

Assignment:

  1. Read the sections of the notes to become familiar with Fortran.

    The Fortran program $CLASSHG/codes/fortran/taylor_converge.f90 contains a subroutine to compute \exp(x) by Taylor series and an example of how it is used. As you will see in lecture, this Taylor series works well for x\geq 0 but not so well for large negative x. Instead it is better to use the Taylor series to approximate y=\exp(-x) and then use 1/y as the approximation.

    Modify taylor_converge.f90 to create taylor2.f90 that uses this technique to compute \exp(x) when x is negative. It should produce more accurate values for x < 0.

  2. Modify the code further to create taylor3.f90 so that the subroutine takes an array of x values as input and approximates the exponential function for each. The subroutine should have the form:

    subroutine exptaylor(x,npts,nmax,y,nterms)

    where npts is the length of the arrays x, y, and nterms.

    Modify the main program so it calls this subroutine only once to compute all the values previously printed out, and print these out in the same format as before. The output of running this code should look the same as that of Part 1.

  3. Consider the piecewise-defined function

    f_1(x) = \begin{cases} \cos(x), \quad & x< 0\\ \sin(x), \quad & 0\leq x\leq 1\\ \sqrt{x}, \quad & 2\leq x<3\\ x-3, \quad & \text{otherwise} \end{cases}

    The Fortran 90 file $CLASSHG/homeworks/homework2/pwfcns.f90 contains a main program to print out the values of the function at a sequence of points, but is missing the function definition. Fill this in and make sure it works.

    Note: Be careful to check the function values where the function is discontinuous – for example, f_1(0) should be 0, not 1. You can use the Python plotting script $CLASSHG/homeworks/homework2/pwfcns_check.py to compare your answers against the true solution; see the comments at the beginning of the script for more information.

  4. Make a directory $MYHG/homeworks/homework2 that contains your solutions, i.e the modified files:

    • taylor2.f90
    • taylor3.f90
    • pwfcns.f90

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.