Due Saturday
Saturday, February 26, 11:59 P.M.
- Overview
- Assignment
- Hints
The purpose of this lab is to introduce you to using DO
loops in Fortran. Additionally, you will gain practice implementing two
fundamental algorithms of numerical mathematics (series approximation
and bisection).
Top
Write a fortran program called pie_approx that calculates either pi
or e (according to the user's specification) using the maximum possible
precision. Your program must satisfy the following requirements:
- You should error check the input provided by the user. If invalid
input is provided, you should print an error message and terminate
program execution.
- All calculations should be performed using 16-byte reals, which can
be specified using a declaration of the form
REAL(KIND=16) :: x, y
Note that in general, you can specify an n-byte real (for
n=4, 8, or 16) using the declaration
REAL(KIND=n) :: x, y
Warning: this behavior is specific to the Intel Fortran compiler; the
correct value of n may vary from one compiler to another.
For example, see the table on page 452 of the text for the correct
value of n used to sepcify a 4/8/16 byte (i.e. 32/64/128 bit)
real variable on various compilers.
- Your code must use only loops, branches, and the elementary arithmetic
operations (i.e. +,-,/,*, and **). Note that this
is actually redundant with the previous requirement since the standard
trigonometric and exponential functions only perform calculations using
8-byte reals.
- You should calculate the number e using the usual power
series representation of the exponential function centered at 0.
This expansion can be found in your Calculus textbook or here.
- You should calculate pi using bisection on the function
sin(x) on an interval containing only one zero.
- You should implement the calculation of sin(x) yourself using
the usual power series expansion at 0 found in Calculus textbooks, or here.
- In each case, you should test for convergence by checking that two
successive approximations are equal (i.e. the actual difference between
the two approximations is a smaller number than the machine can represent
at that magnitude).
- In addition to the approximation, your program should report how
many terms of the power series were needed to approximate e or
how many iterations of bisection were needed to approximate pi,
according to which approximation the user has specified.
Top
- To start out, just write a skeletal program that prompts the user for
which calculation they want. Put in an IF-THEN-ELSE statement
that prints a message saying something like "the calculation of e
will go here". In general, any code that serves as a placeholder
is called a stub.
- The calculation of e is easier. So, your next step should
be to implement a DO loop (inside the e clause of the
IF statement) for the power series expansion of the exponential
function. Make sure this calculation is correct to the maximum number
of digits of accuracy.
- Now get your power series for sin(x) working. Don't worry
about the bisection method yet, just try your calculation for several
different values of x and make sure they come back correct.
- Finish by adding an additional DO loop for the bisection
method. This loop should surround your power series approximation to
sin(x). If you set up the calculation correctly, you shouldn't
have to change anything inside the inner loop (except the indentation level).
- Test your program by checking against the various approximations to
pi and e that can be found on the web.
- Finally, go back and add the WRITE statements for the
requested stats on your program's performance if you haven't already
done so.
Top
Back to the course information page