The cosine function cos(x) can be represented as an infinite series as shown below. An obvious way of computing cos(x) is to sum the terms until a term is less than certain tolerance value, say 0.0000005, in single precision.
This is not a good method to compute cos(x) accurately and Fortran 90 does not use it to implement the COS(x) intrinsic function, because there could be so many numerical problems. However, this is a very good chance for you to gain some experience in finite precision arithmetic.
The problem you should solve is very simple:
| Read in a real number x, and sum the above infinite series until a term is smaller than the given tolerance 0.0000005. Note that x can be any value, large or small, positive or negative, etc. You should not expect x to be of some special type and in some range. Moreover, to yield a reasonably good result, some understanding of trigonometry functions would be needed. A very small tolerance or even the use of double precision may not help much. Consequently, use the default single precision only (i.e., REAL without any KIND). |
1000.0 0.5 -20.5 0.0 +3.1456
For each x, your program should print the following four output lines. The first line shows the input value x, the second is the COS(x) computed using the infinite series (i.e., your COS(x)), the third shows the result computed by Fortran 90's intrinsic function COS(x), and the fourth is the absolute value of the difference of the two computed values.
Input Value = x My cos(x) = cos(x) computed using the above infinite series Fortran cos(x) = Result from Fortran 90 intrinsic function COS(x) Error = The absolute difference of the two computed value
The above four output lines should be repeated for each input value with a blank line between two groups. The complete output should look like the following:
*** CS3911 Programming Assignment #1 *** *** COS(x) Computation Experiment *** --- the above four lines for the first input value --- --- the above four lines for the second input value --- --- output for other input values --- *** Done due to end-of-file ***
The following is a possible input file, which will be used as a sample testing input:
-70000.0 -10000.0 -1500.0 -50.0 -10.0 -1.0 0.0 1.5 3.14156 7.0 15.0 100.0 1000.0 100000.0 500000.0
This procedure may be repeated a number of times with different input files to see if your program works correctly.f90 prog1.f90 -o prog1 <-- compile your program prog1 < our-test-file <-- test your program
! ----------------------------------------------------------- ! NAME : John Smith User ID: xxxxxxxx ! DUE DATE : mm/dd/yyyy ! PROGRAM ASSIGNMENT # ! FILE NAME : xxxx.yyyy.zzzz (your unix file name) ! PROGRAM PURPOSE : ! A couple of lines describing your program briefly ! -----------------------------------------------------------
Here, User ID is the one you use to login. It is not your social security number!
For each function in your program, include a simple description like this:
! ----------------------------------------------------------- ! FUNCTION xxyyzz : (function name) ! the purpose of this function ! PARAMETER USAGE : ! a list of all parameters and their meaning ! FUNCTION CALLED : ! a list of functions that are called by this one ! -----------------------------------------------------------
Bad programming style and missing headers will cost you 30%.
Note that the file name has to be README rather than readme or Readme. Note also that there is no filename extension, which means filename such as README.TXT is NOT acceptable.
README must be a plain text file. We do not accept files produced by any word processor. Moreover, watch for very long lines. More precisely, limit the length of each line to no more than 80 characters with the Return key for line separation. Missing this file, submitting non-text file, file with long lines, or providing incorrect and/or vague answers costs you 40%. Suggestion: Use a Unix text editor to prepare your README rather than a word processor.